Multi-Agent Hierarchical Architecture: Solving Context Pollution and Synchronization Blocking
This article explains why a single powerful model with many tools breaks down as workloads grow, and how a hierarchical Router/Worker architecture addresses two core failures: cross-task context pollution and synchronous blocking. It covers the resource competition behind context windows, the design of task, context, lifecycle, and permission contracts, practical engineering considerations, and the scenarios where a monolithic approach remains the better choice.
Introduction: Why “One Smart Model Plus a Lot of Tools” Doesn’t Scale
In the early stages of agent development, the most common approach is to pick a model that is capable enough, attach as many tools as possible, and write an extremely long prompt covering every scenario, letting this single agent handle everything. This approach works well in demo environments: responses are fast, the execution path is short, and debugging is intuitive. But as the number of tasks grows, execution times lengthen, and concurrent requests increase, problems erupt all at once: the context fills up with irrelevant information, the model mistakes conclusions from a previous task for premises of the current one, a single long-running command freezes the entire session, and users experience it as “unresponsive.”
The hierarchical architecture (Router/Worker) is a response to exactly these structural problems. Its rationale does not come from imitating organizational charts, nor from the intuition that “more layers means more advanced.” It comes from a simple, real constraint: the context window is a scarce resource, and different roles have fundamentally different demands on that resource. Starting from the nature of resource competition, this article breaks down two typical failures of the single large agent model, then presents the key design points, contract constraints, and applicability boundaries of the hierarchical architecture.
1. The Essence: Hierarchy Resolves Competition for Context Resources
1.1 Two Fundamentally Different Resource Demands
Splitting agents into “coordinators” and “executors” is not about drawing an org chart. It is because these two kinds of work consume the context window in completely different patterns.
The coordinator (the main session/Router) is responsible for global scheduling: tracking multiple tasks at once, knowing which step each task has reached, where it is stuck, and who should handle the next step. It needs low-bandwidth information: task identifiers, status summaries, key conclusions, and a one-sentence description of failure reasons. Its working rhythm is short turns: receive a request, make a judgment, dispatch a task, and continue receiving.
The executor (the sub-agent/Worker) is responsible for tackling a single task: actually modifying files, running commands, reading logs, and iterating through trial and error. It needs high-bandwidth information: complete error stacks, lists of approaches already tried, raw tool outputs, and relevant code snippets. Its working rhythm is long turns: continuously pushing toward one goal until it produces a deliverable result or a clear failure.
1.2 The Consequences of Mixing Them
When one session takes on both roles at once, the window is quickly filled with two heterogeneous kinds of information: the summaries needed for coordination get drowned in execution details, while the raw outputs needed for execution are constantly compressed by summarization logic. The result is that neither side gets the information format it truly needs, and both judgment quality and execution quality decline.
The essence of layering is to physically separate two workloads that compete for the same window resource, then use explicit contracts to hold the boundary.
2. Two Typical Failures of a Single Large Agent Doing Everything
2.1 Cross-Task Context Pollution
Suppose the same agent processes Task A and Task B in parallel. The abandoned approaches from trial and error in Task A, large chunks of debugging logs, and temporary intermediate conclusions keep occupying the window. When execution switches to Task B, the model may treat old conclusions from A as implicit premises for B, producing a directional error.
Relying on prompts to tell the model to “ignore the preceding text” is unreliable: the window budget is not freed by a single instruction, and the attention mechanism does not automatically mask historical content because of a declaration. The reliable approach is physical isolation: each task runs in an independent sub-session with its own context space, reclaimed according to policy after the task ends. Tasks do not share raw process data with each other and interact only through structured results.
A schematic task dispatch logic can be written as:
on request:task_id = register(task)if not worker_alive(task_id):worker = spawn_worker(context=isolated)dispatch(worker, task_payload)return # The main session is released immediately and continues receiving
2.2 Synchronous Blocking
The second failure is synchronous waiting. If the main session waits synchronously for a long command to return, such as a full test run, a full build, or a batch data processing job, it can neither receive new messages nor judge the progress of other tasks in parallel until that command returns. From the user’s perspective, the entire agent is “frozen,” even though the underlying model and tools are working fine.
After layering, the main session dispatches a task and returns immediately, retaining the ability to respond to new requests. Long-running execution is pushed down to sub-sessions, where blocking is confined locally and does not propagate globally.
3. Key Design of the Hierarchical Architecture: Holding the Boundary with Contracts
Physical isolation solves “no mutual pollution”; contracts solve “still being able to collaborate after isolation.” Contracts should cover at least the following four categories:
- Task contract: which fields the input contains, what structure the expected output has, and how failures are expressed. Inputs and outputs should be structured data, not large blocks of natural language.
- Context contract: what the parent session passes to the sub-session (goal, constraints, necessary background) and what the sub-session returns (conclusions, artifact references, remaining risks). Raw process data stays inside the sub-session by default.
- Lifecycle contract: when a sub-session is created, how it is terminated on timeout, how failures are retried, and how results are reclaimed. Avoid zombie sessions occupying resources over the long term.
- Permission contract: which tools the sub-session may call, what data scope it may access, and which high-risk operations require returning to the parent session for confirmation.
A simplified return structure example:
{"task_id": "t-1024","status": "succeeded | failed | need_input","summary": "One-sentence conclusion","artifacts": ["path-or-reference"],"risks": ["Uncovered boundary conditions"],"usage": {"turns": 12, "tokens": 18000}}
The parent session makes scheduling decisions based on status and summary, without needing to read the sub-session’s complete execution trace. Only when auditing or reviewing is necessary does it retrieve details by task_id through a logging service. This both controls window usage and preserves traceability.
4. Engineering Implementation Points
- Async first: Task dispatch, result return, and status queries should all go through asynchronous channels, decoupled with message queues or task queues, avoiding binding long-running operations to the session lifecycle.
- Observability: Tag each task and collect call chains, durations, and failure reasons in a unified way. Use monitoring and alerting to detect “stuck sub-sessions” and “abnormally expanding contexts.”
- Context budget: Set separate window budgets and compression strategies for parent and sub-sessions. Store summary generation and raw log retention separately, and route log-type data to a logging service rather than the session context.
- Idempotency and retries: Sub-session execution should be designed to be re-entrant, so retries do not stack side effects. Introduce a confirmation step for high-risk write operations.
- Cost control: Choose models of different capability tiers based on task complexity. The coordination step can use a low-cost model, leaving the budget for the execution step that actually does the work.
5. When Layering Is Not Worth It
Layering is not free. It brings extra session management, state synchronization, failure retries, and debugging complexity. The following scenarios may favor the monolithic approach:
- A single task with an extremely short path, completable in one question-and-answer exchange;
- No concurrency needs, so the real conditions for cross-task pollution do not exist;
- The team lacks async and observability infrastructure, making failures harder to locate after layering;
- Extremely latency-sensitive and very lightweight tasks, where the extra dispatch overhead is not worthwhile.
The decision criterion can be summarized in one sentence: when “context contention” and “synchronous waiting” have not yet become real bottlenecks, there is no need to layer prematurely; when they begin to affect correctness and responsiveness, the benefits of layering quickly exceed its complexity cost.
Conclusion
The value of Router/Worker layering lies not in whether it resembles a team in form, but in that it precisely addresses two engineering realities: the context window is a finite resource, and long-running execution should not block the whole system. Physically separating workloads that compete for the same resource, then using four kinds of contracts (task, context, lifecycle, and permission) to hold the boundary, is what keeps the system correct and smooth as tasks grow in number and length. When implemented on platforms such as Baidu AI Cloud, combined with common capabilities like asynchronous messaging, logging, and monitoring, this architecture can be both robust and operable.
