|
| 1 | +--- |
| 2 | +description: Best practices for building stateful multi-agent workflows with LangGraph in TypeScript. Apply this rule when creating or modifying AI agent architectures, graph states, nodes, or conditional edges. |
| 3 | +globs: ["**/*.ts", "**/*.tsx"] |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | +# LangGraph TypeScript Guidelines |
| 7 | + |
| 8 | +Best practices and patterns for designing robust, type-safe multi-agent systems using LangGraph in TypeScript. |
| 9 | + |
| 10 | +## Core Principles |
| 11 | + |
| 12 | +- **State Immutability:** Treat the graph state as immutable. Nodes must return updates rather than mutating state directly. |
| 13 | +- **Strict Typing:** Always explicitly type your graph state, node inputs/outputs, and edge conditions. |
| 14 | +- **Single Responsibility Nodes:** Each node should do one logical job (e.g., retrieval, formatting, routing). |
| 15 | +- **Explicit Stop Conditions:** Ensure conditional edges always have a guaranteed termination path to prevent infinite execution loops. |
| 16 | + |
| 17 | +## State Definition |
| 18 | +Use the modern `Annotation.define` pattern to establish a type-safe graph state. |
| 19 | + |
| 20 | +```typescript |
| 21 | +import { Annotation } from "@langchain/langgraph"; |
| 22 | +import { BaseMessage } from "@langchain/core/messages"; |
| 23 | + |
| 24 | +// 1. Define the TypeScript interface representing your state |
| 25 | +export interface AgentState { |
| 26 | + messages: BaseMessage[]; |
| 27 | + nextStep: string; |
| 28 | + isCompleted: boolean; |
| 29 | +} |
| 30 | + |
| 31 | +// 2. Define the LangGraph State Annotation |
| 32 | +export const AgentStateAnnotation = Annotation.Root<AgentState>({ |
| 33 | + messages: Annotation<BaseMessage[]>({ |
| 34 | + reducer: (x, y) => x.concat(y), |
| 35 | + default: () => [], |
| 36 | + }), |
| 37 | + nextStep: Annotation<string>({ |
| 38 | + reducer: (x, y) => y ?? x, |
| 39 | + default: () => "start", |
| 40 | + }), |
| 41 | + isCompleted: Annotation<boolean>({ |
| 42 | + reducer: (x, y) => y ?? x, |
| 43 | + default: () => false, |
| 44 | + }), |
| 45 | +}); |
| 46 | +``` |
| 47 | + |
| 48 | +## Node Implementation |
| 49 | +Nodes are async functions that accept the current state and return a partial update to the state. |
| 50 | + |
| 51 | +```typescript |
| 52 | +import { AgentState } from "./state.js"; |
| 53 | + |
| 54 | +// Always type node inputs/outputs explicitly |
| 55 | +export async function callModelNode(state: typeof AgentStateAnnotation.State): Promise<Partial<AgentState>> { |
| 56 | + const { messages } = state; |
| 57 | + |
| 58 | + // Implement agent tool call or LLM completion logic here... |
| 59 | + |
| 60 | + return { |
| 61 | + messages: [/* new response message */], |
| 62 | + nextStep: "verify", |
| 63 | + }; |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## Edge and Router Logic |
| 68 | +Conditional edges determine the control flow of your graph dynamically. |
| 69 | + |
| 70 | +```typescript |
| 71 | +import { AgentState } from "./state.js"; |
| 72 | + |
| 73 | +// Router determines which node to navigate to next |
| 74 | +export function shouldContinue(state: typeof AgentStateAnnotation.State): "continue" | "end" { |
| 75 | + if (state.isCompleted || state.messages.length > 10) { |
| 76 | + return "end"; |
| 77 | + } |
| 78 | + return "continue"; |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +## Graph Assembly |
| 83 | +Assemble and compile the graph builder into a runnable application. |
| 84 | + |
| 85 | +```typescript |
| 86 | +import { StateGraph } from "@langchain/langgraph"; |
| 87 | +import { AgentStateAnnotation, AgentState } from "./state.js"; |
| 88 | +import { callModelNode } from "./nodes.js"; |
| 89 | +import { shouldContinue } from "./edges.js"; |
| 90 | + |
| 91 | +const workflow = new StateGraph(AgentStateAnnotation) |
| 92 | + .addNode("agent", callModelNode) |
| 93 | + .addEdge("__start__", "agent") |
| 94 | + .addConditionalEdges("agent", shouldContinue, { |
| 95 | + continue: "agent", |
| 96 | + end: "__end__", |
| 97 | + }); |
| 98 | + |
| 99 | +export const graph = workflow.compile(); |
| 100 | +``` |
0 commit comments