精选

← 返回 OPC日报

LangChain Blog · 产品 ·

How to Choose the Right Sandbox for AI Agents

LangSmithHow to Choose the Right Sandbox for Your AgentRahul VermaOctober 1, 20268min
Go back to blogCreate agentsCreate agentsShare
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 sandboxLLMs 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:Access to sensitive dataExposure to untrusted contentThe 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 inputsAn agent can have access to sensitive systems or private dataAn agent can change state or communicate externallyIf 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 sandboxWhen 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 filesystemLimited network accessResource limitsControlled reusabilityKernel-level isolation from the host machineThere 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 FilesystemThe 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 accessThe 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 limitsThe 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 reusabilityReusing 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 machineThe 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 platformWe 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.Related contentLangSmithWe rebuilt LangSmith around the agentSam CrowderNovember 16, 20268minObservability & EvalsLangSmithWhat Is Eval-Driven Development? How to Improve AI AgentsDivya SanthanamNovember 9, 202612minLangSmithLangSmith BYOC is now available on AWS, Azure, and Google CloudKarthic SubramanianNovember 3, 20265minSign up for our newsletter to stay up to date
Thank you! Your submission has been received!Oops! Something went wrong while submitting the form.
See what your agent is really doingLangSmith, our agent engineering platform, helps developers debug every agent decision, eval changes, and deploy in one click.Try LangSmith
Get a demo

查看来源原文