Distributing development: when you want different teams to work on different parts of the graph independently, you can define each part as a subgraph, and as long as the subgraph interface (the input and output schemas) is respected, the parent graph can be built without knowing any details of the subgraph
Set up LangSmith for LangGraph development
Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM apps built with LangGraph — read more about how to get started here.
When the parent graph and subgraph have different state schemas (no shared keys), invoke the subgraph inside a node function. This is common when you want to keep a private message history for each agent in a multi-agent system.The node function transforms the parent state to the subgraph state before invoking the subgraph, and transforms the results back to the parent state before returning.
import { StateGraph, StateSchema, START } from "@langchain/langgraph";import * as z from "zod";const SubgraphState = new StateSchema({ bar: z.string(),});// Subgraphconst subgraphBuilder = new StateGraph(SubgraphState) .addNode("subgraphNode1", (state) => { return { bar: "hi! " + state.bar }; }) .addEdge(START, "subgraphNode1");const subgraph = subgraphBuilder.compile();// Parent graphconst State = new StateSchema({ foo: z.string(),});// Transform the state to the subgraph state and backconst builder = new StateGraph(State) .addNode("node1", async (state) => { const subgraphOutput = await subgraph.invoke({ bar: state.foo }); return { foo: subgraphOutput.bar }; }) .addEdge(START, "node1");const graph = builder.compile();
Full example: different state schemas
import { StateGraph, StateSchema, START } from "@langchain/langgraph";import * as z from "zod";// Define subgraphconst SubgraphState = new StateSchema({ // note that none of these keys are shared with the parent graph state bar: z.string(), baz: z.string(),});const subgraphBuilder = new StateGraph(SubgraphState) .addNode("subgraphNode1", (state) => { return { baz: "baz" }; }) .addNode("subgraphNode2", (state) => { return { bar: state.bar + state.baz }; }) .addEdge(START, "subgraphNode1") .addEdge("subgraphNode1", "subgraphNode2");const subgraph = subgraphBuilder.compile();// Define parent graphconst ParentState = new StateSchema({ foo: z.string(),});const builder = new StateGraph(ParentState) .addNode("node1", (state) => { return { foo: "hi! " + state.foo }; }) .addNode("node2", async (state) => { const response = await subgraph.invoke({ bar: state.foo }); return { foo: response.bar }; }) .addEdge(START, "node1") .addEdge("node1", "node2");const graph = builder.compile();for await (const chunk of await graph.stream( { foo: "foo" }, { subgraphs: true })) { console.log(chunk);}
When the parent graph and subgraph share state keys, you can pass a compiled subgraph directly to add_node. No wrapper function is needed — the subgraph reads from and writes to the parent’s state channels automatically. For example, in multi-agent systems, the agents often communicate over a shared messages key.If your subgraph shares state keys with the parent graph, you can follow these steps to add it to your graph:
Define the subgraph workflow (subgraphBuilder in the example below) and compile it
Pass compiled subgraph to the .addNode method when defining the parent graph workflow
import { StateGraph, StateSchema, START } from "@langchain/langgraph";import * as z from "zod";const State = new StateSchema({ foo: z.string(),});// Subgraphconst subgraphBuilder = new StateGraph(State) .addNode("subgraphNode1", (state) => { return { foo: "hi! " + state.foo }; }) .addEdge(START, "subgraphNode1");const subgraph = subgraphBuilder.compile();// Parent graphconst builder = new StateGraph(State) .addNode("node1", subgraph) .addEdge(START, "node1");const graph = builder.compile();
Full example: shared state schemas
import { StateGraph, StateSchema, START } from "@langchain/langgraph";import * as z from "zod";// Define subgraphconst SubgraphState = new StateSchema({ foo: z.string(), bar: z.string(), });const subgraphBuilder = new StateGraph(SubgraphState) .addNode("subgraphNode1", (state) => { return { bar: "bar" }; }) .addNode("subgraphNode2", (state) => { // note that this node is using a state key ('bar') that is only available in the subgraph // and is sending update on the shared state key ('foo') return { foo: state.foo + state.bar }; }) .addEdge(START, "subgraphNode1") .addEdge("subgraphNode1", "subgraphNode2");const subgraph = subgraphBuilder.compile();// Define parent graphconst ParentState = new StateSchema({ foo: z.string(),});const builder = new StateGraph(ParentState) .addNode("node1", (state) => { return { foo: "hi! " + state.foo }; }) .addNode("node2", subgraph) .addEdge(START, "node1") .addEdge("node1", "node2");const graph = builder.compile();for await (const chunk of await graph.stream({ foo: "foo" })) { console.log(chunk);}
This key is shared with the parent graph state
This key is private to the SubgraphState and is not visible to the parent graph
By default, subgraphs start fresh on every invocation — they have no memory of previous calls. This is the right choice for most applications, including multi-agent systems where subagents are invoked as tools.If a subagent needs to remember previous conversations (multi-turn history), you can enable stateful persistence so state accumulates across invocations on the same thread.
The parent graph must be compiled with a checkpointer for subgraph persistence features (interrupts, state inspection, stateful memory) to work. See persistence.
Both checkpointer=False and checkpointer=None produce stateless behavior — each invocation starts fresh with no memory of previous calls. The difference is whether the subgraph supports interrupts within a single invocation.
Compile with checkpointer=False to opt out of checkpointing entirely. No checkpoints are written for the subgraph. The subgraph cannot use interrupt() and its state is not inspectable via get_state. This is the lightest-weight option.
This is the recommended mode for most applications, including multi-agent systems where subagents are invoked as tools. It supports interrupts and parallel calls while keeping each invocation isolated.
The default behavior when you omit checkpointer or set it to None. The subgraph inherits the parent’s checkpointer and each invocation gets a unique checkpoint namespace. Within a single invocation, the subgraph can use interrupt() to pause and resume.The following examples use two subagents (fruit expert, veggie expert) wrapped as tools for an outer agent:
import { createAgent, tool } from "langchain";import { MemorySaver, Command, interrupt } from "@langchain/langgraph";import * as z from "zod";const fruitInfo = tool( (input) => `Info about ${input.fruitName}`, { name: "fruit_info", description: "Look up fruit info.", schema: z.object({ fruitName: z.string() }), });const veggieInfo = tool( (input) => `Info about ${input.veggieName}`, { name: "veggie_info", description: "Look up veggie info.", schema: z.object({ veggieName: z.string() }), });// Subagents — no checkpointer setting (inherits parent)const fruitAgent = createAgent({ model: "gpt-4.1-mini", tools: [fruitInfo], prompt: "You are a fruit expert. Use the fruit_info tool. Respond in one sentence.",});const veggieAgent = createAgent({ model: "gpt-4.1-mini", tools: [veggieInfo], prompt: "You are a veggie expert. Use the veggie_info tool. Respond in one sentence.",});// Wrap subagents as tools for the outer agentconst askFruitExpert = tool( async (input) => { const response = await fruitAgent.invoke({ messages: [{ role: "user", content: input.question }], }); return response.messages[response.messages.length - 1].content; }, { name: "ask_fruit_expert", description: "Ask the fruit expert. Use for ALL fruit questions.", schema: z.object({ question: z.string() }), });const askVeggieExpert = tool( async (input) => { const response = await veggieAgent.invoke({ messages: [{ role: "user", content: input.question }], }); return response.messages[response.messages.length - 1].content; }, { name: "ask_veggie_expert", description: "Ask the veggie expert. Use for ALL veggie questions.", schema: z.object({ question: z.string() }), });// Outer agent with checkpointerconst agent = createAgent({ model: "gpt-4.1-mini", tools: [askFruitExpert, askVeggieExpert], prompt: "You have two experts: ask_fruit_expert and ask_veggie_expert. " + "ALWAYS delegate questions to the appropriate expert.", checkpointer: new MemorySaver(),});
Interrupts
Multi-turn
Multiple subgraph calls
Each invocation can use interrupt() to pause and resume. Add interrupt() to a tool function to require user approval before proceeding:
const fruitInfo = tool( (input) => { interrupt("continue?"); return `Info about ${input.fruitName}`; }, { name: "fruit_info", description: "Look up fruit info.", schema: z.object({ fruitName: z.string() }), });
Compile with checkpointer=True to enable state that accumulates across invocations on the same thread. LangGraph strips task IDs from the checkpoint namespace (turning node:task_id|subnode:subtask_id into node|subnode), so the subgraph writes to the same namespace every time. The subgraph “remembers” previous calls — use this when a subagent needs multi-turn conversation history.The following examples use a single fruit expert subagent compiled with checkpointer=True:
import { createAgent, tool } from "langchain";import { MemorySaver, Command, interrupt } from "@langchain/langgraph";import * as z from "zod";const fruitInfo = tool( (input) => `Info about ${input.fruitName}`, { name: "fruit_info", description: "Look up fruit info.", schema: z.object({ fruitName: z.string() }), });// Subagent with checkpointer=true for persistent stateconst fruitAgent = createAgent({ model: "gpt-4.1-mini", tools: [fruitInfo], prompt: "You are a fruit expert. Use the fruit_info tool. Respond in one sentence.", checkpointer: true, });// Wrap subagent as a tool for the outer agentconst askFruitExpert = tool( async (input) => { const response = await fruitAgent.invoke({ messages: [{ role: "user", content: input.question }], }); return response.messages[response.messages.length - 1].content; }, { name: "ask_fruit_expert", description: "Ask the fruit expert. Use for ALL fruit questions.", schema: z.object({ question: z.string() }), });// Outer agent with checkpointerconst agent = createAgent({ model: "gpt-4.1-mini", tools: [askFruitExpert], prompt: "You have a fruit expert. ALWAYS delegate fruit questions to ask_fruit_expert.", checkpointer: new MemorySaver(),});
Interrupts
Multi-turn
Multiple subgraph calls
Stateful subagents support interrupt() just like per-invocation. Add interrupt() to a tool function to require user approval:
const fruitInfo = tool( (input) => { interrupt("continue?"); return `Info about ${input.fruitName}`; }, { name: "fruit_info", description: "Look up fruit info.", schema: z.object({ fruitName: z.string() }), });
State accumulates across invocations — the subagent remembers past conversations:
const config = { configurable: { thread_id: "1" } };// First calllet response = await agent.invoke( { messages: [{ role: "user", content: "Tell me about apples" }] }, config,);// Subagent message count: 4// Second call — subagent REMEMBERS apples conversationresponse = await agent.invoke( { messages: [{ role: "user", content: "Now tell me about bananas" }] }, config,);// Subagent message count: 8 (accumulated!)
Multiple calls to different stateful subgraphs from a single node require namespace isolation — wrap each agent in a StateGraph with a unique node name:
const config = { configurable: { thread_id: "1" } };// First call — LLM calls both fruit and veggie expertslet response = await agent.invoke( { messages: [{ role: "user", content: "Tell me about cherries and broccoli" }] }, config,);// Fruit subagent message count: 4// Veggie subagent message count: 4// Second call — both agents accumulate independentlyresponse = await agent.invoke( { messages: [{ role: "user", content: "Now tell me about oranges and carrots" }] }, config,);// Fruit subagent message count: 8 (remembers cherries!)// Veggie subagent message count: 8 (remembers broccoli!)
Multiple calls to the same subgraph with stateful persistence cause namespace conflicts — both calls read from and write to the same checkpoint, corrupting state. Use stateless with interrupts persistence instead when you need multiple calls to the same subgraph.
Namespace isolation for parallel stateful subgraphs
When multiple subgraphs are called inside a node with checkpointer=True, LangGraph assigns each invocation a position-based namespace suffix. The first invocation gets the base namespace (e.g., calling_node), the second gets calling_node|1, and so on. State persists per call position — if you reorder calls, each subgraph may load the wrong state.Subgraphs added as nodes do not have this limitation because each node has a unique name that becomes part of the namespace.
For stable, name-based namespaces, wrap each subgraph in its own StateGraph with a unique node name. Each wrapper must be a different compiled subgraph instance — you cannot invoke the same instance twice in a single node.
Control subgraph persistence with the checkpointer parameter on .compile():
const subgraph = builder.compile({ checkpointer: false }); // or true, or null
Feature
Without interrupts
With interrupts (default)
Stateful
checkpointer=
False
None
True
Interrupts (HITL)
❌
✅
✅
Multi-turn memory
❌
❌
✅
Multiple calls (different subgraphs)
✅
✅
Multiple calls (same subgraph)
✅
✅
❌
State inspection
❌
✅
Interrupts (HITL): The subgraph can use interrupt() to pause execution and wait for user input, then resume where it left off.
Multi-turn memory: The subgraph retains its state across multiple invocations within the same thread. Each call picks up where the last one left off rather than starting fresh.
Multiple calls (different subgraphs): Multiple different subgraph instances can be invoked within a single node without checkpoint namespace conflicts.
Multiple calls (same subgraph): The same subgraph instance can be invoked multiple times within a single node. With stateful persistence, these calls write to the same checkpoint namespace and conflict — use per-invocation persistence instead.
State inspection: The subgraph’s state is available via get_state(config, subgraphs=True) for debugging and monitoring.
When you enable persistence, you can inspect the subgraph state using the subgraphs option. With checkpointer=False, no subgraph checkpoints are saved, so subgraph state is not available.
Viewing subgraph state requires that LangGraph can statically discover the subgraph — i.e., it is added as a node or called inside a node. It does not work when a subgraph is called inside a tool function or other indirection (e.g., the subagents pattern). Interrupts still propagate to the top-level graph regardless of nesting.
Stateless
Stateful
Returns subgraph state for the current invocation only. Each invocation starts fresh.
import { StateGraph, StateSchema, START, MemorySaver, interrupt, Command } from "@langchain/langgraph";import * as z from "zod";const State = new StateSchema({ foo: z.string(),});// Subgraphconst subgraphBuilder = new StateGraph(State) .addNode("subgraphNode1", (state) => { const value = interrupt("Provide value:"); return { foo: state.foo + value }; }) .addEdge(START, "subgraphNode1");const subgraph = subgraphBuilder.compile(); // inherits parent checkpointer// Parent graphconst builder = new StateGraph(State) .addNode("node1", subgraph) .addEdge(START, "node1");const checkpointer = new MemorySaver();const graph = builder.compile({ checkpointer });const config = { configurable: { thread_id: "1" } };await graph.invoke({ foo: "" }, config);// View subgraph state for the current invocationconst subgraphState = (await graph.getState(config, { subgraphs: true })).tasks[0].state; // Resume the subgraphawait graph.invoke(new Command({ resume: "bar" }), config);
Returns accumulated subgraph state across all invocations on this thread.
import { StateGraph, StateSchema, MessagesValue, START, MemorySaver } from "@langchain/langgraph";// Subgraph with its own persistent stateconst SubgraphState = new StateSchema({ messages: MessagesValue,});const subgraphBuilder = new StateGraph(SubgraphState);// ... add nodes and edgesconst subgraph = subgraphBuilder.compile({ checkpointer: true }); // Parent graphconst builder = new StateGraph(SubgraphState) .addNode("agent", subgraph) .addEdge(START, "agent");const checkpointer = new MemorySaver();const graph = builder.compile({ checkpointer });const config = { configurable: { thread_id: "1" } };await graph.invoke({ messages: [{ role: "user", content: "hi" }] }, config);await graph.invoke({ messages: [{ role: "user", content: "what did I say?" }] }, config);// View accumulated subgraph state (includes messages from both invocations)const subgraphState = (await graph.getState(config, { subgraphs: true })).tasks[0].state;
To include outputs from subgraphs in the streamed outputs, you can set the subgraphs option in the stream method of the parent graph. This will stream outputs from both the parent graph and any subgraphs.
for await (const chunk of await graph.stream( { foo: "foo" }, { subgraphs: true, streamMode: "updates", })) { console.log(chunk);}
Set subgraphs: true to stream outputs from subgraphs.
Stream from subgraphs
import { StateGraph, StateSchema, START } from "@langchain/langgraph";import * as z from "zod";// Define subgraphconst SubgraphState = new StateSchema({ foo: z.string(), bar: z.string(),});const subgraphBuilder = new StateGraph(SubgraphState) .addNode("subgraphNode1", (state) => { return { bar: "bar" }; }) .addNode("subgraphNode2", (state) => { // note that this node is using a state key ('bar') that is only available in the subgraph // and is sending update on the shared state key ('foo') return { foo: state.foo + state.bar }; }) .addEdge(START, "subgraphNode1") .addEdge("subgraphNode1", "subgraphNode2");const subgraph = subgraphBuilder.compile();// Define parent graphconst ParentState = new StateSchema({ foo: z.string(),});const builder = new StateGraph(ParentState) .addNode("node1", (state) => { return { foo: "hi! " + state.foo }; }) .addNode("node2", subgraph) .addEdge(START, "node1") .addEdge("node1", "node2");const graph = builder.compile();for await (const chunk of await graph.stream( { foo: "foo" }, { streamMode: "updates", subgraphs: true, })) { console.log(chunk);}
Set subgraphs: true to stream outputs from subgraphs.