Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.podflare.ai/llms.txt

Use this file to discover all available pages before exploring further.

Anthropic’s Messages API supports a hosted code_execution tool. Podflare can replace the hosted execution with a Podflare microVM — same tool spec, your compute, your data plane.

Install

pip install podflare anthropic

Use

from anthropic import Anthropic
from podflare import Sandbox
from podflare.integrations.anthropic import handle_code_execution_tool_use

client = Anthropic()

with Sandbox() as sbx:
    messages = [{"role": "user", "content": "What's sqrt(12345)?"}]
    while True:
        resp = client.messages.create(
            model="claude-opus-4-7",
            max_tokens=1024,
            tools=[{"type": "code_execution_20250825", "name": "code_execution"}],
            messages=messages,
        )
        messages.append({"role": "assistant", "content": resp.content})
        if resp.stop_reason != "tool_use":
            break

        tool_results = [
            handle_code_execution_tool_use(block, sbx)
            for block in resp.content
            if getattr(block, "type", None) == "tool_use"
            and getattr(block, "name", None) == "code_execution"
        ]
        messages.append({"role": "user", "content": tool_results})

    final = resp.content[-1].text
    print(final)

What handle_code_execution_tool_use does

Takes an Anthropic tool_use block (SDK object or dict) with {type: "tool_use", id: ..., name: "code_execution", input: {code: "..."}} and returns the matching tool_result:
{
  "type": "tool_result",
  "tool_use_id": "toolu_xxx",
  "content": [{"type": "text", "text": "stdout:\n...\nstderr:\n...\nexit_code: 0"}],
  "is_error": false
}
is_error: true when exit_code != 0, so Claude can pick up execution failures.

State across turns

Passing the same Sandbox into multiple handle_code_execution_tool_use calls gives Claude a persistent Python REPL across the whole conversation. That’s the single biggest win over Anthropic’s hosted container, where REPL state depends on tool version + model.

Fork-aware conversations

You can also parent.fork(n=...) between turns to let Claude explore multiple paths, pick a winner, and merge_into to commit — all transparently to the model.
# Before forking:
parent_messages = [...]

children = sbx.fork(n=3)
try:
    # Send parent_messages down three branches, each with its own sandbox.
    ...
finally:
    for c in children:
        c.close()