翻篇收录说明
翻篇网页快照

Pydantic AI + E2B: sandboxed coding agents in Python

https://pydantic.dev/articles/pydantic-ai-e2b-sandboxes

最近抓取:2026-12-03 07:00:25(北京时间)

最近内容变化:2026-12-03 07:00:25(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Run Pydantic AI agents in E2B cloud sandboxes with the native E2BSandbox capability. Pause and resume between runs, and recover a Temporal worker crash.

正文摘录

/Pydantic AI Run Pydantic AI coding agents in E2B sandboxes Laís Carvalho 8 mins 2026/10/22 As Markdown A coding agent that can run python -m unittest and edit files is useful. A coding agent that runs them on your laptop, as you, with your SSH keys in ~/.ssh and your production .env two directories up, is a liability you have agreed to without reading the terms and conditions. The agent decided what to run, and nobody reviewed the command before it ran. An agent that waited for review would not be an agent. E2B is now a native sandbox provider in Pydantic AI. Add E2BSandbox() to an agent's capabilities and every shell command and file operation the agent makes runs inside an isolated E2B microVM instead of on your machine. Nothing from your environment reaches the sandbox. The sandbox can pause when a run ends and resume when the next one starts, with its filesystem and memory intact. And because the workspace is identified by a credential-free reference, a durable workflow can carry it across a worker crash. This post walks through two examples from the E2B cookbook: a two-run diagnose-then-fix agent that pauses the sandbox between runs, and the same task inside a Temporal workflow where the worker process gets killed halfway through. #One line, and the agent is off your machine Install the harness with the E2B and Temporal extras, the provider extras the snippets below use (the quick start calls Anthropic, the cookbook examples call OpenAI), and the Logfire extra so the runs are traced. Then set E2B_API_KEY and the provider key: uv add "pydantic-ai-harness[e2b,anthropic,temporal]" "pydantic-ai-slim[openai,logfire]" Then give the agent the sandbox alongside the tools that will use it: import logfire from pydantic_ai import Agent from pydantic_ai_harness.coder import Coder from pydantic_ai_harness.e2b_sandbox import E2BSandbox logfire.configure() logfire.instrument_pydantic_ai() agent = Agent('anthropic:claude-opus-5-5', capabilities=[E2BSandbox(), Coder()]) result = agent.run_sync('Clone https://github.com/pydantic/pydantic-ai and summarize how capabilities work.') E2BSandbox supplies execution only. Coder, or the narrower Shell and FileSystem capabilities, supply the tools, and those tools now run in the sandbox. There is no custom E2B adapter and no template to build. E2B's default template ships with Python and git, which is enough for this post; a project with its own dependencies installs them in a custom template or with a pip install run inside the sandbox before the agent starts, since the default template has pip but not uv. A few defaults worth knowing before the first run. A sandbox lives for an hour unless you pass sandbox_timeout, and when that runs out E2B pauses it rather than killing it. Internet access is on unless you turn it off with allow_internet_access=False. And Pydantic AI never kills a sandbox when a run ends: the application that created it owns its lifetime, and a sandbox you forget is a sandbox you are paying for. The E2B sandbox guide covers cleanup, including an on_run_error hook for runs that fail before there is a result to read a reference from. #Diagnose, pause, resume, fix The first cookbook example gives the agent a small Python CLI that summarizes JSONL API request logs. Its tests fail: the CLI counts server errors wrong and computes p95 latency wrong. The agent gets two runs. The first reads the code and the failing tests and writes diagnosis.md without touching the source. The application then pauses the sandbox, builds a fresh backend attached to it, and a second run fixes the bug using the saved diagnosis and the first run's conversation history. The agent definition is ordinary Pydantic AI. The only E2B-specific part is the capability: import logfire from pydantic_ai import Agent from pydantic_ai_harness.e2b_sandbox import E2BSandbox from pydantic_ai_harness.filesystem import FileSystem from pydantic_ai_harness.shell import Shell logfire.configure() logfire.instrument_pydantic_ai() WORKDIR = '/home/user/log-cli' agent = Agent( 'openai:gpt-4.1-mini', name='log_cli_fixer', instructions='Work only in this sandbox project. Use the supplied tools; verify claims by running tests.', capabilities=[ E2BSandbox( working_dir=WORKDIR, sandbox_timeout=600, allow_internet_access=False, ), Shell(tools=['run_command'], default_timeout=20, max_output_chars=6000), FileSystem(tools=['read_file', 'write_file', 'edit_file']), ], ) The cookbook narrows the tool surface on purpose. Shell exposes only run_command, FileSystem exposes three file tools, and internet access is off because the fixture needs no downloads. A sandbox is a boundary, and a narrow tool list makes the boundary cheaper to reason about. The application seeds the fixture before the agent starts, by constructing the backend itself and working in it through Workspace: from pydantic_ai.workspaces import Workspace from pydantic_ai_harness.e2b_sandbox import E2BSandbox, E2BSandboxBackend backend = E2BSandboxBackend(working_dir=WORKDIR, sandbox_timeout=600, allow_internet_access=False) workspace = Workspace(backend) for name in ('log_stats.py', 'test_log_stats.py', 'requests.jsonl'): await workspace.write_bytes(name, (FIXTURE / name).read_bytes()) first = await agent.run(DIAGNOSE, workspace=backend, usage_limits=limits()) ref = first.workspace.ref Building the backend does no I/O. The first write_bytes creates the sandbox, and from then on backend.ref identifies it. The run result carries the same reference in first.workspace.ref. Now the pause. The application asks the backend for the typed e2b.AsyncSandbox, pauses it, and then constructs a new backend that knows only the reference: sandbox = await backend.get_sandbox() assert await sandbox.pause() attached = E2BSandbox(working_dir=WORKDIR, sandbox_timeout=600).backend(ref) continued = Workspace(attached) assert (await continued.read_text('diagnosis.md')).strip() second = await agent.run( FIX, workspace=ref, message_history=first.all_messages(), usage_limits=limits(), ) assert second.workspace.ref == ref The attached backend's first file read resumes the paused sandbox. E2B keeps a paused sandbox indefinitely, filesystem and memory both, so diagnosis.md is where the first run left it. The second run gets workspace=ref to select the sandbox and message_history to carry the conversation. Either the ref or a history that names it will select the sandbox, but only the history carries what the agent already knows, and the cookbook passes both. The example then does something I want more agent demos to do: it checks the agent's work instead of trusting the agent's summary. It restores the test file from the trusted host fixture, because a shell command can bypass a file tool's restrictions, confirms the input logs were not edited, and runs the tests itself. Four tests pass, the CLI prints the right counts, and the application kills the sandbox in a finally block and confirms it is gone from the account. Created sandbox: <id> Diagnosis saved. Model usage: ... Paused sandbox: <id> Resumed sandbox: <same id>; checksums unchanged before repair ... Ran 4 tests ... OK CLI: {"requests": 4, "server_errors": 2, "p95_ms": 100} Cleanup verified: <same id> The script asserts sandbox identity and SHA-256 checksums before and after the pause. The same id, the same bytes. #Kill the worker, keep the sandbox The pause-and-resume example proves the sandbox outlives a run. The Temporal example proves it outlives the process that was driving the agent. The task is the same log CLI. This time the two agent runs live inside a Temporal workflow, and between them the demo sends SIGKILL to the worker. No Python finally, no graceful shutdown. A new worker starts on the same task queue, Temporal replays the recorded history, and the workflow continues the repair in the sandbox the dead worker left behind. TemporalDurability joins the capability list and the agent is otherwise the one from the first example. The cookbook builds it in a shared make_agent() helper; written out in full, with the Temporal activity settings the example uses, it is: from datetime import timedelta import logfire from pydantic_ai import Agent from pydantic_ai.durable_exec.temporal import TemporalDurability from pydantic_ai_harness.e2b_sandbox import E2BSandbox from pydantic_ai_harness.filesystem import FileSystem from pydantic_ai_harness.shell import Shell from temporalio.common import RetryPolicy logfire.configure() logfire.instrument_pydantic_ai() WORKDIR = '/home/user/log-cli' agent = Agent( 'openai:gpt-4.1-mini', name='log_cli_fixer', instructions='Work only in this sandbox project. Use the supplied tools; verify claims by running tests.', capabilities=[ E2BSandbox(working_dir=WORKDIR, sandbox_timeout=600, allow_internet_access=False), Shell(tools=['run_command'], default_timeout=20, max_output_chars=6000), FileSystem(tools=['read_file', 'write_file', 'edit_file']), TemporalDurability( activity_config={ 'start_to_close_timeout': timedelta(seconds=45), 'retry_policy': RetryPolicy(maximum_attempts=2), }, ), ], ) The agent needs a name, and both it and the workflow must be defined at module level so Temporal can register the activities. Both worker processes import the same module. The workflow takes the sandbox reference as its input. That matters: the controller creates and seeds one sandbox, then hands the workflow a WorkspaceRef, which holds no credentials. Temporal records that input before any worker touches the agent, so whichever worker ends up running the second half of the job knows which sandbox to attach to. This is the cookbook's workflow.py, trimmed to the parts that matter. DIAGNOSE, FIX, fingerprints, and limits are the prompts, the checksum helper, and the UsageLimits from the example's project.py: from temporalio import workflow with workflow.unsafe.imports_passed_through(): from agent import agent from project import DIAGNOSE, FIX, fingerprints, limits from pydantic_ai.durable_exec.temporal import PydanticAIWorkfl