Multi-Agent Orchestration with Context Delegation
Intelligent task delegation where agents read, don't receive
🤖 Multi-Agent Orchestration with Context Delegation
The Core Insight
Instead of passing data TO agents, tell them WHERE to look. Each agent loads only what it needs, keeping context fresh and relevant.
Architecture Overview
┌─────────────────────────────────────────────────────────────────┐
│ ORCHESTRATOR AGENT │
│ │
│ Input: "Fix the authentication bug in login component" │
│ │
│ Step 1: ANALYZE │
│ ├─ What type of task? → bugfix │
│ ├─ What domain? → angular, authentication │
│ └─ Complexity? → medium (may need multiple agents) │
│ │
│ Step 2: PLAN │
│ ├─ Sub-task 1: Diagnose the bug │
│ ├─ Sub-task 2: Research similar past issues │
│ ├─ Sub-task 3: Implement fix │
│ └─ Sub-task 4: Verify fix │
│ │
│ Step 3: DELEGATE (with context pointers, not data) │
│ └─ "DiagnoseAgent: Read login.component.ts lines 45-80, │
│ check console errors, identify the bug type" │
│ │
└─────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ DIAGNOSE AGENT │
│ │
│ Received: Pointers to what to read (NOT the data itself) │
│ │
│ Actions: │
│ 1. Read login.component.ts:45-80 │
│ 2. Load core rules for "angular" + "authentication" │
│ 3. Check error patterns in our knowledge base │
│ 4. DECIDE: "This is a null reference on user object" │
│ │
│ Can delegate further if needed: │
│ └─ "EmbeddingSearchAgent: Find 3 similar null reference bugs" │
│ │
│ Report back: Structured diagnosis with confidence │
│ │
└─────────────────────────────────────────────────────────────────┘
Delegation Protocol
Message Format
interface AgentDelegation {
// What to do
task: string;
taskType: 'diagnose' | 'research' | 'implement' | 'verify' | 'search';
// WHERE to look (not the data itself)
contextPointers: {
files?: { path: string; lines?: [number, number] }[];
decisions?: { query: string; limit: number };
patterns?: { category: string };
coreRules?: { categories: string[] };
};
// Constraints
maxTokens?: number; // Budget for this sub-task
timeout?: number; // Max time
canDelegate?: boolean; // Can this agent delegate further?
// Expected output
expectedOutput: {
type: 'diagnosis' | 'recommendation' | 'code' | 'verification';
format: 'structured' | 'freeform';
};
}
Example Delegation Chain
USER: "Fix the flaky test in auth.spec.ts"
ORCHESTRATOR:
├─ Plan: [Diagnose, Research, Fix, Verify]
└─ Delegate to DIAGNOSE_AGENT:
{
task: "Identify why the test is flaky",
contextPointers: {
files: [{ path: "auth.spec.ts" }],
coreRules: { categories: ["testing"] }
},
canDelegate: true
}
DIAGNOSE_AGENT:
├─ Reads auth.spec.ts (fresh context)
├─ Loads testing core rules (5 rules, ~200 tokens)
├─ Notices: async timing issue
├─ Delegates to SEARCH_AGENT:
{
task: "Find past flaky test fixes",
contextPointers: {
decisions: { query: "flaky test async timing", limit: 3 }
},
canDelegate: false
}
SEARCH_AGENT:
├─ Searches embeddings
├─ Returns: 3 similar past fixes
└─ Reports back to DIAGNOSE_AGENT
DIAGNOSE_AGENT:
├─ Combines: file analysis + core rules + similar fixes
└─ Reports to ORCHESTRATOR:
{
diagnosis: "Async operation not awaited",
confidence: 0.92,
suggestedFix: "Add await before assertion",
similarPastFix: "dev_12345..."
}
ORCHESTRATOR:
├─ Receives diagnosis
├─ Delegates to IMPLEMENT_AGENT with diagnosis
└─ ... continues the chain
Context Budget Management
Per-Agent Limits
┌─────────────────────────────────────────────────────────────────┐
│ CONTEXT BUDGET ALLOCATION │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ORCHESTRATOR: 5,000 tokens │
│ ├─ Task understanding: 500 │
│ ├─ Planning: 500 │
│ ├─ Core rules (always): 500 │
│ ├─ Sub-agent responses: 2,500 │
│ └─ Reserve: 1,000 │
│ │
│ SUB-AGENTS: 3,000 tokens each │
│ ├─ Task from orchestrator: 200 │
│ ├─ Loaded context: 2,000 (files, decisions, etc.) │
│ ├─ Core rules for category: 300 │
│ └─ Reserve for output: 500 │
│ │
│ SEARCH AGENTS: 1,000 tokens │
│ ├─ Search query: 100 │
│ ├─ Results: 800 │
│ └─ Reserve: 100 │
│ │
└─────────────────────────────────────────────────────────────────┘
Why This Works
| Single Agent | Multi-Agent Delegation |
|---|---|
| 1 context window | N context windows |
| Must load everything upfront | Load on-demand |
| Context gets polluted | Each agent starts fresh |
| Can't specialize | Specialized agents |
| 50K tokens, 10% relevant | 3K tokens, 90% relevant |
Agent Types
1. Orchestrator Agent
- Role: Plan, delegate, synthesize
- Loads: Task understanding, core rules, sub-agent responses
- Delegates to: All other agent types
2. Diagnose Agent
- Role: Understand what's wrong
- Loads: Specific files, error context, relevant patterns
- Delegates to: Search agents for similar issues
3. Research Agent
- Role: Find relevant past decisions/patterns
- Loads: Embeddings, pattern library
- Delegates to: None (leaf node)
4. Implement Agent
- Role: Write/modify code
- Loads: Target files, style guides, examples
- Delegates to: Verify agents
5. Verify Agent
- Role: Check if solution works
- Loads: Test files, success criteria
- Delegates to: None (leaf node)
6. Search Agent
- Role: Semantic search in knowledge base
- Loads: Only search results
- Delegates to: None (leaf node)
Implementation Phases
Phase 1: Basic Delegation (NOW)
- Orchestrator creates plans
- Single-level delegation (no recursion yet)
- Context pointers protocol
Phase 2: Recursive Delegation (NEXT)
- Sub-agents can delegate
- Depth limit (max 3 levels)
- Budget tracking across chain
Phase 3: Learning from Delegation (LATER)
- Track which delegation patterns work
- Auto-optimize agent selection
- Learn ideal context pointers for task types
Benefits
- Fresh Context: Each agent starts with empty context, loads only relevant data
- Specialization: Agents can be optimized for specific tasks
- Scalability: Complex tasks split into manageable pieces
- Efficiency: Total relevant context much higher than single agent
- Resilience: If one sub-agent fails, orchestrator can retry or use alternative