supervisor

fun supervisor(model: ChatModel, agents: List<TeamAgent>, system: String? = null, tokenBudget: Int? = null, retry: Retry? = null, shorten: (List<ChatMessage>) -> List<ChatMessage>? = null): CompiledGraph<TeamState>

Returns a graph that is a team of agents with a supervisor: model reads the task, gives each part to the agent that fits it, reads what comes back, and answers when the task is done.

val team = supervisor(
model = opus,
system = "You answer the customers of a pizza shop.",
agents = listOf(
TeamAgent("orders", "Knows where an order is.", haiku, tools = listOf(orderStatus)),
TeamAgent("menu", "Knows the dishes and their prices.", haiku, tools = listOf(menuPrice)),
),
)

val state = team.invoke(TeamState("Where is my pizza, and what does a calzone cost?")).state
println(state.answer)

The supervisor gives a task with a tool call. An agent sees only that task, in a conversation of its own, and the supervisor sees only the agent's answer, so what the agents read stays small. Each task starts a new conversation: an agent does not remember its earlier tasks. The conversation is in TeamState.assignments, for the code that reads it.

Tasks of one answer for different agents run at the same time. Tasks for the same agent run one after the other.

Each agent is a subgraph with its TeamAgent.name, with the nodes model and tools of a toolLoop and a last node finish. So a run pauses and continues inside an agent as in any subgraph, and a checkpointer holds the agent's work:

// Approve the tool calls of the agent "orders" by hand.
val config = GraphConfig("ticket-7", checkpointer, interruptBefore = setOf("orders/tools"))

A failure of an agent's model fails the run, and resume continues inside the agent. The steps of the agent count on their own against GraphConfig.maxIterations, for each task.

In a stream, the events of an agent arrive inside a GraphEvent.SubgraphEvent that names it. Its text is in textDelta too, so check for that event to show only the supervisor's answer.

TeamState.tokens counts the supervisor, and each agent when it finishes. Assignment.conversation has the tokens of one task. An AgentObserver is told about every call, and for an agent the parent of the call's run is the node of the agent.

Parameters

model

the model of the supervisor. It should be good at planning: it decides who does what.

agents

the team.

system

instructions for the supervisor.

tokenBudget

the tokens the whole team may use. The supervisor is not asked again once the count has reached it, and the run ends without an answer. An agent gets what is left when it starts, so tasks that run at the same time can together go above it.

retry

how the supervisor's model is asked again when a call to it fails.

shorten

returns the part of the conversation the supervisor reads, such as keepLast.

Throws

if agents is empty, two agents have the same name, an agent is named supervisor, a name cannot be part of a tool name, or tokenBudget is less than 1.