code_execution_20250825 tool spec that Anthropic’s hosted environment uses. You pass the tool definition to client.messages.create as usual, and when Claude responds with a tool_use block, you route it through handle_code_execution_tool_use instead of Anthropic’s servers. Your data never leaves your compute, and Python REPL state persists across every turn of the conversation.
Anthropic’s Messages API supports a hosted
code_execution tool. Podflare
can replace the hosted execution with a self-hosted Firecracker microVM —
same tool spec, your compute, your data plane.Install
Quickstart
Create a sandbox
Open a
Sandbox context manager. All tool calls within the with block
share one persistent REPL.Add the tool to your messages call
Declare the
code_execution_20250825 tool in the tools list. Claude uses
this spec to decide when to run code.Full example
What handle_code_execution_tool_use does
The function accepts an Anthropic tool_use block — either the SDK object or a plain dict — in the shape:
sandbox.run_code(code) and returns the matching tool_result dict:
is_error is set to true whenever exit_code != 0, so Claude can detect and react to execution failures automatically.
Persistent REPL state across turns
Passing the sameSandbox instance to every handle_code_execution_tool_use call gives Claude a single, continuous Python REPL for the entire conversation. Variables, imports, and loaded files from earlier turns are available in later ones — the single biggest advantage over Anthropic’s hosted container, where state lifetime depends on tool version and model.
Fork-aware conversations
Usesandbox.fork(n=...) between turns to let Claude explore multiple execution paths in parallel, then pick a winner and commit it with merge_into.