Dynamic single-agent configuration
A single assistant needs to change its system prompt and available tools as the conversation moves between stages.
AI Agents · Function
Handoffs let tools change state so an agent can dynamically reconfigure itself or transfer control to another agent. They can be implemented with middleware for one agent or with separate agent subgraphs.
A handoff is a state-driven transfer of control. A tool changes a persistent state value and returns a Command, allowing middleware to reconfigure one agent or route execution among separate agent nodes; when messages are updated during a cross-agent handoff, the tool call and its acknowledging ToolMessage must both remain in the history.
tool(async () => new Command({ goto: "agent_name", update: { active_agent: "agent_name" } }), options)A Command that updates graph state and triggers a transition to another step or agent.
import { createAgent } from "langchain";
import { tool } from "langchain";
import { Command } from "@langchain/langgraph";
import { z } from "zod";
const handoff = tool(
async () =>
new Command({
goto: "specialist",
update: { active_agent: "specialist" },
}),
{
name: "transfer_to_specialist",
description: "Transfer the conversation to the specialist agent.",
schema: z.object({}),
},
);
const agent = createAgent({
model: "openai:gpt-4.1-mini",
tools: [handoff],
});
const result = await agent.invoke({
messages: [{ role: "user", content: "I need specialist help." }],
});
console.log(result.messages.at(-1)?.content);This illustrates the documented mechanism: a tool returns a Command that updates state and routes execution to another step or agent.
A single assistant needs to change its system prompt and available tools as the conversation moves between stages.
Different specialists should own distinct graph nodes and receive control through explicit routing.
A workflow needs to retain the active stage across turns and use it to select subsequent behavior.
A handoff uses a tool to update state and return a Command. The updated state can change an agent’s configuration or route execution to another agent node.
Follow-up: How would you preserve history across separate agents? Include the AIMessage containing the handoff tool call and a matching ToolMessage acknowledging it.
Use middleware when one agent should dynamically change its prompt or tools based on state. Use multiple agent subgraphs when distinct agents should exist as separate graph nodes.
Follow-up: What is the main context-management difference? Middleware preserves the normal message flow, while separate agents require an explicit choice of messages passed between them.
A handoff tool returns a Command that changes state and triggers a transition. For agent-node routing, the handoff uses Command.PARENT to identify the next node in the parent graph.
Follow-up: What history invariant must hold after the transfer? Every model tool call must have its corresponding tool response, including an artificial response for the handoff.
CommandCommand.PARENTToolMessageAIMessagesingle agent with middlewaremultiple agent subgraphsactive_agentcurrent_step