Skip to content
langchain-js logo

LangChain.js

langchain-js

Builds LLM-powered applications with LangChain.js for chat, agents, and RAG. Use when creating AI applications with chains, memory, tools, and retrieval-augmented generation in JavaScript.

SKILL.md

Full skill instructions

LangChain.js

Framework for building LLM-powered applications. Provides abstractions for chat models, chains, agents, RAG, and memory with support for multiple providers.

Quick Start

npm install langchain @langchain/​openai @langchain/​core

Basic Chat

import { ChatOpenAI } from '@langchain/​openai';

const model = new ChatOpenAI({
  modelName: 'gpt-4o',
  temperature: 0.7,
});

const response = await model.invoke('What is the capital of France?');
console.log(response.content);

Chat Models

OpenAI

import { ChatOpenAI } from '@langchain/​openai';

const model = new ChatOpenAI({
  modelName: 'gpt-4o',
  temperature: 0,
  maxTokens: 1000,
});

const response = await model.invoke([
  { role: 'system', content: 'You are a helpful assistant.' },
  { role: 'user', content: 'Hello!' },
]);

Anthropic

npm install @langchain/​anthropic
import { ChatAnthropic } from '@langchain/​anthropic';

const model = new ChatAnthropic({
  modelName: 'claude-sonnet-4-20250514',
  temperature: 0,
});

const response = await model.invoke('Explain quantum computing');

Google (Gemini)

npm install @langchain/​google-genai
import { ChatGoogleGenerativeAI } from '@langchain/​google-genai';

const model = new ChatGoogleGenerativeAI({
  modelName: 'gemini-pro',
  temperature: 0,
});

Streaming

import { ChatOpenAI } from '@langchain/​openai';

const model = new ChatOpenAI({
  modelName: 'gpt-4o',
  streaming: true,
});

const stream = await model.stream('Tell me a story');

for await (const chunk of stream) {
  process.stdout.write(chunk.content as string);
}

Prompt Templates

import { ChatPromptTemplate } from '@langchain/​core/​prompts';

const prompt = ChatPromptTemplate.fromMessages([
  ['system', 'You are a {role} assistant.'],
  ['user', '{input}'],
]);

const formattedPrompt = await prompt.format({
  role: 'helpful',
  input: 'What is TypeScript?',
});

With Model Chain

import { ChatOpenAI } from '@langchain/​openai';
import { ChatPromptTemplate } from '@langchain/​core/​prompts';

const model = new ChatOpenAI();
const prompt = ChatPromptTemplate.fromMessages([
  ['system', 'Translate the following to {language}'],
  ['user', '{text}'],
]);

const chain = prompt.pipe(model);

const response = await chain.invoke({
  language: 'French',
  text: 'Hello, how are you?',
});

Output Parsers

import { ChatOpenAI } from '@langchain/​openai';
import { ChatPromptTemplate } from '@langchain/​core/​prompts';
import { StringOutputParser, JsonOutputParser } from '@langchain/​core/​output_parsers';

// String output
const stringChain = prompt.pipe(model).pipe(new StringOutputParser());
const text = await stringChain.invoke({ input: 'Hello' });

// JSON output
const jsonChain = prompt.pipe(model).pipe(new JsonOutputParser());
const data = await jsonChain.invoke({ input: 'List 3 colors as JSON' });

Structured Output

import { z } from 'zod';
import { ChatOpenAI } from '@langchain/​openai';

const model = new ChatOpenAI({ modelName: 'gpt-4o' });

const schema = z.object({
  name: z.string().describe('The person name'),
  age: z.number().describe('The person age'),
  occupation: z.string().describe('The person occupation'),
});

const structuredModel = model.withStructuredOutput(schema);

const result = await structuredModel.invoke(
  'Extract info: John is 30 years old and works as a developer.'
);
// { name: 'John', age: 30, occupation: 'developer' }

Memory (Conversation History)

import { ChatOpenAI } from '@langchain/​openai';
import { ChatPromptTemplate, MessagesPlaceholder } from '@langchain/​core/​prompts';
import { RunnableWithMessageHistory } from '@langchain/​core/​runnables';
import { ChatMessageHistory } from 'langchain/​memory';

const model = new ChatOpenAI();

const prompt = ChatPromptTemplate.fromMessages([
  ['system', 'You are a helpful assistant.'],
  new MessagesPlaceholder('history'),
  ['user', '{input}'],
]);

const chain = prompt.pipe(model);

// In-memory store
const messageHistories: Record<string, ChatMessageHistory> = {};

const chainWithHistory = new RunnableWithMessageHistory({
  runnable: chain,
  getMessageHistory: async (sessionId) => {
    if (!messageHistories[sessionId]) {
      messageHistories[sessionId] = new ChatMessageHistory();
    }
    return messageHistories[sessionId];
  },
  inputMessagesKey: 'input',
  historyMessagesKey: 'history',
});

// Use with session
const response = await chainWithHistory.invoke(
  { input: 'My name is Alice' },
  { configurable: { sessionId: 'user-123' } }
);

const response2 = await chainWithHistory.invoke(
  { input: 'What is my name?' },
  { configurable: { sessionId: 'user-123' } }
);
// Response includes "Alice"

RAG (Retrieval-Augmented Generation)

npm install @langchain/​openai langchain

Document Loading

import { TextLoader } from 'langchain/​document_loaders/​fs/​text';
import { PDFLoader } from 'langchain/​document_loaders/​fs/​pdf';
import { WebLoader } from 'langchain/​document_loaders/​web/​cheerio';

// Load text file
const textLoader = new TextLoader('./​document.txt');
const textDocs = await textLoader.load();

// Load PDF
const pdfLoader = new PDFLoader('./​document.pdf');
const pdfDocs = await pdfLoader.load();

// Load webpage
const webLoader = new WebLoader('https://example.com');
const webDocs = await webLoader.load();

Text Splitting

import { RecursiveCharacterTextSplitter } from 'langchain/​text_splitter';

const splitter = new RecursiveCharacterTextSplitter({
  chunkSize: 1000,
  chunkOverlap: 200,
});

const chunks = await splitter.splitDocuments(docs);

Vector Store

import { MemoryVectorStore } from 'langchain/​vectorstores/​memory';
import { OpenAIEmbeddings } from '@langchain/​openai';

const embeddings = new OpenAIEmbeddings();

const vectorStore = await MemoryVectorStore.fromDocuments(
  chunks,
  embeddings
);

// Search
const results = await vectorStore.similaritySearch('query', 4);

Full RAG Chain

import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/​openai';
import { MemoryVectorStore } from 'langchain/​vectorstores/​memory';
import { ChatPromptTemplate } from '@langchain/​core/​prompts';
import { createRetrievalChain } from 'langchain/​chains/​retrieval';
import { createStuffDocumentsChain } from 'langchain/​chains/​combine_documents';

// Setup
const model = new ChatOpenAI({ modelName: 'gpt-4o' });
const embeddings = new OpenAIEmbeddings();
const vectorStore = await MemoryVectorStore.fromDocuments(chunks, embeddings);
const retriever = vectorStore.asRetriever();

// Prompt
const prompt = ChatPromptTemplate.fromTemplate(`
Answer the question based only on the following context:
{context}

Question: {input}
`);

// Chain
const documentChain = await createStuffDocumentsChain({ llm: model, prompt });
const retrievalChain = await createRetrievalChain({
  combineDocsChain: documentChain,
  retriever,
});

// Query
const response = await retrievalChain.invoke({
  input: 'What is the main topic?',
});

console.log(response.answer);

Agents & Tools

import { ChatOpenAI } from '@langchain/​openai';
import { TavilySearchResults } from '@langchain/​community/​tools/​tavily_search';
import { Calculator } from '@langchain/​community/​tools/​calculator';
import { createReactAgent } from '@langchain/​langgraph/​prebuilt';

const model = new ChatOpenAI({ modelName: 'gpt-4o' });

const tools = [
  new TavilySearchResults({ maxResults: 3 }),
  new Calculator(),
];

const agent = createReactAgent({
  llm: model,
  tools,
});

const result = await agent.invoke({
  messages: [{ role: 'user', content: 'What is the weather in Tokyo?' }],
});

Custom Tools

import { tool } from '@langchain/​core/​tools';
import { z } from 'zod';

const weatherTool = tool(
  async ({ location }) => {
    // Call weather API
    return `The weather in ${location} is sunny, 72°F`;
  },
  {
    name: 'get_weather',
    description: 'Get the current weather for a location',
    schema: z.object({
      location: z.string().describe('The city name'),
    }),
  }
);

const tools = [weatherTool];

LangGraph (Advanced Agents)

npm install @langchain/​langgraph
import { StateGraph, Annotation } from '@langchain/​langgraph';

const StateAnnotation = Annotation.Root({
  messages: Annotation<BaseMessage[]>({
    reducer: (x, y) => x.concat(y),
  }),
});

const workflow = new StateGraph(StateAnnotation)
  .addNode('agent', agentNode)
  .addNode('tools', toolNode)
  .addEdge('__start__', 'agent')
  .addConditionalEdges('agent', shouldContinue)
  .addEdge('tools', 'agent');

const app = workflow.compile();

const result = await app.invoke({
  messages: [{ role: 'user', content: 'Search for AI news' }],
});

Next.js API Route

// app/​api/​chat/​route.ts
import { ChatOpenAI } from '@langchain/​openai';
import { ChatPromptTemplate } from '@langchain/​core/​prompts';
import { StringOutputParser } from '@langchain/​core/​output_parsers';
import { StreamingTextResponse, LangChainAdapter } from 'ai';

const model = new ChatOpenAI({ streaming: true });
const prompt = ChatPromptTemplate.fromMessages([
  ['system', 'You are a helpful assistant.'],
  ['user', '{input}'],
]);

const chain = prompt.pipe(model).pipe(new StringOutputParser());

export async function POST(request: Request) {
  const { message } = await request.json();

  const stream = await chain.stream({ input: message });

  return new StreamingTextResponse(LangChainAdapter.toAIStream(stream));
}

Environment Variables

OPENAI_API_KEY=sk-xxxxxxxx
ANTHROPIC_API_KEY=sk-ant-xxxxxxxx
GOOGLE_API_KEY=xxxxxxxx
TAVILY_API_KEY=tvly-xxxxxxxx

Best Practices

  1. Use structured output - For reliable data extraction
  2. Stream responses - Better UX for chat applications
  3. Chunk documents - Optimize for context windows
  4. Use vector stores - For efficient similarity search
  5. Add memory - For conversational context
  6. Use LangGraph - For complex agent workflows
  7. Type with Zod - For input/​output validation

More skills from majiayu000

xiaohongshu logo
majiayu000/claude-arsenal

xiaohongshu

xiaohongshu

286 148
View
agent-task-conductor logo
majiayu000/claude-skill-registry

agent-task-conductor

Conduct multi-agent task orchestration and workflow coordination.

663 1
View
conductor-setup logo
majiayu000/claude-skill-registry

conductor-setup

Initialize project with Conductor artifacts (product definition,

663 1
View
animation-designer logo
majiayu000/claude-skill-registry

animation-designer

Expert in web animations, transitions, and motion design using Framer Motion and CSS

663 1
View
diagramming logo
majiayu000/claude-skill-registry

diagramming

Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...

663 1
View
h3-pg logo
majiayu000/claude-skill-registry-data

h3-pg

PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.

23 1
View
conductor-development logo
majiayu000/claude-skill-registry

conductor-development

Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...

663 1
View
conductor-status logo
majiayu000/claude-skill-registry

conductor-status

Display project status, active tracks, and next actions

663 1
View
dockerization logo
majiayu000/claude-skill-registry

dockerization

Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.

663 1
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View