Pydantic AI
Give a Pydantic AI agent a Platinum sandbox as its workspace, so the harness's Coder, Shell and FileSystem tools run in a microVM of their own.
Last updated
Give a Pydantic AI agent a Platinum sandbox as its workspace. Tools that use ctx.workspace, such as the Pydantic AI Harness's Coder, Shell and FileSystem, then run commands and file operations in a microVM. The model and the agent loop stay in your process.
Install
The backend ships in the Python SDK as an optional extra. It needs Python 3.11 or later and pydantic-ai 2.71 or a later 2.x:
pip install 'platinum-sdk[pydantic-ai]' pydantic-ai-harnessRun an agent in a sandbox
Add PlatinumSandbox to the agent's capabilities next to the tools that use a workspace. It reads PT_TOKEN and PT_API_URL:
from pydantic_ai import Agent
from pydantic_ai_harness.coder import Coder
from platinum.pydantic_ai import PlatinumSandbox
sandbox = PlatinumSandbox(cpu=2, ram_mb=2048)
agent = Agent("anthropic:claude-sonnet-5-5", capabilities=[sandbox, Coder()])
result = await agent.run("Write hello.py and run it.")
await sandbox.destroy(result.workspace.ref)Pydantic AI never deletes a workspace, so call destroy when the work is done.
Continue in the same sandbox
The sandbox is created on the run's first workspace operation. A later run with the message history, or with workspace=result.workspace.ref, attaches to the same sandbox and starts it if it was stopped.
| Behaviour | Detail |
|---|---|
| Working directory | /workspace, created on first use |
| Long commands | A timeout up to 80 s is one request; longer ones, or none, run as a managed process |
| Timeouts | WorkspaceTimeoutError, with the output printed so far |
| Cancelling | A cancelled command is stopped in the sandbox |
| Files | The builtin file errors (FileNotFoundError, IsADirectoryError, ...); large files move in parts |
Bring git, Node or other tools
The default template, pt-base, has Python 3.12 but no git, curl or Node. Build a template from an image once:
from platinum import Platinum, Template
Template.from_image("python:3.12-slim").apt_install(["git", "curl", "nodejs", "npm"]).build(Platinum(), "agent-tools")The build takes a minute or two. Pass the template's name, and every new sandbox starts with those tools:
PlatinumSandbox(template="agent-tools")Keep API keys out of the sandbox
Pass org secrets at creation. The egress proxy writes each one into its allowed hosts' request headers, so the agent's tools use the key without holding it:
sandbox = PlatinumSandbox(create_options={"secrets": ["search-api"]})See also
LangChain Deep Agents
Run a LangChain Deep Agents (deepagents) agent with its file and shell tools in a Platinum microVM, through a sandbox backend in the Python SDK.
Google ADK
Give a Google Agent Development Kit (ADK) agent tools that run shell commands, Python code and file operations in a Platinum microVM.