跳到正文
实时
LangChain Blog·· 2026-10-0140

如何为 AI 智能体选择合适的沙箱

How to Choose the Right Sandbox for Your Agent

要点速览

AI 智能体通过编写和运行代码实现高价值功能,但存在安全风险。攻击者可能利用提示词注入诱导智能体生成危害数据系统的代码。选择沙箱需关注隔离文件系统、有限网络访问、资源限制和内核级隔离等特性,以保护数据系统并减少提示词注入攻击危害。

正文

AI agents are most useful when they can take action, and letting them write and run code is one of the highest-value capabilities you can give them. But that autonomy comes with security risks. Agent-written code can create unexpected threats to your data and systems, so you need to control where that code runs and what it can access.

Sandboxes give teams that control. They isolate AI-generated code, limit its permissions, and create a safer boundary for letting agents do real work. They’re computers your agent can safely use. This guide covers the security risks of running AI-generated code and how to choose a sandboxing approach that protects your data and systems without limiting agent usefulness.

What is an AI agent sandbox?

An AI agent sandbox is an isolated environment where an agent can run the code it writes, install packages, and work with files without touching your host machine or the rest of your infrastructure. If an attacker attempts to hijack the agent through prompt injection, the sandbox limits what the agent do to your data and systems.

The Agent lethal trifecta: why agents need a sandbox

LLMs excel at software engineering, making code generation and execution one of the best ways for agents to create value. The code itself can be the output, or the agent can create programs to help it accomplish non-software engineering tasks. A common example of the latter is writing one-off scripts to help with custom data analysis. The risk, however, is that an attacker can use prompt injection to induce your agent to write code that compromises your data and systems. 

Currently, there is no surefire way to prevent prompt injection, and untrusted content containing a prompt injection attack can reach your agent’s context in many ways. End-user inputs, external MCP server responses, and third party-written skills are just a few common examples. Simon Willison, co-author of the popular Django Web Framework and inventor of the term “prompt injection”, summarizes the ensuing risk by outlining the “lethal trifecta” of conditions that, when all true, means your agent can allow an attacker to steal your data:

  1. Access to sensitive data
  2. Exposure to untrusted content
  3. The ability to communicate externally 

Inspired by Willison’s “lethal trifecta” and Chromium’s security Rule of Two, Meta proposed the “Agents Rule of Two.” Under this framework, an agent should not operate fully autonomously if all three of the following properties apply within a session.

  • An agent can process untrustworthy inputs
  • An agent can have access to sensitive systems or private data
  • An agent can change state or communicate externally

If a task requires all three, Meta recommends human approval or another reliable means of validation.

Providing an agent with the tools it needs usually means giving it both access to sensitive data and the ability to communicate externally. Advances in foundation models and harness engineering allow AI agents to leverage more and more context, but that increases the odds an attacker can send a prompt injection attack.

An email assistant, for example, reads messages from unknown senders, accesses a private inbox, and sends replies. A malicious instruction in an incoming message could exploit that combination to make the agent disclose private information or send an unauthorized message. 

Odds are, the lethal trifecta applies to your agent, which means it needs safeguards limiting what actions it can take with what data. Successfully implementing those controls is a lot easier if you first narrow all three risk factors. That’s where sandboxes help. 

What to look for in an AI agent sandbox

When evaluating coding agent sandboxes, look for the following features to limit access to sensitive data and reduce the harm a successful prompt injection attack can cause.

  • Isolated filesystem
  • Limited network access
  • Resource limits
  • Controlled reusability
  • Kernel-level isolation from the host machine

There are products on the market calling themselves "sandboxes" that don't provide this functionality out of the box, so evaluate potential solutions critically before choosing one for your agent. For example, the open-source Kubernetes Agent Sandbox is only secure if deployed to a cluster that has kernel-level isolation between containers, which most Kubernetes clusters do not enforce. 

Isolated Filesystem

The sandbox should contain only the data the agent needs to do its work, and block the agent from trying to access any other data. This minimizes exposure to sensitive data while making it easier to audit and control. 

Limited network access

The sandbox should let you specify which external endpoints the agent can send data to via the internet. This means that, even if an attacker induces your agent to leak sensitive data, it can only send it to entities you trust.

Resource limits

The sandbox should let you control how much compute and memory the agent can use and for how long. A compromised agent should not be able to overconsume your system’s resources. 

Controlled reusability

Reusing sandboxes can be a convenient way to persist agent state across executions, but it does mean that if an attacker compromises the sandbox once, the damage can persist. The sandbox solution should let you decide if that risk is worth taking. 

Kernel-level isolation from the host machine

The controls above only work if the agent can’t override the systems enforcing them. Unfortunately, the kernels that power operating systems often contain bugs a compromised agent can exploit to take control of your machine and bypass any controls protecting the data on it. The solution here is virtualization, meaning your sandbox runs using its own kernel separate from the one powering the machine running it. You can use a microVM-powered solution to efficiently create kernel-isolated sandboxes without incurring the costs of spinning up a full virtual machine per sandbox. 

A sandbox solution that has all of the above doesn’t mean you don’t need to think seriously about the risks a compromised agent poses to your data and systems. Sandboxes alone do not fully eliminate any aspect of the lethal trifecta. Instead, they shrink access to sensitive data and limit external communication to the point where managing prompt injection risk becomes a small enough problem for your team to solve confidently.

Should your agent run inside the sandbox or call it as a tool?

An agent can connect to a sandbox in one of two ways, which our CEO and co-founder Harrison Chase describes in The two patterns by which agents connect sandboxes:

  • Agent in the sandbox. You build an image with your agent preinstalled and run it inside the sandbox. This mirrors local development, but every agent update means rebuilding and redeploying the image.
  • Sandbox as a tool. The agent runs on your own server and calls the sandbox through an API whenever it needs to execute code.

The case for calling the sandbox as a tool rests on three advantages: you can update agent logic without rebuilding an image, API keys stay outside the sandbox, and agent state stays separate from execution.

The main trade-off is network latency, since every execution call crosses the network. Deep Agents supports both patterns.

LangSmith Sandboxes: secure code execution, integrated with our end-to-end agent engineering platform

We developed the LangSmith agent engineering platform to complement our open source products, letting you take the agents you built with LangChain, LangGraph, and/or Deep Agents and robustly test, deploy, and monitor them. Since sandboxes are often integral to building secure agents, we built a managed solution into LangSmith. We designed the product following the best practices listed above and subjected it to rigorous penetration testing. LangSmith Sandboxes are in use by companies of all sizes, including enterprises like Monday.com 

LangSmith Sandboxes are each backed by a dedicated microVM with its own filesystem, giving them kernel-level isolation from each other as well as the underlying infrastructure. You control their lifecycle from start to shutdown to eventual destruction, reusing them as much or as little as you need along the way. You decide what network access, if any, a process running inside the sandbox gets. We also include an authorization proxy that injects secure credentials into outbound traffic after it leaves the sandbox, so you don’t need to put secrets inside the sandbox, where an untrusted process could access and try to misuse them. 

You can manage LangSmith Sandboxes using the same API and authentication you already use with our observability, evaluations, and deployment functionality. Adding sandbox use to an agent built with LangChain, LangGraph, or Deep Agents takes just a few lines of code. 

Why LangSmith Sandboxes:
Secured following modern best practices
Auth proxy isolating secrets from untrusted code
Seamless integration with the LangChain and LangSmith ecosystem

If you are ready to use sandboxes to keep your agents both useful and secure, you can get started with LangSmith Sandboxes today following the documentation here.

来源: LangChain Blog · langchain.com