Build, audit, review, and update LangGraph.js agents. Use PROACTIVELY when working with LangGraph, @langchain/langgraph, agent graphs, state machines, or AI workflows in TypeScript/JavaScript...
LangGraph decomposes agents into discrete nodes (functions) connected through shared state. Execution flows through a graph where nodes do work and edges determine what runs next.
State is the shared memory accessible to all nodes. Design state before nodes:
import { Annotation, MessagesAnnotation } from "@langchain/langgraph";
const AgentState = Annotation.Root({
...MessagesAnnotation.spec,
// Add custom fields with reducers
context: Annotation<string[]>({
reducer: (x, y) => x.concat(y),
default: () => [],
}),
});
Critical rules:
// Node: receives state, returns partial update
async function callModel(state: typeof AgentState.State) {
const response = await model.invoke(state.messages);
return { messages: [response] };
}
// Edge: determines next node
function shouldContinue(state: typeof AgentState.State) {
const lastMessage = state.messages.at(-1);
if (lastMessage?.tool_calls?.length) return "tools";
return END;
}
const graph = new StateGraph(AgentState)
.addNode("agent", callModel)
.addNode("tools", toolNode)
.addEdge(START, "agent")
.addConditionalEdges("agent", shouldContinue)
.addEdge("tools", "agent")
.compile(); // Required!
For conversation memory, human-in-the-loop, or fault tolerance:
import { MemorySaver } from "@langchain/langgraph";
const graph = workflow.compile({
checkpointer: new MemorySaver()
});
// Invoke with thread_id
await graph.invoke(input, {
configurable: { thread_id: "user-123" }
});
What would you like to do?
Wait for response, then read the matching workflow from workflows/ and follow it.
| Response | Workflow |
|---|---|
| 1, "new", "create", "build", "start", "scaffold" | workflows/build-new-agent.md |
| 2, "add", "feature", "implement", "extend" | workflows/add-feature.md |
| 3, "audit", "review", "check", "assess", "evaluate" | workflows/audit-agent.md |
| 4, "debug", "fix", "broken", "error", "bug", "issue" | workflows/debug-agent.md |
| 5, "test", "tests", "testing", "coverage" | workflows/write-tests.md |
| 6, "optimize", "performance", "slow", "fast", "improve" | workflows/optimize-agent.md |
| 7, other | Clarify intent, then select appropriate workflow |
# 1. TypeScript compiles?
npx tsc --noEmit
# 2. Tests pass?
npm test
# 3. Agent runs?
npx ts-node src/agent.ts
Report:
All in references/:
LangChain Fundamentals:
Architecture:
Features:
Patterns:
All in workflows/:
| File | Purpose |
|---|---|
| build-new-agent.md | Create a new LangGraph.js agent from scratch |
| add-feature.md | Add capabilities to an existing agent |
| audit-agent.md | Review architecture and identify issues |
| debug-agent.md | Find and fix agent bugs |
| write-tests.md | Test nodes, graphs, and integrations |
| optimize-agent.md | Improve performance and reduce latency |
All in templates/:
| File | Purpose |
|---|---|
| basic-agent.ts | Minimal ReAct agent scaffold |
| rag-agent.ts | Retrieval-augmented agent |
| multi-agent.ts | Multi-agent system with subgraphs |
For topics not fully covered here, consult: