AI Agent Security Architecture: Containment First vs Analyze First
As AI coding agents evolve from passive code-completion tools into autonomous collaborators that can read and write the filesystem, run shell commands, and access network resources, the question of how to keep them safe becomes a central architectural concern. This article compares two opposing engineering philosophies: containment first, which relies on operating-system-level isolation to enforce what the agent cannot do, and analyze first, which uses a layer of AI to semantically review what the agent intends to do. We break down their differences across six dimensions, including security models, attack surfaces, tool systems, cost and reliability trade-offs, observability, and user experience, and offer guidance on choosing or combining the two approaches.
Introduction: Greater Capability, Greater Exposure
In current software development practice, AI coding agents are rapidly evolving from passive code-completion tools into autonomous collaborators that can read and write the filesystem, execute shell commands, and access network resources. Yet greater capability also brings greater potential risk: an agent that can run commands, modify disk contents, and issue network requests on its own can cause irreversible damage if it makes a mistake or is maliciously manipulated—deleting critical files, leaking sensitive credentials, or committing code in bulk to the wrong branch.
As a result, balancing “making the agent capable enough” against “making the agent safe enough” has become the foremost concern in architecture design. The industry has split into two sharply different engineering philosophies. One advocates containment first: using operating-system-kernel-level isolation and restrictions to define, at a physical level, what the agent “cannot do.” The other advocates analyze first: using a layer of AI to semantically review what the agent “intends to do,” employing AI to supervise AI. This is not a simple matter of one being better than the other, but rather two fundamentally different trust models.
A Core Mental Model: Every Difference Traces Back to One Root Question
Almost all of the design differences between these two architectural systems can ultimately be traced back to different answers to one fundamental question:
How do you ensure safety when AI executes commands?
- The containment first answer: Don’t count on the AI to “behave itself”; instead, limit its capability boundaries. It chooses a trust model plus hard isolation—treating the AI as an ordinary process with restricted capabilities, so that any operation exceeding its boundaries is directly rejected by the operating system kernel, no matter how the agent is manipulated.
- The analyze first answer: Since the model cannot be fully trusted, introduce a second layer of AI to review every intent of the first layer. It chooses a partial-trust plus intelligent-review model—using semantic understanding to judge whether a command is dangerous.
Once this fundamental stance is chosen, the next seven or eight seemingly independent technical decisions—number of tools, cost structure, error handling, cross-platform strategy, observability design—all follow as a chain reaction. Understanding this causal chain is far more valuable than memorizing any specific parameter.
Deep Comparison Across Six Dimensions
1. Security Model: Kernel-Enforced Deterministic Blocking vs. AI Semantic Review
This is the most fundamental divide between the two architectures.
Containment first relies on the isolation capabilities provided by the operating system kernel. On Unix-like systems, this typically manifests as system call filtering (restricting the set of system calls a process can invoke), mandatory access control (limiting accessible files and network resources), and namespace isolation; on desktop operating systems, it corresponds to their respective sandboxing frameworks; in the Windows ecosystem, it maps to restricted token mechanisms. Its core modes are usually divided into three tiers: read-only, workspace-write, and danger-full-access.
- Bypass resistance: Very high. Interception happens at the kernel layer, is deterministic, and does not depend on any “judgment.”
- Resistance to novel attacks: Physically immune. No matter how novel the attack method, as long as it crosses the boundary, the kernel will block it.
- The cost: Each operating system must be implemented separately for cross-platform support; moreover, failures often manifest as “silent failures”—an operation is rejected without a clear prompt, resulting in a poor user experience.
Analyze first relies on an LLM classifier layer combined with multi-tier permissions. Its decision chain usually consists of: path allow/deny lists + classifier semantic scoring + interactive approval.
- Bypass resistance: Lower. Security depends on whether the classifier “judges correctly.”
- Resistance to novel attacks: Can potentially be bypassed. Techniques the classifier has never seen may slip through.
- The advantage: A single logic works across platforms, and it can explain to users “why it blocked” through interactive dialogs, offering a friendlier experience.
# Containment first decision (pseudocode)if target_path not in allowed_write_dirs or syscall not in allowed_set:kernel.reject() # Deterministic kernel-level block, independent of content semantics# Analyze first decision (pseudocode)score = classifier.evaluate(command_text, context)if score > threshold or path in blacklist:ask_user_for_approval() # Depends on the correctness of the judgment
2. The Three Attack Surfaces of a Classifier
The weak point of the analyze first architecture lies in the fact that the classifier needs to “understand” the semantics of commands, which exposes three attack surfaces:
- Prompt injection: An attacker induces the model to generate a command that looks ordinary but is actually malicious, through files, web pages, or comments that get read.
- Command obfuscation: Using techniques such as base64 encoding, environment variable concatenation, and multi-layer pipes to make a dangerous command appear “harmless” on the surface, bypassing text-level rule matching.
- Novel attacks: Attack patterns the classifier never encountered during training are inherently difficult to identify.
The containment first architecture is essentially immune to all three of these attacks—it doesn’t care what a command “looks like” at all, only whether the operation “crosses the boundary.” This is the fundamental advantage of “capability restriction” over “intent analysis.”
3. Tool System: A Minimalist Swiss Army Knife vs. A Professional Toolbox
Containment first tends to give the model a minimalist toolset (around a dozen tools). It often does not include a built-in file-search tool, instead letting the model invoke general-purpose search commands itself; file editing uses a one-shot, multi-file, transactional patch-style modification (either everything takes effect or everything rolls back); shell execution goes directly through the kernel sandbox; network access also reuses general-purpose commands. Its philosophy is: give the model a shell and let it assemble the operations it needs.
Analyze first tends to provide a rich and structured toolset (over forty tools). It equips dedicated tools for file search, file editing (one change at a time), command execution (going through permission checks), web page fetching, web search, multi-agent orchestration, and code semantic understanding (based on the Language Server Protocol), among others. Its philosophy is: every tool has a clear input/output contract, like a professional toolbox.
4. Trade-offs in Cost, Dependencies, and Reliability
| Dimension | Containment first (Option A) | Analyze first (Option B) |
|---|---|---|
| Flexibility | ✅ High; freely assemble any command | ❌ Low; limited to predefined tools |
| Dependency reliability | ❌ Relies on the user’s built-in system environment | ✅ Bundled dependencies, works out of the box |
| Token cost | ✅ Low; tool definitions are compact | ❌ High; a large number of tool schemas consume context |
| Observability | ❌ Low; shell is a black box | ✅ High; structured results are easy to audit |
| Permission granularity | ❌ Coarse; shell black box is hard to subdivide | ✅ Fine; authorization per tool |
Containment first trades general-purpose commands for flexibility but shifts the reliability burden onto the user’s system environment; analyze first trades structured tools for reliability and observability, but pays the price of higher token costs and lower flexibility.
5. Observability and Permission Granularity
Because the shell is a “black box,” the containment first architecture struggles to perform fine-grained auditing and subdivided authorization for specific operations—the kernel only tells you “it was rejected,” but can hardly clarify “which part of which step crossed the line.” The analyze first architecture, by contrast, is naturally better suited for audit logging, cost attribution, and per-tool permission control due to its fine tool granularity and structured returns, which is especially important for enterprise-grade compliance scenarios.
6. User Experience and Failure Modes
Containment first failures tend to be “silent”—a command is rejected by the kernel, but the user may not immediately know the reason; analyze first tends toward “interactive”—popping up a dialog to explain the risk and request authorization. The former is safer but may be confusing, while the latter is more transparent but may interrupt flow.
Selection Guidance: There Is No Best, Only the Most Suitable
- If your primary requirement is defending against untrusted input and guarding against adversarial attacks (for example, having the agent process untrusted content from external repositories, issues, or web pages) and the runtime environment is relatively controlled, the kernel-level hard isolation of containment first is the safer choice.
- If you value cross-platform consistency, fine-grained permissions, auditability, and out-of-the-box reliability more, and the trust sources are relatively closed, the structured tool chain of analyze first is more appropriate.
- In practice, the two are not mutually exclusive. Mature solutions often adopt layered defense: using containment isolation as a fallback boundary while layering semantic review and approval on top at the interaction layer, balancing determinism and explainability.
Conclusion
The divergence between containment first and analyze first is essentially a contest between two security philosophies: “restricting capability” and “reviewing intent.” The former trades the kernel’s determinism for a security floor, while the latter trades AI’s semantic understanding for flexibility and transparency. What truly determines the success or failure of an architecture is not how many isolated features are stacked together, but whether you have thought through that fundamental question—do you trust that “the boundary cannot be crossed,” or that “AI can see through malice”? Once you have thought this through, every subsequent design decision will naturally fall into place.
