Code execution is a server tool: unlike the bash or text editor tools, where your application runs the commands, here Anthropic hosts the runtime. You add the tool to a request, Claude writes Python, the code runs in a managed container, and the outputs come back inside the same API response. For a data question like "which region's refund rate changed most quarter over quarter?", Claude writes pandas code, executes it, reads the printed result, and answers from the actual numbers instead of estimating.
Declaring the tool and choosing a version
You declare it as {"type": "code_execution_20250825", "name": "code_execution"} — no input schema needed. Three versions are generally available with no beta header: code_execution_20250825 (the baseline), code_execution_20260120 (adds REPL state persistence and programmatic tool calling), and code_execution_20260521 (the same runtime, but the tool description tells Claude about the 90-second wall-clock limit per Python cell). Within the container, Claude also gets two sub-tools — bash_code_execution for shell commands and text_editor_code_execution for viewing and editing files — so it can behave like a small self-contained workbench.
What the sandbox looks like
| Property | Value |
|---|---|
| Runtime | Python 3.11 on Linux x86_64 |
| Resources | 1 CPU, 5 GiB RAM, 5 GiB disk |
| Network | No internet access; no runtime package downloads |
| Isolation | Scoped to the API key's workspace |
| Lifetime | Checkpointed after ~5 minutes idle; restorable by container ID for 30 days |
The no-internet rule is a security feature, not a limitation to route around: whatever data Claude analyzes must be data you deliberately passed in. The container comes pre-loaded with the usual analysis stack — pandas, numpy, scipy, scikit-learn, statsmodels, matplotlib, seaborn, sympy and more — plus command-line utilities like unzip, rg, and sqlite.
Data in, results out
Small inputs can simply go in the prompt. For real files, upload via the Files API (beta header files-api-2025-04-14) and reference the file ID in a container_upload content block; the file appears in the container's filesystem for Claude's code to open. In the other direction, files Claude creates — a cleaned CSV, a chart PNG — are returned as file IDs you download through the Files API. For structured results, the simplest pattern is to ask Claude to print JSON and also state the answer in prose; execution output (stdout, return codes, errors) is captured in the tool-result blocks of the response, so failures are visible rather than silent. Watch for the documented error codes such as execution_time_exceeded and output_file_too_large.
from anthropic import AnthropicAWS # needs AWS_REGION and
# ANTHROPIC_AWS_WORKSPACE_ID env vars
client = AnthropicAWS()
resp = client.messages.create(
model="claude-opus-4-8",
max_tokens=4096,
tools=[{"type": "code_execution_20260120", "name": "code_execution"}],
messages=[{"role": "user",
"content": "Simulate 10k coin flips and report the "
"longest run of heads as JSON."}],
)
Pricing
Code execution is free whenever the request also includes the current web tools (web_search_20260209 / web_fetch_20260209 or later). Otherwise it bills by execution time with a 5-minute minimum per session, after a generous allowance: 1,550 free hours per organization per month, then $0.05 per hour per container. One trap worth knowing: files included in a request incur execution time even if the tool is never invoked. Code execution also works inside the Message Batches API at the same price. Note it is not eligible for Zero Data Retention.
Platform availability — the sharpest 3P divide
This is where deployment choice bites. Code execution is generally available on the first-party Claude API and on Claude Platform on AWS, and in beta on Microsoft Foundry (Hosted on Anthropic deployments). It is not available on Amazon Bedrock or Google Vertex AI. If sandboxed execution is central to your workload and you are AWS-committed, Claude Platform on AWS is the practical route; on Bedrock or Vertex you would run model-generated code in your own sandbox via the client-side bash tool instead.
Where to go next
See the Files API article for the upload/download mechanics, and Skills, which build on this same container. The feature matrix summarizes availability.