AI Coding Agent Architecture: Containment First vs. Analyze First
This article compares two foundational architectures for AI coding agents: containment first, which relies on deterministic, kernel-level sandboxing, and analyze first, which uses model-based review and path policies. It explains how the security model shapes tool design, context budgets, cross-platform costs, observability, and user experience, then offers a layered defense approach, selection guidance by scenario, and a benchmark framework for comparing architectures.
1. The Starting Point: Not “Which Is Stronger,” but “Constrain First or Understand First”
When building an AI coding agent, teams most often argue about model capability and the number of tools. But what really determines the shape of the system is an earlier architectural question: before the agent takes any action, how do you guarantee that what it is about to do is safe?
The industry commonly offers two answers, which can be abstracted into two engineering philosophies:
- Containment First: Do not assume the model is always correct. Use deterministic mechanisms to define “what can be done” before discussing intelligence. A typical approach runs command execution inside isolation capabilities provided by the operating system kernel, so out-of-bounds operations physically cannot happen.
- Analyze First: Do not fully trust model output. Use another layer of models or classifiers to review and score “what is about to be done,” then decide whether to allow it, supported by path allowlists/denylists and interactive approval.
The key insight is that this is not seven or eight independent feature trade-offs, but a chain of consequences triggered by one fundamental choice. Once the security model is fixed, tool granularity, context budget, cross-platform cost, observability, and user experience are all “derived” from it.
2. Security Model: Deterministic Blocking vs. Probabilistic Judgment
Containment First: Kernel-Level Capability Restrictions
The idea is to “delegate permissions to the operating system,” using kernel-provided sandbox capabilities to narrow access to resources such as files, network, and processes. Different platforms usually have their own mechanisms (for example, namespaces and system call filtering on Linux, application sandboxes on desktop systems, and restricted tokens on Windows). What they share is that the decision is deterministic: a disallowed path is simply disallowed, without relying on an understanding of intent.
- Advantages: “Physical immunity” to prompt injection, command obfuscation, and unseen attack patterns—because the decision happens at the execution boundary, not at the semantic layer.
- Costs: Cross-platform support requires separate implementation and testing; failures are often “silent denials,” where the user only sees that an operation did not take effect, requiring extra explainability compensation.
Analyze First: AI Reviewing AI
The idea is to “use a model to judge a model’s intent.” A typical pipeline first sends a command or patch through a classifier for scoring, then combines that with path allowlists/denylists and human approval before allowing execution.
- Advantages: One set of logic works across platforms; when rejecting, it can give a natural-language reason, leading to a better interactive experience.
- The risk surface concentrates in three areas: prompt injection (inducing the generation of seemingly ordinary commands), command obfuscation (encoding, variable concatenation, segmented execution), and novel patterns not covered in classifier training. The decision is probabilistic, so there is a possibility of being bypassed.
A Practical Compromise
A more robust engineering approach is layered defense: use deterministic containment as the outer backstop (at minimum limiting write scope and network egress), use semantic review on the inside to improve experience and explainability, and force human confirmation for high-risk actions. Illustrative policy pseudocode:
if action in DENY_LIST: # deterministic hard constraintreject("policy")elif classifier.score(action) > T: # probabilistic soft constraintrequire_human_approval(action)else:execute_in_sandbox(action)
3. Tool System: Swiss Army Knife vs. Professional Toolbox
The security philosophy maps directly onto tool design.
- Minimal toolset (about a dozen tools): Give the model only a controlled shell, a patch-application capability, and an entry point for dispatching subtasks in batches, leaving the rest for the model to assemble commands itself (for example, invoking retrieval commands on its own). The advantage is flexibility and a small context footprint for tool schemas; the disadvantage is that reliability depends on whether the host environment has all dependencies installed, and observability is poor.
- Rich toolset (dozens of tools): Split retrieval, precise editing, command execution, web fetching, language service (LSP) semantic understanding, and sub-agent collaboration into independent tools with explicit inputs and outputs, usually bundling their own binary dependencies. The advantage is predictable behavior, clear logs, and locatable failures; the disadvantage is that the tool descriptions themselves consume a large number of tokens, and flexibility is limited.
Key comparison:
| Dimension | Minimal Toolset | Rich Toolset |
|---|---|---|
| Flexibility | High | Lower |
| Reliability | Depends on runtime environment | Bundled dependencies, stable |
| Token cost | Low | High (many schemas) |
| Observability | Weak | Strong |
An often-overlooked detail is edit granularity: a single transactional multi-file patch suits cross-file refactoring and is easy to roll back; single-point edits are more controllable and easier to review, but come with a higher turn cost for large-scale changes.
4. Context and Cost: The Underestimated Architectural Variable
The more tools there are, the more context is consumed by tool descriptions, parameter constraints, and invocation examples, leaving less budget for code and reasoning. A containment-first minimal toolset tends to spend “context” on the code itself; an analyze-first approach needs careful balancing among “tool descriptions + review prompts + conversation history.”
Cost control recommendations:
- Load tool descriptions in tiers: keep common tools resident and inject long-tail tools on demand;
- Summarize and deduplicate retrieval results to avoid stuffing entire files into the context;
- Use batch dispatch for repetitive subtasks (for example, launching sub-agents in batches from a list) instead of serial conversation;
- Record the token composition of each turn (system prompt / tool schema / retrieval / history) and optimize with data.
5. Experience and Observability: Silent Failures Must Be Compensated
The biggest experience weakness of containment first is “an operation is rejected without explanation.” Practical compensation includes: returning rejection reasons in a structured form (which policy, which path, how to request an exception), providing a read-only dry-run report, and offering a copyable alternative command in the session.
Observability should cover at least four event types: action requests, policy decision results, actual execution and exit codes, and human approval records. Logs and metrics can land on general-purpose capabilities (logging services, monitoring and alerting, object storage archiving) to support auditing and traceback.
6. Selection Guidance: By Scenario, Not by Preference
- High compliance, lots of untrusted input, strong isolation required: prefer containment first, accepting the cross-platform implementation cost;
- Interactive development, clear rejection reasons needed, fast cross-platform delivery: prefer analyze first, but deterministic backstops are mandatory;
- Large enterprise R&D platforms: fuse both—kernel-level containment + semantic review + mandatory approval + full auditing.
7. Evaluation Framework: Compare Architectures with the Same Benchmark
It is advisable to build a unified evaluation set covering: unauthorized writes, outbound network connections, encoded/obfuscated commands, injection-style malicious instructions, cross-file refactoring success rate, proportion of wasted turns, average tokens, and end-to-end latency. Metrics should look not only at “task completion rate” but also at violation interception rate and false rejection rate—the latter determines whether developers are willing to keep using the system long term.
8. Conclusion
The disagreement between “containment first” and “analyze first” is essentially a different answer to “where do you place trust”: in the determinism of the kernel, or in the judgment of the model. A mature solution is not either/or, but letting deterministic mechanisms hold the baseline, letting semantic analysis improve the experience, and continuously calibrating the two with evaluation and auditing.
