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.
Best use case
langchain-js is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
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.
Teams using langchain-js should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/langchain-js-majiayu000/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How langchain-js Compares
| Feature / Agent | langchain-js | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
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.
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
SKILL.md Source
# 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
```bash
npm install langchain @langchain/openai @langchain/core
```
### Basic Chat
```typescript
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
```typescript
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
```bash
npm install @langchain/anthropic
```
```typescript
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)
```bash
npm install @langchain/google-genai
```
```typescript
import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
const model = new ChatGoogleGenerativeAI({
modelName: 'gemini-pro',
temperature: 0,
});
```
## Streaming
```typescript
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
```typescript
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
```typescript
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
```typescript
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
```typescript
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)
```typescript
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)
```bash
npm install @langchain/openai langchain
```
### Document Loading
```typescript
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
```typescript
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
const splitter = new RecursiveCharacterTextSplitter({
chunkSize: 1000,
chunkOverlap: 200,
});
const chunks = await splitter.splitDocuments(docs);
```
### Vector Store
```typescript
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
```typescript
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
```typescript
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
```typescript
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)
```bash
npm install @langchain/langgraph
```
```typescript
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
```typescript
// 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
```bash
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 validationRelated Skills
langchain-tool-calling
How chat models call tools - includes bind_tools, tool choice strategies, parallel tool calling, and tool message handling
langchain-notes
LangChain 框架学习笔记 - 快速查找概念、代码示例和最佳实践。包含 Core components、Middleware、Advanced usage、Multi-agent patterns、RAG retrieval、Long-term memory 等主题。当用户询问 LangChain、Agent、RAG、向量存储、工具使用、记忆系统时使用此 Skill。
langchain-agents
Expert guidance for building LangChain agents with proper tool binding, memory, and configuration. Use when creating agents, configuring models, or setting up tool integrations in LangConfig.
langchain4j-vector-stores-configuration
Configure LangChain4J vector stores for RAG applications. Use when building semantic search, integrating vector databases (PostgreSQL/pgvector, Pinecone, MongoDB, Milvus, Neo4j), implementing embedding storage/retrieval, setting up hybrid search, or optimizing vector database performance for production AI applications.
Langchain Patterns
LangChain is a framework for building applications powered by LLMs. It helps manage the complexity of prompt chaining, memory, retrieval, agents, and tool use, making it faster to build AI application
llm-application-dev-langchain-agent
You are an expert LangChain agent developer specializing in production-grade AI systems using LangChain 0.1+ and LangGraph.
langchain-python
Instructions for using LangChain with Python Triggers on: **/*.py
langchain-architecture
Design LLM applications using the LangChain framework with agents, memory, and tool integration patterns. Use when building LangChain applications, implementing AI agents, or creating complex LLM w...
bgo
Automates the complete Blender build-go workflow, from building and packaging your extension/add-on to removing old versions, installing, enabling, and launching Blender for quick testing and iteration.
mcp-create-declarative-agent
Skill converted from mcp-create-declarative-agent.prompt.md
MCP Architecture Expert
Design and implement Model Context Protocol servers for standardized AI-to-data integration with resources, tools, prompts, and security best practices
mathem-shopping
Automatiserar att logga in på Mathem.se, söka och lägga till varor från en lista eller recept, hantera ersättningar enligt policy och reservera leveranstid, men lämnar varukorgen redo för manuell checkout.