handoffTeam

fun handoffTeam(agents: List<TeamAgent>, tokenBudget: Int? = null): CompiledGraph<TeamState>

Returns a graph that is a team of agents who hand the conversation to each other: the first of agents answers, and each agent can end its turn by handing the conversation to another, which goes on from there.

val team = handoffTeam(
listOf(
TeamAgent("reception", "Greets the customer.", haiku, system = "Find out what the customer needs."),
TeamAgent("billing", "Handles invoices and refunds.", haiku, tools = listOf(refund), system = "You handle invoices."),
TeamAgent("orders", "Knows where an order is.", haiku, tools = listOf(orderStatus), system = "You track orders."),
),
)

val state = team.invoke(TeamState("I want my money back for order 7.")).state
println("${state.agent}: ${state.answer}")

// The next message goes to the agent that has the conversation.
val next = team.invoke(state.withUserMessage("How long will it take?")).state

Each agent gets a tool transfer_to_<name> for every other agent. All agents share one conversation, so unlike with a supervisor, the agent that takes over reads all that was said and done so far, under its own instructions. The run ends when an agent answers without handing over. TeamState.agent says who has the conversation.

A hand-off changes the conversation in the state, because a model takes the turns of another agent for its own: the tool calls and their results of the agent that hands over become messages of the user that say who did what, in brackets, and the hand-off itself becomes one. It also removes ChatMessage.Assistant.providerContent, such as Claude's thinking, which a provider ties to the instructions and tools it was written under. The texts of the agents stay.

Each agent is a subgraph with its TeamAgent.name, with the nodes model and tools of a toolLoop and a last node handover, so interruptBefore = setOf("billing/tools") approves its tool calls by hand. Two agents that keep handing the conversation to each other end at GraphConfig.maxIterations, or at a tokenBudget.

Parameters

agents

the team. The first one starts the conversation.

tokenBudget

the tokens the whole team may use. It works as in toolAgent.

Throws

if agents has fewer than two agents, two agents have the same name, a name cannot be part of a tool name, or tokenBudget is less than 1.