The Core Idea: Orchestration is the control layer of a multi-agent system. It routes each request to the right agent, delegates and hands off work, coordinates results, protects shared state from conflicts, and ties it together in a workflow.
- Beginner: Routing
Routing decides which agent should handle a request. A good router keeps each specialist focused and avoids waking up the wrong, expensive one.
| Routing method | How it works | Trade-off |
|---|---|---|
| Rules and keywords | Match words to agent skills | Fast and cheap, but brittle |
| Classifier or small model | A cheap model picks the agent | Flexible, small cost |
| LLM router | A language model reads and decides | Most flexible, slowest |
| Embedding match | Compare the request with agent descriptions | Good for many agents |
In the code: A keyword router sends invoice charged twice to billing, error at login to technical, and a request that matches nothing to a general agent.
- Beginner: Delegation and Handoffs
Delegation means one agent gives a task to another. A handoff transfers the whole conversation to a different agent. Both need clear instructions, context and an expected result.
| Good delegation includes | Why |
|---|---|
| The goal in one sentence | The receiver knows what to do |
| Only the needed context | Saves tokens and avoids confusion |
| The expected output format | Easy to merge |
| Limits | Time, budget, allowed tools |
Common mistake: Handing over the whole conversation every time. Pass a short, precise brief instead.
- Medium: Coordination
Coordination makes sure the pieces fit: that tasks are not duplicated, results are merged and the final answer is consistent.
| Coordination job | Example |
|---|---|
| Avoid duplicate work | Two agents should not research the same question |
| Order dependent tasks | Analysis waits for the data |
| Merge results | Combine three summaries into one |
| Resolve conflicts | Two agents disagree: ask a judge or a human |
| Track progress | Know which tasks are done, running or failed |
Tip: A task board (a list of tasks with status and owner) is a simple and effective coordination tool.
- Medium: Managing Shared State
When several agents read and write the same data, they can overwrite each other. A safe approach is optimistic concurrency: each write names the version it read, and the write fails if someone else changed the data first.
In the code: Agents A and B both read version 1. A writes first, so the version becomes 2. B's write, still based on version 1, is rejected. B reads again and its second write succeeds. Both notes survive.
| Technique | Use |
|---|---|
| Version checks | Detect conflicting writes |
| Append-only logs | Never overwrite, only add |
| Ownership | Only one agent may write each field |
| Locks | Block others while writing (use sparingly) |
- Advanced: Multi-Agent Workflows
A complete multi-agent workflow combines everything: a router picks the specialist, the specialist works (using tools and shared state), results are validated, and a coordinator returns the final result.
Do not forget: Add step limits, timeouts and escalation to a human at the orchestration level too. A system of agents can loop just like a single agent.
- Visual Explanation: Routing and a Shared-State Conflict
The figure shows how the router in the code sends four requests to different agents, then walks through a write conflict on shared state.
Click to see why we do not simply overwriteCompare ▼
Part 1 shows each request with its destination. Part 2 follows the two agents through the conflict. Open the bar to see why silent overwrites are dangerous.
- Python Implementation: A Router, a Version-Checked State and a Workflow
This script builds a keyword router for three specialist agents with a general fallback, shows two agents writing to shared state with version checks (one write is rejected and retried), and runs a small coordinator workflow.
# Routing, delegation, coordination and shared stateSKILLS = { "billing": {"billing", "invoice", "refund", "charge", "payment"}, "technical": {"error", "crash", "bug", "login", "password"}, "sales": {"price", "buy", "plan", "upgrade", "discount"},} def route(question): words = set(question.lower().replace("?", "").replace(",", "").split()) scores = {agent: len(words & vocabulary) for agent, vocabulary in SKILLS.items()} best = max(scores, key=scores.get) return best if scores[best] > 0 else "general" for q in ["Why was my invoice charged twice?", "The app shows an error at login", "How much is the upgrade plan?", "Tell me a joke"]: print(q.ljust(36), "-> routed to:", route(q)) # Shared state with optimistic concurrency: a write must name the version it readclass SharedState: def __init__(self): self.version, self.data = 1, {"notes": []} def update(self, expected_version, note): if expected_version != self.version: return False # someone else changed it first self.data["notes"] = self.data["notes"] + [note] self.version += 1 return True state = SharedState()seen_by_a = seen_by_b = state.version # both agents read version 1print("Agent A writes:", state.update(seen_by_a, "A: found the invoice"), "-> version", state.version)print("Agent B writes:", state.update(seen_by_b, "B: refund approved"), "-> conflict, must re-read")print("Agent B retries:", state.update(state.version, "B: refund approved"), "-> version", state.version)print("Final notes:", state.data["notes"]) # Delegation and coordination: a coordinator hands off, then merges the resultsdef run_workflow(question): specialist = route(question) result = specialist + " agent handled: " + question return {"routed_to": specialist, "result": result, "status": "done"} print(run_workflow("Why was my invoice charged twice?"))The router sends each request to the right specialist and falls back to the general agent. The shared-state demo shows a rejected write and a successful retry, so both notes survive. The final line is the coordinator's result for the billing question.
Knowledge Check
Key Points
The Big Picture
Route → delegate → coordinate → update shared state safely → validate → answer
Orchestration is what turns several separate agents into one reliable system.
Up Next: You can now design teams of agents. In Agent Frameworks you will see why frameworks exist and how they package the loop, tools, state and workflows you have been building by hand.