Skip to main content

Agent frameworks & SDKs

Pydantic AI

Run Pydantic AI's Shell and FileSystem capabilities in an E2B sandbox. The native E2BSandbox workspace keeps its files across runs, pauses and worker restarts.

Maintained by
Pydantic
How it works
Built into pydantic-ai-harness
Package
pydantic-ai-harness[e2b]
Language
Python

Set it up

Get started

Paste the prompt into your coding agent, or follow the steps yourself.

Paste this into your coding agent

Add E2B sandboxes to this Pydantic AI project.

  1. 1.Install pydantic-ai-harness with the e2b extra ("pydantic-ai-harness[e2b]") using the project's package manager. It needs Python 3.11 or newer.
  2. 2.The sandbox needs the E2B_API_KEY environment variable. If it isn't set, ask me for my API key from https://console.e2b.dev/?tab=keys. Never hardcode it.
  3. 3.Follow the Pydantic AI guide at https://docs.e2b.dev/agents/pydantic-ai. Create an E2BSandboxBackend, give my agent the E2BSandbox, Shell and FileSystem capabilities, pass the backend as workspace=, and destroy the sandbox in a finally block.
  4. 4.Run the agent once and show me the output that came from the sandbox.

Or set it up yourself

  1. 1. Install the E2B integration

    pip install "pydantic-ai-harness[e2b]>=0.54.0" "pydantic-ai-slim[openai]>=2.54.0"
  2. 2. Set your API keys

    export E2B_API_KEY="your_e2b_api_key"
    export OPENAI_API_KEY="your_openai_api_key"
  3. 3. Give the agent an E2B workspace

    from pydantic_ai import Agent
    from pydantic_ai_harness.e2b_sandbox import E2BSandbox, E2BSandboxBackend
    from pydantic_ai_harness.filesystem import FileSystem
    from pydantic_ai_harness.shell import Shell
    
    backend = E2BSandboxBackend(
        working_dir="/home/user/log-cli",
        sandbox_timeout=300,
        allow_internet_access=False,
    )
    
    agent = Agent(
        "openai:gpt-4.1-mini",
        capabilities=[
            E2BSandbox(),
            Shell(tools=["run_command"], default_timeout=20),
            FileSystem(tools=["read_file", "write_file", "edit_file"], content_hashes=False),
        ],
    )
  4. 4. Run it and clean up

    import asyncio
    
    
    async def main():
        try:
            result = await agent.run(
                "Write log_stats.py that counts HTTP 500–599 responses in a log file, "
                "create a small sample log, and run the script on it.",
                workspace=backend,
            )
            print(result.output)
        finally:
            if backend.ref is not None:
                await E2BSandbox().destroy(backend.ref)
    
    
    asyncio.run(main())

Security

What runs in the sandbox

Commands and file edits run in an isolated sandbox; the agent loop and model calls stay on your machine.

  • CodeShell commands and file edits from the Shell, FileSystem and Coder capabilities.
  • StateSave result.workspace.ref and pass it back as workspace= to reconnect. A paused sandbox resumes with its files.
  • LifetimeSandboxes default to a 3,600-⁠second lifetime and pause on timeout. Pydantic AI never deletes them; call destroy(ref).
  • SecretsAPI keys stay in the process running the agent. The sandbox doesn't inherit your environment.

3B+ sandboxes started

Give every agent a machine