Workspaces
A workspace is where your agent runs commands and reads and writes files: a directory on your machine,
a container, or a cloud sandbox. Tools reach it through ctx.workspace,
so the same tools work in any of them, and you switch by changing one capability.
import asyncio
from pydantic_ai import Agent, ModelRetry, RunContext
from pydantic_ai.capabilities import LocalWorkspace
from pydantic_ai.workspaces import WorkspaceError
agent = Agent(
'anthropic:claude-sonnet-5-5',
capabilities=[LocalWorkspace('.')],
)
@agent.tool
async def execute(ctx: RunContext, command: list[str]) -> str:
"""Run a command in the project directory."""
try:
result = await ctx.workspace.run(command, timeout=60)
except WorkspaceError as error: # e.g. a timeout, or a read-only workspace
raise ModelRetry(str(error))
return result.stdout if result.exit_code == 0 else f'[exit {result.exit_code}] {result.stderr}'
async def main() -> None:
result = await agent.run('Write fizzbuzz to fizzbuzz.py and run it.')
print(result.output)
#> fizzbuzz.py is written and runs clean.
if __name__ == '__main__':
asyncio.run(main())
LocalWorkspace('.')gives every run the current directory. Commands start there and relative paths resolve against it. The directory must already exist.ctx.workspace.run(...)returns the command’sexit_code,stdoutandstderr. A failing command is a normal result, so the model sees what went wrong.timeout=60stops a stuck command with aWorkspaceTimeoutError, which carries the output so far. The clock starts once the sandbox is ready, so creating it doesn’t count.- Turning any
WorkspaceErrorintoModelRetrylets the model try something else. Without it, the error ends the run.
You rarely write these tools yourself: Coder, Shell and
FileSystem from the Pydantic AI Harness give the model file, search and shell
tools that work in whatever workspace the run has.
LocalWorkspace runs the model’s commands on your machine, as you. To run them somewhere isolated, swap
it for a sandbox. Your tools, such as the execute tool above or Coder here, now run there:
from pydantic_ai import Agent
from pydantic_ai_harness.coder import Coder
from pydantic_ai_harness.e2b_sandbox import E2BSandbox
agent = Agent('anthropic:claude-opus-5-5', capabilities=[E2BSandbox(), Coder()])
result = agent.run_sync('Clone https://github.com/pydantic/pydantic-ai and run its tests.')
The sandbox is created the first time the run uses it:
- With
Coder, that’s when the run starts, becauseCoderloads the repository’s instructions. PassCoder(repo_context=False)to wait for the first tool call instead. - A durable run also creates it when the run starts.
The sandbox keeps running after the run until you delete it or its provider stops it. Cleaning up shows how to handle that.
LocalWorkspace and WindowsWorkspace come with pydantic-ai. The rest come with
Pydantic AI Harness, each behind its own install extra.
| Workspace | Where commands run | Isolated from your machine | You need |
|---|---|---|---|
LocalWorkspace | A directory on this machine (macOS, Linux) | No | Nothing |
WindowsWorkspace | A directory on this machine, in PowerShell | No | Windows |
| Docker Container | A local Docker or Podman container | Yes, sharing your machine’s kernel | Docker or Podman |
| Smol Sandbox | A smolvm microVM on this machine | Yes | Apple silicon Mac, or Linux with KVM |
| BoxLite Sandbox | A BoxLite micro-VM on this machine, with an optional egress allow-list | Yes | Apple silicon Mac, or Linux with KVM |
| OpenSandbox | A sandbox on your own OpenSandbox server, on Docker or Kubernetes | Yes | An OpenSandbox server |
| Kubernetes Sandbox | A pod from an agent-sandbox warm pool | Yes | A cluster with agent-sandbox |
| Modal Sandbox | A Modal cloud sandbox | Yes | A Modal account |
| E2B Sandbox | An E2B cloud sandbox | Yes | An E2B account |
| Daytona Sandbox | A Daytona cloud sandbox | Yes | A Daytona account |
| Render Sandbox | A Render sandbox | Yes | A Render workspace with Sandboxes access |
| Sprites Sandbox | A persistent Fly.io Sprite | Yes | A Sprites API token |
| boxd Sandbox | A boxd cloud machine | Yes | A boxd account |
| SSH Workspace | Another machine, over your ssh client | Yes, on another host, but not sandboxed there: commands run as your SSH user | SSH access |
| Bubblewrapped Workspace | Wraps another workspace and runs its commands in Linux bwrap | Best effort, not a security boundary | Linux with bwrap on that host |
Other packages implement the same interface:
pydantic-ai-backend, a community package by
Vstorm, provides workspaces backed by Kubernetes pods, Docker, Daytona, a sandboxd service that holds the
Docker socket for you, and in-memory documents you keep in your own database. To list a package here,
open a pull request.
For anything else, write a backend: one class that runs a command, plus a test suite that checks it behaves like the others.
Custom Workspaces also shows how to:
- Supply a workspace from a capability to give each user their own.
- Mark who wrote its files, for the whole workspace or by path, so the model doesn’t take a third-party file’s text as your instructions.
- Wrap a workspace to add a policy of your own.
LocalWorkspace isolates nothing: a command can read and change anything you can, and the directory only
sets where commands start. Use it for your own, trusted work, and a sandbox for code you don’t trust.
A sandbox is a container or a VM. A container shares your machine’s kernel, so a kernel flaw can let a command escape it. A VM runs its own kernel, which makes it the stronger boundary for code you don’t trust. Docker’s Containers are not VMs explains the difference.
Commands in a LocalWorkspace get your PATH, HOME, LANG, LC_ALL and LC_CTYPE, and nothing else
from the agent’s environment, so your API keys don’t reach the commands the model runs. Pass what they
need with env=, never the whole os.environ:
from pydantic_ai import Agent
from pydantic_ai.capabilities import LocalWorkspace
agent = Agent(
'anthropic:claude-sonnet-5-5',
capabilities=[LocalWorkspace('.', env={'UV_OFFLINE': '1'})],
)
ctx.workspace has the same methods in every workspace:
runruns a command.read_textandwrite_textread and write text.read_bytesandwrite_bytesread and write exact bytes.- Writing creates missing parent directories.
list_dir,stat,exists,make_dirandremovework with directories and entries.
from pydantic_ai import ModelRetry, RunContext
from workspace_agent import agent
@agent.tool
async def read_source(ctx: RunContext, path: str) -> str:
"""Read a text file from the project."""
try:
return await ctx.workspace.read_text(path)
except FileNotFoundError:
raise ModelRetry(f'{path} does not exist.')
@agent.tool
async def save_notes(ctx: RunContext, notes: str) -> str:
"""Save notes for later steps."""
await ctx.workspace.write_text('NOTES.md', notes)
return 'Saved to NOTES.md.'
- Relative paths resolve against the working directory. It is a starting point, not a boundary:
..and absolute paths reach the rest of the environment. run(..., cwd='subdir')starts one command in another directory, relative to the working directory. The directory must already exist.- Sandbox providers use different default users and directories, so prefer relative paths, or set
working_dir=on the sandbox. - A bad path raises the usual error, such as
FileNotFoundErrororIsADirectoryError. RaiseModelRetryfrom it so the model can try again. Uncaught, it ends the run. On Temporal, it follows the retry policy instead. - An environment that is gone, such as a sandbox deleted during a command, raises
WorkspaceUnavailableErrorand ends the run. A command killed by a signal while its environment stays live returns its exit code instead.
Pass the message history to the next run, and it continues in the same workspace. With a sandbox, the files and installed packages from the first run are still there:
from workspace_agent import agent
async def main() -> None:
first = await agent.run('Write fizzbuzz to fizzbuzz.py and run it.')
await agent.run('Now add a test for it.', message_history=first.all_messages())
This works because each model response records which workspace it ran in. The record is a
WorkspaceRef, a reference (or “ref”) holding the provider’s name and
the environment’s ID, stored on workspace_ref. It is
saved with the messages, so this works however you store them:
from pydantic_ai import ModelMessagesTypeAdapter
from workspace_agent import agent
async def main() -> None:
first = await agent.run('Write fizzbuzz to fizzbuzz.py and run it.')
print(first.response.workspace_ref == first.workspace.ref)
#> True
saved = first.all_messages_json() # store it in your database
history = ModelMessagesTypeAdapter.validate_json(saved)
await agent.run('Now add a test for it.', message_history=history)
To continue without the history, for example in a new conversation about the same project, save the
reference and pass it as workspace=:
from workspace_agent import agent
async def main() -> None:
first = await agent.run('Write fizzbuzz to fizzbuzz.py and run it.')
ref = first.workspace.ref # a small dataclass: `provider` and `id`
await agent.run('Explain what fizzbuzz.py does.', workspace=ref)
- A sandbox’s
refisNoneuntil the run first uses it. A run that never touches the workspace records no reference, and a later turn that doesn’t use it keeps the earlier one. - If the sandbox a reference names is gone, the run raises
WorkspaceUnavailableErrorinstead of starting over in an empty one. Passworkspace='new'to start over on purpose. sanitize_messagesand the UI adapters strip references from history a client sends you, so a client can’t choose where your agent works. Save the reference on your server and pass it asworkspace=.
Pydantic AI never deletes an environment. A sandbox keeps running, and billing, after the run until you delete it or its provider’s lifetime settings stop it. Each sandbox page shows how to delete one.
- After a successful run that used the sandbox,
result.workspace.refnames it, so you can continue in it or delete it. If the ref isNone, the run never created a sandbox, and callingresult.workspacemethods creates a new one that no message records. - A failed run returns no result. Delete its sandbox in an
on_run_errorhook, wherectx.workspace.refnames it.after_rundoesn’t run when a run fails. - If you delete the sandbox after each run, pass
workspace='new'when you continue its history, or the next run raisesWorkspaceUnavailableError.
workspace= also takes a live workspace. Here the coding agent asks a reviewer to check its work. The
reviewer reads the same files, and ReadOnlyWorkspace stops
it from changing them:
from pydantic_ai import Agent, RunContext
from pydantic_ai.workspaces import ReadOnlyWorkspace
from workspace_agent import agent
reviewer = Agent('anthropic:claude-opus-5-5', instructions='Review code. Be brief.')
@reviewer.tool
async def read_file(ctx: RunContext, path: str) -> str:
return await ctx.workspace.read_text(path)
@agent.tool
async def ask_reviewer(ctx: RunContext, request: str) -> str:
"""Ask a reviewer to check files in this workspace."""
review = await reviewer.run(request, workspace=ReadOnlyWorkspace(ctx.workspace), usage=ctx.usage)
return review.output
async def main() -> None:
result = await agent.run('Write fizzbuzz to fizzbuzz.py and run it.')
result = await agent.run('Ask the reviewer to check fizzbuzz.py.', message_history=result.all_messages())
print(result.output)
#> The reviewer says fizzbuzz.py is correct.
The reviewer has no workspace capability of its own, so it works wherever it is told to: a local checkout in one run, a sandbox in the next.
Pass read_only=True to let tools read and list files while refusing commands and changes:
from pydantic_ai import Agent
from pydantic_ai.capabilities import LocalWorkspace
agent = Agent('anthropic:claude-sonnet-5-5', capabilities=[LocalWorkspace('.', read_only=True)])
A refused change raises WorkspaceReadOnlyError.
Commands are refused too, because a command can change files. A capability can check
ctx.workspace.read_only to leave its write tools out.
For a single run, pass workspace=ReadOnlyWorkspace(...), as the
reviewer above does. For a policy of your own, see
Wrapping a workspace.
A run uses the first of these that applies:
- The
workspace=argument. - The reference on the latest response in
message_history. - The agent’s workspace capability: its directory for
LocalWorkspace, a new sandbox for a sandbox.
workspace= takes one of:
'new', to start fresh and ignore the history.- A
WorkspaceRef, for the agent’s capability to attach to. - A live workspace, such as
result.workspaceorctx.workspace, or a backend, used as is.
To move to a new directory or provider without breaking old conversations, list both capabilities. They are asked in order, and only the one that recognizes a reference attaches to it. New conversations use the first. Conversations that started in the second continue there.
from pydantic_ai import Agent
from pydantic_ai.capabilities import LocalWorkspace
agent = Agent(
'anthropic:claude-opus-5-5',
capabilities=[
LocalWorkspace('~/projects/current', id='current'), # new conversations
LocalWorkspace('~/projects/archive', id='archive'), # conversations that started here
],
)
LocalWorkspace only attaches to a reference to its own directory, so message history can’t point the
agent at another directory on your machine. The directory is compared as written, without resolving
symlinks, so if you change ~/proj to the path it links to, old conversations no longer match.
When no capability supplies the workspace:
- A reference in
message_historythat no capability recognizes raisesUserError, for example after you switch providers. Passworkspace='new', or keep the old capability. An agent with no workspace capability, such as one that summarizes the conversation, ignores the reference. workspace=with aWorkspaceRefor'new'raisesUserError.- Without a reference, the run has no workspace, and tools that use it raise
WorkspaceUnavailableError.
In tests, with agent.override(workspace=LocalWorkspaceBackend(tmp_path)): puts every run in that directory,
whatever the agent’s capability, the history or workspace= say. LocalWorkspaceBackend is the class that
does LocalWorkspace’s work, without the capability around it.
Under Temporal, DBOS or Prefect, attach the workspace capability next to the durability capability:
from pydantic_ai import Agent
from pydantic_ai.capabilities import LocalWorkspace
from pydantic_ai.durable_exec.temporal import TemporalDurability
agent = Agent(
'anthropic:claude-sonnet-5-5',
name='fizzbuzz', # names the agent's Temporal activities
capabilities=[LocalWorkspace('.'), TemporalDurability()],
)
Swap TemporalDurability for DBOSDurability or PrefectDurability and nothing else in the agent changes.
The engine guides show where each run starts.
Tools use ctx.workspace as in a plain run:
- Harness tools such as
Coder,ShellandFileSystemwork on all three engines, including background jobs, stickycdandFileChangeRequestedEventapproval. - On Temporal, live events and delegation have limits.
FileSystemand theshelltool also work under Azure Durable Task and Render Workflows.
An engine normally runs each tool call as one recorded unit: a Temporal activity, a DBOS step or a Prefect
task. A tool that must run in the workflow code itself, for example to approve a file change before writing
it, can set durable=False. The
workspace calls it makes are still recorded as activities, steps or tasks.
- A durable run creates or attaches its environment when it starts, even if no tool uses it. Retries, replays and recovery reattach to it.
- If a worker dies after creating a sandbox but before that is recorded, the retry can create a second one. Delete the first with the provider’s tools.
- Without an explicit
run_id, a durable run takes its ID from the Temporal run ID, DBOS workflow ID or Prefect flow run ID, so workspace state such as a stickycdor a background job survives a worker restart. - On Temporal,
LocalWorkspacecalls run on whichever worker picks them up, so every worker needs the directory at the same absolute path, on shared storage.
- A workspace call can run again if a worker dies during it.
- Timeouts, output-limit failures, read-only refusals and a lost environment are not retried.
- Other errors, such as
FileNotFoundError, follow the engine’s retry policy. On Temporal that retries without limit by default. RaiseModelRetryfrom them, or setmaximum_attemptson the retry policy. - On Temporal, a command must finish within its activity’s
start_to_close_timeout, 60 seconds by default, whatever its owntimeout. Raise it for a tool withmetadata={'temporal': ActivityConfig(start_to_close_timeout=...)}. - Workspace calls from workflow code use
activity_config. If you set it, keep astart_to_close_timeoutin it. - On DBOS, tool calls in a run with a workspace run one at a time, so their steps replay in order.
- A sticky
cdis kept in the workspace under.pydantic-ai-harness/shell/run-state/, keyed by the run, so a new worker continues in the same directory. It is removed when the run ends. - Commands in one run that change directory at the same time can overwrite each other’s state.
- A retried
start_commandreattaches to its job instead of starting a second process. - If a launch failed partway, the retry reports a pending launch. Once the process has stopped, remove its
files under
.pydantic-ai-harness/shell/.
Each step rebuilds the workspace from the agent’s capability, and workspace= passes only the reference.
Anything you wrap around a workspace for one run would be dropped, so:
- Put policy on the capability, such as
LocalWorkspace(..., read_only=True)orLocalWorkspace(..., authority='none'). - A
ReadOnlyWorkspace(...)orAuthorityWorkspace(...)passed to the run raisesUserError, unless the capability already applies the same policy. - A live workspace passed to the run that has no ref yet raises
UserError. A previousresult.workspacewithout a ref is the exception: it starts a fresh workspace. - A workspace capability passed to the run raises
UserErrorif it selects a different kind of workspace or policy. Its other settings, such as a sandbox image, are ignored. - Workspace calls made from workflow code store their arguments,
env=and file contents included, in durable history. Keep secrets in the capability’senv=or use them inside a tool. workspace.backendis not available in workflow code, which includes DBOS function tools and tools withdurable=False. Reach the provider’s own API from a tool that runs as an activity or step.- The deprecated
TemporalAgent,DBOSAgentandPrefectAgentrefuse a workspace. Use the durability capability instead.
LocalWorkspace needs macOS or Linux. On Windows, use
WindowsWorkspace. It takes the same options and isolates
nothing either.
- Commands run in
pwshwhen it is onPATH, and in Windows PowerShell otherwise. - Commands get only the variables Windows tools need to start, plus
env. - A timeout or cancellation ends the processes a command started.
from pydantic_ai import Agent
from pydantic_ai.capabilities import WindowsWorkspace
agent = Agent('anthropic:claude-sonnet-5-5', capabilities=[WindowsWorkspace('.')])
A tool that builds paths or shell commands itself can read
ctx.workspace.platform and use
ctx.workspace.paths (posixpath or ntpath).
- A run has one workspace.
- A local command whose combined output passes 10 MiB raises
WorkspaceOutputLimitError, with the first 64 KiB of each stream. Redirect large output to a file. - Commands get stdin at EOF, so use non-interactive flags such as
-y. LocalWorkspacebackground processes outlive the run. Redirect their output to a file so the command doesn’t wait for them, and stop them before your program exits.- Run spans record the workspace id, a
LocalWorkspace’s directory path included, even withinclude_content=False. - Realtime sessions don’t select a workspace yet.