Part 4 · Engineering Patterns for Reliable Agents

Sandbox & Credential Isolation

Generated code and secrets never share the same container. Structural security beats prompt-based security

THE QUESTION THIS PAGE ANSWERS

ANSWER FIRST

What is the key idea behind “Sandbox & Credential Isolation”?

Generated code and secrets never share the same container. Structural security beats prompt-based security

DECISION RULE

Make the claim earn its place. Use this page as a decision aid, not a definition to memorize. Connect the idea to one real task, one observable result, and one failure that would change your mind.

TRY NEXT

Write one question you could answer with evidence after trying this idea.

WATCH FOR

A conclusion that sounds complete but leaves the key assumption untested.

Core Principle
Generated code and secrets
must never be in the same place
This is the first principle of Agent security: physical isolation between credentials and the execution environment.
What Went Wrong with the Old Approach

Traditional Architecture: Everything in One Container

Everything in One Container
Code execution, API keys, OAuth tokens, and session credentials all coexist in the same runtime environment. Whatever the Agent can see, malicious code can see too.
Prompt Injection Steals Keys in One Step
An attacker only needs to convince the Agent via Prompt Injection to run a single line of code (echo $API_KEY) to exfiltrate all environment-variable secrets. The attack chain is extremely short and nearly impossible to prevent.
The Smarter the Agent, the Worse the Problem
A more capable Agent means more powerful tool-calling abilities. Under the old architecture, this also means a larger attack surface. Model improvements do not automatically fix this problem—they actually make it worse.
The more capable the model, the more severe the security problem. This means security architecture cannot rely on the model's self-discipline—it must be guaranteed through structural design: even if the model is fully compromised, attackers should still be unable to reach credentials.
Two Credential Isolation Patterns
MODE 1
Bound to Resource
The Token is embedded into the resource access path at the time of use and never exists as a standalone variable. The Agent can use it, but can never see it.
GIT CLONE SCENARIO
The Token is injected into the remote URL at clone time:
https://token@github.com/repo.git
The Agent in the sandbox can perform push/pull operations normally, but cannot directly read or extract the Token—it is buried deep in the Git configuration, not an accessible environment variable.
Token
Injected into Remote URL
Usable but Invisible to Agent
MODE 2
Vault Proxy Pattern
Tokens are stored in a secure Vault service. Every API call from the Agent is routed through a proxy, which looks up the corresponding credential by Session ID and injects it into the request. The Agent never touches the raw Token.
MCP OAUTH SCENARIO
When the Agent initiates an MCP call, the request first reaches the proxy service. The proxy looks up the OAuth Token from the Vault using the current Session ID, injects it into the request header, and forwards the request to the target MCP server. The Agent only ever knows "the call succeeded"—it never sees a single character of the Token.
Agent Sends Request
Proxy Injects Token
Target Service
OS-Level Sandbox Isolation

Three-Layer Isolation Barrier

Filesystem Isolation
The Agent can only access files within its working directory. All other filesystem paths on the host machine—credentials, configs, and system files—are invisible.
Network Isolation
The sandbox restricts network access. The Agent cannot send data to arbitrary external services—even if it obtains a credential, it has no way to exfiltrate it.
Process Isolation
The Agent's code runs in an isolated process space with no access to other processes on the host. It can neither read other processes' memory nor send signals to them.
Three-Layer Trust Hierarchy

Layered Permission Control from Tool to Organization

TOOL
Tool-Level Trust
The finest-grained control. Certain high-risk tools—like file deletion, database writes, and email sending—require human approval each time they are used. Safe read-only operations (e.g., searching code, reading files) can be set to auto-allow.
SESSION
Session-Level Trust
The permission scope for a single conversation. At the start of a session, the user grants the Agent a specific scope (e.g., "may read/write /src but not modify /config"). The scope remains fixed for the entire session and is automatically revoked when the session ends.
GLOBAL
Global Policy
Organization-level security policy constraints. Regardless of what permissions a user grants during a session, the Agent cannot violate global policies—such as "never access the production database" or "no outbound requests to external domains." This is the ultimate safety backstop.
Structural security > Prompt-level security. Design the system so that attacks are structurally impossible—you can't rely on the model's self-discipline. Physical isolation of credentials from the execution environment, multi-layer trust control, and OS-level sandboxing are the cornerstones of running Agents safely in production.

Where the risk boundary sits in “Core Principle”

“Generated code and secrets never share the same container.” moves security beyond telling a model not to make mistakes. The real protection is ensuring that a mistaken judgment cannot become an irreversible result through permissions, data, or the environment.

Separate model suggestions from real authority

In the flow described by “Generated code and secrets never share the same container.”, check what the user may request, what the model may suggest, what the tool actually permits, and who can approve a write or send action. Web pages, documents, and tool results can carry untrusted instructions; looking like documentation does not grant them authority.

Security includes failure and recovery

Use “Generated code and secrets never share the same container.” for a reverse exercise: add bad input, a missing credential, or an approval that never arrives. Confirm that the system refuses, pauses, and leaves a trace instead of executing to completion.

From “Core Principle” to “What Went Wrong with the Old Approach”

“Core Principle” grounds the problem in “Generated code and secrets must never be in the same place This is the first principle of Agent security: physical isolation between credentials and the execution environment”. “What Went Wrong with the Old Approach” then moves it toward “Traditional Architecture: Everything in One Container Everything in One Container Code execution, API keys, OAuth tokens, and session credentials all coexist in the same runtime environment. Whatever the Agent…”. Together, they show that the lesson is not just a conclusion to remember, but a claim with conditions.

Carry the judgment into the next situation

For security, separate what the model wants to do from what the system permits. Check data boundaries, tool permissions, human confirmation, and recovery after failure.

  • “Core Principle”: Generated code and secrets must never be in the same place This is the first principle of Agent security: physical isolation between credentials and the execution environment
  • “What Went Wrong with the Old Approach”: Traditional Architecture: Everything in One Container Everything in One Container Code execution, API keys, OAuth tokens, and session credentials all coexist in the same runtime environment. Whatever the Agent…
  • “Three-Layer Trust Hierarchy”: Layered Permission Control from Tool to Organization TOOL Tool-Level Trust The finest-grained control. Certain high-risk tools—like file deletion, database writes, and email sending—require human approval each…

The final “Three-Layer Trust Hierarchy” brings the discussion to “Layered Permission Control from Tool to Organization TOOL Tool-Level Trust The finest-grained control. Certain high-risk tools—like file deletion, database writes, and email sending—require human approval each…”. The useful thing to carry forward is knowing which judgments must be revisited when input, scale, or risk changes.

Mark as learned Your reading progress updates automatically
← PreviousNext →

Keep reading

The next useful article in the thread.

ARTICLE DISCUSSION

Leave one useful thought here.

Keep the idea that clicked, the question that stayed open, or a small note for the next learner.

Discussing Sandbox & Credential Isolation Engineering Patterns for Reliable Agents
3discussionsArticle discussion · synced with the Circle
View in the learning circle
AM
Asha MorganContent editor
INSIGHTField note

I turned one judgment from this article into a small experiment I could run today. Knowing what to observe next is more useful than simply remembering the conclusion.

ARTICLE DISCUSSION7 helpful
LH
Lin HarperIndie developer
INSIGHTInsight

After reading this, I first looked for the conditions behind the idea instead of copying the method into a project. That order made the later trade-offs much clearer.

ARTICLE DISCUSSION5 helpful
KM
Kiki MooreProduct operations
QUESTIONQuestion

When this judgment reaches real work, which constraint should be added first? I am curious which step matters most between reading and the first practical attempt.

ARTICLE DISCUSSION4 helpful