Building a Layered AI Agent Architecture: From Context Isolation to Scheduling Decoupling

    This article explains why a single-session AI agent breaks down as task volume, duration, and tool output size grow, and how a layered architecture with a routing layer and an execution layer addresses the root causes. It covers context window contention, cross-task context pollution, synchronous blocking, and the contracts that keep layer boundaries stable, along with guidance on when layering is not worth the added complexity.

    1. The Starting Problem: Pushing a Single “Do-Everything” Session to Its Limits

    The most common initial design for a multi-task agent is straightforward: pick a capable model, attach a set of tools, write a long system prompt, and let one session handle every responsibility. This structure is highly efficient in a demo, because the chain is short, the context is continuous, and debugging feedback is fast.

    Problems surface once the number of tasks grows, individual tasks run longer, and tool responses get larger. These issues are not caused by insufficient model capability. They come from a mismatch between how resources are organized and the scheduling model. A layered architecture (routing layer plus execution layer) is a joint solution aimed at exactly these two points.

    One judgment needs to be stated up front: the value of layering does not come from dividing labor the way an organization does. It comes from reallocating scarce resources and from making execution boundaries explicit.

    2. Root Cause One: The Context Window Is a Contended Scarce Resource

    The routing layer and the execution layer have completely different context requirements.

    The routing layer handles global coordination: which tasks exist, what stage each one is in, what is blocking it, and who gets the next step. It needs identifiers, summaries, and state machine fields. This is a low-bandwidth, short-turn form of information.

    The execution layer handles single-point progress: actually modifying files, running commands, reading error stack traces, and comparing differences across multiple attempts. Raw tool returns, failure logs, and intermediate artifacts can all affect the next decision. This is a high-bandwidth, long-turn form of information.

    When one session carries both roles, the window gets alternately filled with two kinds of heterogeneous information, which leads to three consequences:

    1. Key state gets diluted: the proportion of the window occupied by state fields that actually drive decisions drops.
    2. Early content gets pushed out: compression or truncation drops the oldest information first, and global constraints are usually at the very front.
    3. Decision quality fluctuates: the same prompt behaves inconsistently at different fill levels, making results hard to reproduce.

    An illustrative split of responsibilities and context budgets:

    1. Routing layer context (low bandwidth)
    2. task_id | goal summary | status | blocking reason | artifact reference | next action
    3. Execution layer context (high bandwidth, isolated per task)
    4. task goal | relevant file snippets | commands and raw output | approaches already tried | failure reasons

    3. Root Cause Two: Cross-Task Context Pollution

    Assume one session handles task A and task B in parallel. Abandoned approaches and large log blocks from A keep occupying the window. When B’s turn comes, the model may treat A’s stale conclusions as premises for B and produce inferences that look reasonable but come from the wrong source.

    Relying on a prompt instruction telling the model to “ignore the above” is unreliable, because the window budget is not released by a single instruction. The workable approach is physical isolation: each task runs in an independent sub-session and returns only structured results.

    1. {
    2. "task_id": "T-102",
    3. "status": "blocked",
    4. "summary": "Build failed, missing dependency declaration",
    5. "artifacts": ["log://T-102/build"],
    6. "next_action": "Retry after adding the dependency"
    7. }

    Isolation brings an additional benefit: replayability. Each task’s context is retained independently, which makes it easier to locate problems, re-run a task, and audit it, without replaying the entire main session.

    4. Root Cause Three: Synchronous Blocking and Scheduling Decoupling

    If the routing layer waits synchronously for a long command to return (a full test run, a build, a bulk scan), it cannot accept new requests before that return, and it cannot tell how other tasks are progressing. From the user’s side, this looks like the system is “stuck.”

    After layering, the routing layer returns as soon as it dispatches a task and becomes event-driven:

    1. Receive request -> classify/split -> dispatch execution -> release immediately
    2. <- result or heartbeat returns <- update status -> aggregate output

    Three things are needed to support this: an explicit task state machine (queued/running/blocked/completed/failed), timeout and cancellation semantics, and heartbeat or progress reporting. If any one of them is missing, the asynchronous flow degrades into “invisible waiting.”

    5. Contract Design: Boundaries Rely on Agreements, Not Prompts

    Whether layering stays stable depends on how clear the contracts between layers are:

    • Input contract: the goal, the constraints, and the scope of resources allowed to be touched.
    • Output contract: status, summary, artifact references, and error classification.
    • Permission contract: the execution layer’s write scope is minimized, and dangerous operations require explicit confirmation.
    • Budget contract: per-task context limit, turn limit, and timeout limit.

    The value of contracts is that the routing layer can schedule without understanding execution details, and the execution layer can make progress without being aware of the global picture.

    6. When Layering Is Not Worth It

    Layering is not more advanced just because it adds complexity. Keep a single session in these cases: a single short task, no need for parallelism, small tool responses, extreme sensitivity to latency, or a team that lacks state management and observability. In these situations, the coordination cost of layering exceeds the benefit. Start by landing context partitioning and structured output, then layer once there is real evidence of pollution and blocking.

    7. Conclusion

    The essence of a layered architecture is a joint solution of context engineering and scheduling engineering: physically separate the parts that compete for the same window, hold boundaries with contracts, and trade asynchrony for throughput. In a general cloud environment, a message queue can handle task dispatch, logging services plus monitoring and alerting can support observability, and a container platform can host isolated execution, letting routing and execution each run at a suitable pace. The deciding criterion is always evidence-driven: quantify the pollution rate and blocking duration first, then decide the layering granularity.