Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

AI Agents · Const

Agent handoffs

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.

How it works

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.

Syntax

new Command({ goto: "agent_name", update: { messages: [...] } })

Returns

A Command that updates state and routes execution to a new step or agent.

Key points

  • Handoffs let an agent alter its behavior according to state that persists across turns.
  • A tool can change a state variable such as the active agent or current step.
  • Returning Command from a handoff tool triggers movement to another step or agent.
  • The middleware pattern keeps one agent while adapting its configuration dynamically.
  • The subgraph pattern represents distinct agents as separate graph nodes.
  • Graph-based handoff tools use Command.PARENT to select the next agent node.

Common mistakes

  • When a handoff tool updates messages, include a matching tool response so the model's tool-call history remains valid.
  • A subgraph handoff must preserve the initiating AI message together with an acknowledging ToolMessage.
  • With multiple subgraphs, message propagation is your responsibility rather than automatic.

Example: Handoff tool

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.

When to use it

Dynamic single-agent behavior

A single conversational agent must switch prompts and tools as the workflow progresses.

Specialist-agent routing

Separate specialist agents need to transfer control through a graph.

Selective message propagation

A workflow needs tightly controlled inter-agent context.

Interview questions

QWhat is the core mechanism behind an agent handoff?

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.

QHow do single-agent and multi-agent handoffs differ?

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.

QWhat must be preserved during a subgraph handoff?

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.

  • Command
  • Command.PARENT
  • single agent with middleware
  • multiple agent subgraphs
  • ToolMessage
  • AIMessage

Source: Handoffs. Checked 2026-10-15.