|
| 1 | +# LLM Context Orchestration from Events |
| 2 | + |
| 3 | +## Core Principle |
| 4 | + |
| 5 | +In ADK, there is a clear distinction between the **Event Stream** and the **LLM Context**: |
| 6 | + |
| 7 | +- **Events are the Ground Truth**: They are immutable records of what has happened in a session (user messages, model responses, tool calls, results). They serve as the audit log and persistence state. |
| 8 | +- **LLM Context is an Orchestrated View**: The context passed to an LLM is not merely a dump of the raw event log. It is a carefully orchestrated view, filtered and transformed to match the specific role, task, and branch of the agent currently executing. |
| 9 | + |
| 10 | +## Orchestration Strategies |
| 11 | + |
| 12 | +The framework orchestrates the translation of events into LLM context using several strategies: |
| 13 | + |
| 14 | +### 1. Task Delegation Translation |
| 15 | + |
| 16 | +When a coordinator agent delegates a task to a sub-agent (Task Agent) via a tool call: |
| 17 | + |
| 18 | +- **Source Event**: Coordinator calls a tool like `request_task_<sub_agent_name>(args...)`. |
| 19 | +- **Orchestrated Context**: |
| 20 | + - The arguments in the `request_task_<sub_agent_name>` tool call are extracted and placed in the **System Instruction (SI)** or treated as the core instruction for the sub-agent. |
| 21 | + - The first user message presented to the sub-agent is synthesized to represent the goal (e.g., "Finish task of [sub_agent_name] with arguments [args]"). |
| 22 | +- **Goal**: Isolate the sub-agent from the coordinator's full history and give it a crisp, clear starting point. |
| 23 | + |
| 24 | +### 2. Branch Isolation |
| 25 | + |
| 26 | +In complex workflows with parallel execution: |
| 27 | + |
| 28 | +- **Source Events**: Events from all nodes and branches are stored in the same session chronologically. |
| 29 | +- **Orchestrated Context**: The framework filters events by `branch` (e.g., `node:path.name`). An agent only sees events that belong to its own execution path. |
| 30 | +- **Goal**: Prevent cross-node event pollution and ensure deterministic behavior in isolated tasks. |
| 31 | + |
| 32 | +### 3. History Trimming and Compaction |
| 33 | + |
| 34 | +To prevent context window overflow and stale instruction loops: |
| 35 | + |
| 36 | +- **Source Events**: A long history of retries, tool calls, and interactions. |
| 37 | +- **Orchestrated Context**: The framework may trim older events or summarize them (event compaction). In task mode, it might keep only the essential setup events, ignoring stale retry loops that would otherwise confuse the LLM. |
| 38 | +- **Goal**: Maintain a focused and efficient context window for the LLM. |
| 39 | + |
| 40 | +## Summary |
| 41 | + |
| 42 | +The relationship is one of **Source vs. View**. Events are the source of truth for the session, while LLM context is a highly orchestrated view of that truth, tailored for the active agent. |
0 commit comments