Multi-agent
One agent with a long list of tools and a long prompt does many jobs badly. Freyja offers two ways to split the work across agents: an agent that another agent calls as a tool, and a team of agents that pass one conversation between them. This guide covers both and how to choose.
An agent as a tool
Agent::into_tool(name, description) turns an agent into an AgentTool, a tool another agent can call. The inner agent has its own system prompt, its own tools and its own model.
let researcher = Agent::new(client.clone())
.system("You research one question and report what you found.")
.tool(search)
.token_budget(5_000)
.into_tool("research", "Researches a question and returns the findings");
let agent = Agent::new(client)
.tool(researcher)
.token_budget(20_000);
let run = agent.ask("What changed in the last Rust release?").await?;
println!("{} ({} tokens)", run.answer, run.usage.total_tokens);
This example needs a key and network.
The task argument
The calling model sees a tool with one required string argument, task, whose description asks for what to do with everything needed to do it. The description you pass to into_tool is what the model reads when deciding whether to call it, so write it as you would any tool description.
A fresh run each call
Each call starts the inner agent with no history. It sees nothing of the outer conversation and remembers nothing between calls, so the task must carry all the context. Only the inner agent's final answer goes back to the calling model.
If the inner run ends any other way than answering, or fails with an error, the calling model reads an error: result and its own run carries on. That includes an inner run that pauses on Decision::Ask.
Tokens
The inner run's tokens are added to the outer run's usage through call_metered, and they count against the outer token_budget. The outer agent checks its budget after each of its own model calls and again after a turn's tools, so it makes no further model call once over. It does not cut an inner run short, so the total can pass the budget by whatever the inner runs of that turn spend, and several calls in one turn run at the same time. Give the inner agent its own token_budget to bound each one. An inner run that fails with an error is not counted. See Observability.
To cap the whole request instead, put a shared Budget in the context. It is charged by the outer agent and every helper, so one limit covers all of them. See A budget for a whole request.
Guard scope
The outer guard and hooks see only the one call to the agent tool, never the inner agent's tool calls. To guard those, set a guard on the inner agent. Each side is checked on its own terms.
The outer run's Context reaches the inner agent's tools, so a user id set once is available at every level.
A team
Team is a set of named agents that pass one conversation between them. Where into_tool lends work out and takes the result back, a handoff gives the conversation itself to the next agent, which answers the user directly.
use freyja::{Agent, Team};
let desk = Agent::new(client.clone()).system("You greet people and route them.");
let billing = Agent::new(client).system("You handle invoices and refunds.").tool(refund);
let team = Team::new()
.agent("desk", "Greets people and answers general questions", desk)
.agent("billing", "Handles invoices, charges and refunds", billing);
let mut chat = team.conversation("desk", storage);
let run = chat.send("I was charged twice").await?;
println!("{}: {}", run.agent.unwrap_or_default(), run.answer);
storage is any Storage backend, such as InMemoryStorage::new(). This example needs a key and network.
Transfer tools
Each member gets one tool per other member, named transfer_to_<name>, that takes no arguments. The description you gave Team::agent is what the other members read when deciding whether to hand over, so describe when the member is the right one. Names may hold only letters, digits, _ and -. Team::agent panics on an empty, invalid or repeated name, and Team::conversation panics when the start is not a member.
The answer comes in the same send
When a member calls a transfer tool, the turn's other tool calls still run. The named member then continues the same send on the full history, under its own system prompt and its own tools. The user gets one Run back, and run.agent names the member that gave the final answer. Outside a team run.agent is None.
The new member sees every message so far, including the earlier member's tool calls and results, even for tools it does not have itself. OpenAI, Anthropic and Gemini were each checked to accept a history that holds calls to tools the new agent lacks. run.usage and run.turns sum every member that ran in the send. A token_budget belongs to each member's own agent and applies to its own stretch of the send, which starts from zero, so the sum can pass any one member's budget. A shared Budget in the context is charged by every member and caps the whole send, see A budget for a whole request.
Restarts
Later messages go to the member last handed to. Freyja reads that from the stored transcript, so a conversation reopened on the same storage after a restart carries on with the same member. A transfer counts only when its call has the result transferred to <name>.
Guards on transfers
A guard sees a transfer like any other call. Decision::Deny keeps the conversation with the current member, which reads the denial and carries on. Decision::Ask pauses the run before any tool of that turn runs, and resume does the handoff once the call is allowed. See Human approval.
Watching handoffs
A streamed send reports each handoff as AgentEvent::Handoff { from, to } just before the new member starts.
use freyja::{AgentEvent, StreamEvent};
let run = chat
.send("I was charged twice")
.stream(|event| match event {
AgentEvent::Handoff { from, to } => println!("[{from} -> {to}]"),
AgentEvent::Model(StreamEvent::TextDelta(text)) => print!("{text}"),
_ => {}
})
.await?;
The handoff limit
One send makes at most three handoffs, which stops two members from passing a conversation back and forth. If the last member also asks to hand over, the run ends with StopReason::HandedOff(name) and run.agent is the member that asked. The next message goes to name.
Choosing between them
| Agent as a tool | Team | |
|---|---|---|
| Who answers the user | The calling agent | The member handed to |
| What the specialist sees | Only the task string | The whole conversation |
| History between calls | None | One shared transcript |
| Control returns to caller | Yes, with the answer | No, the conversation moves on |
| Guard on the specialist's tools | Set on the inner agent | Each member's own guard |
| Good for | A bounded job with a clear result, such as research, a lookup or a summary | A conversation that changes owner, such as front desk to billing to support |
Reach for an agent tool when the caller should stay in charge and use the answer as one input among others. Reach for a team when the user should end up talking to a different specialist who needs the context already gathered. The two combine: a team member can itself use agent tools.
See also
- Human approval for
Askon transfers and on inner agents - Observability for usage across agents and the
runspan - Hooks for what a hook sees in each setup
- Agents for the single-agent loop and
Run - Conversations for storage and streaming
- Testing for scripting a team
- Reporting tokens for
call_metered