Lesson 49.3 · 3 of 5 · 4 min read · Reviewed 24 Nov 2026
Pipelines and handoffs
By the end, you can
- Describe pipelines and handoffs.
- Design clean handoff messages.
Pipelines
[ intake ] -> [ classify ] -> [ extract fields ] -> [ draft reply ] -> [ check policy ]
Each stage has one job, its own prompt and tests. This is prompt chainingPrompt chaining: Splitting a task into steps where each model call handles one step and passes its output to the next. (41.1) with agentsAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. as stages. It’s predictable, cheap to debug and easy to evaluate stage by stage.
Handoffs
A handoffEscalation: Handing a case from an agent to a person, with the context needed to continue. passes control: the receiving agent now owns the conversation.
triage agent: "This is a billing dispute."
-> handoff(to="billing", payload={...})
billing agent continues with the customer
A good handoff payload
{
"goal": "Resolve a disputed charge on invoice 881",
"customer_id": "42",
"verified": true,
"facts": ["Charged twice on 3 Nov", "Card ending 4411"],
"done": ["Identity verified", "Duplicate charge confirmed in ledger"],
"next": "Decide refund eligibility and tell the customer",
"constraints": ["Refunds over 500 need a human"]
}
Pitfalls
- Lost context: the user repeats themselves. Fix with a structured payload.
- Ping-pong: agents hand back and forth. Limit handoffs and define who owns which cases.
- Unclear ownership: two agents reply at once. Only one agent owns the conversation at a time.
Across organisations, protocols like A2AAgent-to-agent protocol (A2A): An open protocol for agents built by different vendors to discover each other and exchange tasks. (chapter 38) standardise handoffs between agents built by different teams.
Key points
- A pipeline passes work through fixed stages, each handled by a specialised agent or prompt.
- A handoff transfers the conversation or task to another agent, which takes over completely.
- Pipelines are predictable and easy to test stage by stage.
- Handoffs need a clear payload: the goal, what's done, the key facts and what's expected next.
Gate a pipeline and write a handoff
Stop bad data between pipeline stages and hand held items over with a complete payload.
You will have: An extract, check, post pipeline with a validation gate and a JSON handoff for held invoices.
Steps
- Run the starter: all three invoices are posted, including the bad one.
- Make check() compare the sum of the lines with the total.
- Write handoff() with goal, done, facts and next.
- Run it and read the handoff as if you had never seen the pipeline.
Done when
- 2 invoices are posted and 1 is held.
- The handoff states the mismatch with both numbers.
- It has all four fields and says what to do next.
Open in the Playground No API key needed.
Starter code (Python)
import json
# A three-stage invoice pipeline. The check stage stops bad data; held items get a clear handoff.
INVOICES = [
{"id": "INV-1", "lines": [120, 80], "total": 200},
{"id": "INV-2", "lines": [50, 50, 25], "total": 150}, # total does not match the lines
{"id": "INV-3", "lines": [300], "total": 300},
]
def extract(inv):
return dict(inv) # simulated: fields already read from the PDF
def check(inv):
# TODO: return (True, "") if the lines add up to the total, else (False, "<what is wrong>")
return True, ""
def post(inv, ledger):
ledger.append(inv["id"])
def handoff(inv, problem):
# TODO: return goal, done, facts and next, so a person can act without opening the pipeline
return {"goal": "post invoice"}
ledger, held = [], []
for raw in INVOICES:
inv = extract(raw)
ok, problem = check(inv)
if ok:
post(inv, ledger)
else:
held.append(handoff(inv, problem))
print(f"posted: {len(ledger)}, held: {len(held)}")
for h in held:
print(json.dumps(h))
Show a solution
import json
# A three-stage invoice pipeline. The check stage stops bad data; held items get a clear handoff.
INVOICES = [
{"id": "INV-1", "lines": [120, 80], "total": 200},
{"id": "INV-2", "lines": [50, 50, 25], "total": 150}, # total does not match the lines
{"id": "INV-3", "lines": [300], "total": 300},
]
def extract(inv):
return dict(inv) # simulated: fields already read from the PDF
def check(inv):
s = sum(inv["lines"])
if s != inv["total"]:
return False, f"lines add up to {s} but the total says {inv['total']}"
return True, ""
def post(inv, ledger):
ledger.append(inv["id"])
def handoff(inv, problem):
return {
"goal": f"Post invoice {inv['id']} to the ledger",
"done": ["extracted fields", "checked lines against total"],
"facts": {"lines": inv["lines"], "total": inv["total"], "problem": problem},
"next": "Confirm the correct total with the supplier, then re-run the check",
}
ledger, held = [], []
for raw in INVOICES:
inv = extract(raw)
ok, problem = check(inv)
if ok:
post(inv, ledger)
else:
held.append(handoff(inv, problem))
print(f"posted: {len(ledger)}, held: {len(held)}")
for h in held:
print(json.dumps(h))