Dynamic single-agent behavior
A single conversational agent must switch prompts and tools as the workflow progresses.
AI Agents · Const
Agent handoffs use tools to update state and dynamically route behavior to another step or agent. The documentation presents middleware-based handoffs within one agent and subgraph-based handoffs between distinct agents.
Agent handoffs use tools to update persistent state, allowing behavior or routing to change across turns. LangChain documents both middleware-based handoffs within one agent and graph-based handoffs between distinct agent nodes.
new Command({ goto: "agent_name", update: { messages: [...] } })A Command that updates state and routes execution to a new step or agent.
import { createAgent, tool } from "langchain";
import { Command } from "@langchain/langgraph";
import { z } from "zod";
const transferToSupport = tool(
() => new Command({
goto: "support",
update: {
messages: [{ role: "tool", content: "Transferred to support", tool_call_id: "handoff" }],
},
}),
{
name: "transfer_to_support",
description: "Transfer the conversation to the support agent",
schema: z.object({}),
},
);
const agent = createAgent({
model: "openai:gpt-4.1-mini",
tools: [transferToSupport],
});
await agent.invoke({ messages: [{ role: "user", content: "I need support" }] });This illustrates a handoff tool returning Command to update state and route execution to another agent.
A single conversational agent must switch prompts and tools as the workflow progresses.
Separate specialist agents need to transfer control through a graph.
A workflow needs tightly controlled inter-agent context.
A handoff is driven by a tool that returns Command, which updates state and causes a transition to another step or agent. The state can also dynamically change one agent's prompt and available tools.
Follow-up: When would you choose subgraphs? Use them when agents need bespoke implementations, such as complex graph-based retrieval or reflection workflows.
Use one agent with middleware for most handoff scenarios because it is simpler and message history flows naturally. Use multiple agent subgraphs when distinct graph nodes require independent implementations.
Follow-up: What is the tradeoff of subgraphs? They provide precise control over inter-agent messages, but require explicit history management.
A handoff tool can route to another graph node with Command.PARENT and must preserve valid conversation history. Include the AI tool-call message and a ToolMessage acknowledging that call.
Follow-up: Why does the acknowledgement matter? Without the matching tool response, the request-response sequence is malformed for the LLM.
CommandCommand.PARENTsingle agent with middlewaremultiple agent subgraphsToolMessageAIMessage