Sub topic 7
What to pass between agents
This chapter covers the context that moves from one agent to another when work is delegated. It assumes you can read basic Python, know how chat messages and JSON work, and have seen the handoff and agent-as-tool patterns from earlier chapters.
Why context is a design decision
Every time one agent delegates to another, you choose what the receiving agent will see. That choice affects four things:
- Correctness. If the receiver lacks a fact, it will guess or ask the user again.
- Cost. Every token you pass is billed again on the next model call.
- Focus. A long, irrelevant history distracts the receiver and can pull it off task.
- Safety. Whatever you pass may include data the receiver should not have, or text that tries to steer it.
The two core patterns default to different answers. In a handoff, the new agent usually continues the same message history. In the agent-as-tool pattern, the worker starts fresh and sees only the task string. Neither default is always right, so you need to decide on purpose.
Three things you can pass
| Option | What the receiver sees | Typical use |
|---|---|---|
| Full history | Every earlier message, tool call, and tool result | Handoffs where the user expects continuity |
| Task text only | One string written by the delegating agent | Orchestrator calling a narrow specialist |
| Structured payload | A small object with named fields | Either pattern, when you need control and auditability |
You can also combine them. A common mix is a structured payload plus the last few user messages.
Option 1: pass the full history
The handoff loop from the earlier chapter does this. Only the active agent changes. The messages list keeps growing and the new agent reads all of it.
agent = agent.handoffs[reply["target"]]
continue # same `messages` list is used on the next turnFull history is simple and loses nothing. It has these costs:
- Token use grows with every turn, and each later call pays for the whole history.
- The receiver sees the previous agent's reasoning, tool outputs, and any internal notes. Some of that may be confusing or sensitive.
- The receiver's instructions may conflict with the style or commitments in earlier messages. For example, a sales agent may have promised something that a support agent is not allowed to promise.
Use full history when the user is in a single continuous conversation and the specialists share the same trust level.
Option 2: pass only the task text
In the orchestrator example, each worker starts with a history that contains one user message: the task.
def tool(task: str) -> str:
_, text = run(worker, [{"role": "user", "content": task}], call_model)
return textThis keeps the worker cheap and focused. It also moves a burden onto the orchestrator. The orchestrator must write a task that stands on its own. A worker cannot ask "which order did the customer mean?" because it has no access to the earlier conversation.
Compare these two tasks:
- Weak:
"Check the refund." - Strong:
"Check whether order 1042 (delivered on time, item reported damaged) qualifies for a refund under the standard policy. Return a yes or no and one sentence of reasoning."
The strong version names the order, the known facts, the question, and the expected output shape. A good rule: if a human contractor received only this text, they should be able to start work.
Option 3: pass a structured payload
A structured payload is a small object with fixed fields. It gives the receiver a summary without the whole history. It also gives you something you can log, validate, and test.
from dataclasses import dataclass, asdict
import json
@dataclass
class HandoffPayload:
goal: str
facts: list[str]
open_questions: list[str]
constraints: list[str]
payload = HandoffPayload(
goal="Issue a refund for order 1042",
facts=["Customer reports a damaged item", "Order was delivered on time"],
open_questions=["Does the refund exceed the auto-approval limit?"],
constraints=["Do not promise a replacement"],
)
message = {"role": "user", "content": json.dumps(asdict(payload))}Each field has a job:
goalsays what the receiver must accomplish. Without it, the receiver may restate the problem or drift.factslists what is already established, so the receiver does not repeat work or ask again.open_questionslists what is still unknown, so the receiver knows where its effort should go.constraintslists limits that must carry over, such as promises not to make or actions not allowed.
The payload is only as good as whoever fills it in. If the delegating agent is a model, it can omit a fact or invent one. Treat the payload as a summary written by an unreliable author, and fill in the fields you can from your own code.
Pass the facts, not the opinions
When summarizing, separate what is known from what was inferred. "Customer says the item arrived damaged" is a fact about a claim. "The customer is lying" is a conclusion the receiver should reach on its own, if at all. Mixing the two makes errors spread. One agent's guess becomes the next agent's premise.
A useful habit is to record where a fact came from:
facts = [
{"claim": "Order delivered on 2026-11-20", "source": "orders_db"},
{"claim": "Item arrived damaged", "source": "customer_message"},
]The receiver can then weigh a database lookup differently from a customer's statement.
State that is not a message
Some information should not go through the model at all. Examples are a customer ID, a session token, or an account tier. If you put these in a message, the model has to copy them correctly, and it can make mistakes or leak them.
A better approach is to keep this state in a separate object that your code owns, and hand it to tools directly.
from dataclasses import dataclass
@dataclass
class RunContext:
customer_id: str
auth_level: str
def issue_refund(ctx: RunContext, order_id: str, amount: float) -> str:
if ctx.auth_level != "support":
return "denied: insufficient permissions"
return f"refunded {amount:.2f} on {order_id} for {ctx.customer_id}"
ctx = RunContext(customer_id="c-77", auth_level="support")
print(issue_refund(ctx, "1042", 19.99))Here the model chooses order_id and amount. Your code supplies ctx. The model never sees the permission level and cannot change it by writing text.
Common errors
- Passing everything "to be safe." Costs grow and the receiver gets distracted. Start with the smallest context that works and add fields when tests show a gap.
- Passing too little. The receiver asks the user to repeat themselves, or acts on a wrong guess. Look for repeated questions in your traces.
- Vague task strings. The orchestrator writes "handle this" and the worker has no way to know what "this" means.
- Dropping constraints. A promise or limit lives in the history, but the summary leaves it out. Put constraints in their own field so they are hard to forget.
- Trusting forwarded text. A tool result or user message may contain instructions. If you forward it unchanged, the next agent may follow them. Label forwarded content as data, and pass only what the receiver needs.
- Letting the model carry identity. Put IDs and permissions in code-owned state, not in prose.
Choosing what to pass
Use these questions in order:
- Does the receiver need to continue a conversation with the user? If yes, pass the history, possibly trimmed to the recent turns.
- Is the receiver a narrow worker with a single job? If yes, pass a self-contained task string.
- Do you need to log, validate, or test the hand-off? If yes, use a structured payload.
- Is any of the data secret or tied to permissions? If yes, keep it in code-owned state.
- Does the receiver share the sender's trust level? If not, pass less.
Exercises
Exercise
An orchestrator calls a write worker with the task "Write the summary." The worker returns a generic paragraph unrelated to the user's request. What went wrong, and how do you fix it?
Show answer
The worker starts with a fresh history containing only the task, so it has no idea what to summarize. The orchestrator must write a self-contained task that includes the subject, the source facts (or the research result), the audience, and the expected output format. Better still, pass a structured payload with a goal, facts, and constraints.
Exercise
A support agent hands off to a billing agent. The billing tool needs the customer's account ID and the caller's permission level. Where should these values live, and why?
Show answer
In a code-owned context object passed directly to the tools, not in the messages. The model can copy IDs wrongly, leak them, or be talked into changing them by text. If code supplies them, the tool checks permissions regardless of what any message says.
Exercise
Name two reasons to prefer a structured payload over forwarding the full history in a handoff.
Show answer
Any two of these: lower token cost on later calls; the receiver sees only relevant facts and stays focused; the fields can be validated and logged; constraints are carried explicitly; sensitive or untrusted content in the history is not forwarded automatically.