Hosted Code Execution Tools in Claude, OpenAI, and Gemini APIs

Compare sandboxed code execution across Anthropic Claude, OpenAI, and Google Gemini APIs: container lifecycles, file workflows, and limits.

By ailog · Published Oct 17, 2026 · 7 min read · How we work
In short
  • Claude provides a containerized Bash shell where Python runs alongside filesystem tools, with optional REPL state retention in newer versions.
  • OpenAI provides isolated Python containers through the Responses API, offering configurable memory sizes from 1g to 64g with 20-minute inactivity timeouts.
  • Gemini executes Python directly via multi-turn interactions, adding native visual code execution in Gemini 3 Flash to crop and inspect images.
  • Anthropic eliminates code execution fees when pairing the sandbox with official web search or fetch tools, leaving only base token fees.

Architectural Concepts: Bash Shell vs Python Containers

Server-side code execution shifts computational workloads from client-side runtime environments into sandboxed infrastructure managed by model providers. While all three platforms deliver isolated execution without external internet connectivity, their operational environments and runtime models differ fundamentally.

Anthropic treats code execution as an integrated system environment. Rather than isolating Python execution to an abstracted script runner, the Claude code execution tool gives models access to an underlying Bash command-line interface and internal filesystem manipulation tools. When Claude processes math or parses datasets, it writes files and executes them via Bash inside an isolated container. In contrast, OpenAI models operate against a dedicated Python container environment configured via the Responses API or Containers endpoint, known internally to the model as the python tool. Google Gemini adopts a turn-based interaction flow where the model emits explicit code execution call steps and consumes the returned text or image results within iterative conversation steps.

The operational boundary determines which languages run natively. Claude executes Bash shell commands and executes Python scripts stored on disk. OpenAI and Gemini restrict code execution exclusively to Python scripts. On Gemini, attempting to run shell scripts or code written in other programming languages fails to execute on the server, even though the model can still generate source code for other languages as regular output text.

Request Schemas and Tool Activation Patterns

Enabling code execution requires adding provider-specific tool declarations to the outgoing payload. In Anthropic Messages API calls, developers specify explicit versioned tool types. The platform maintains three production version strings: code_execution_20250825, code_execution_20260120, and code_execution_20260521. The base version code_execution_20250825 handles standard Bash tasks, while code_execution_20260120 introduces REPL variable persistence and programmatic tool invocation. The code_execution_20260521 definition alters system descriptions to inform models of internal 90-second cell limits. None of these production tool versions require an extra beta header.

curl -sS https://api.anthropic.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-opus-5-5",
    "max_tokens": 4096,
    "messages": [{"role": "user", "content": "Compute sample variance of [1, 2, 3, 4, 5]"}],
    "tools": [{"type": "code_execution_20250825", "name": "code_execution"}]
  }'

In the OpenAI Responses API, the tool is declared under the code_interpreter type name. Developers can pass an auto-configured container configuration directly in the tool declaration or attach a predefined container instance created via the containers endpoint. For complex multi-turn coordination, explore /posts/function-and-tool-calling-across-claude-gpt-and-gemini/.

curl https://api.openai.com/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-6-astra",
    "tools": [{
      "type": "code_interpreter",
      "container": {"type": "auto", "memory_limit": "4g"}
    }],
    "input": "Solve 3x + 11 = 14 using the python tool"
  }'

Google configures code execution through the Interactions endpoint on v1beta. Setting the tool list to include {"type": "code_execution"} allows models like gemini-3.8-flash to emit code_execution_call arguments and process code_execution_result structures iteratively across interaction turns.

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gemini-3.8-flash",
    "input": "Calculate the sum of the first 50 prime numbers with code",
    "tools": [{"type": "code_execution"}]
  }'

Container Lifecycles, Memory Limits, and Timeouts

Resource allocation and session lifespan vary substantially across providers. Anthropic provides sandboxed containers with no network access, preventing runtime pip package installation. A single container’s ID can be carried forward across successive API requests. When using code_execution_20260120 or code_execution_20260521, the internal Python REPL state and memory bindings persist between requests that reuse the container. Under code_execution_20260521, individual Python cells enforce a 90-second wall-clock boundary; exceeding this produces a non-zero return_code and a detection_timeout status message, whereas entire execution timeouts trigger an execution_time_exceeded error.

OpenAI provides fine-grained control over container hardware limits. Through auto mode or the explicit /v1/containers endpoint, developers choose between four discrete RAM allocations: 1g (the default setting), 4g, 16g, or 64g. A container remains active until 20 minutes of inactivity elapse. Any container operation, including reading metadata or modifying attached files, refreshes the container’s last_active_at timestamp. Once the 20 minutes elapse, the container enters an expired state, all active memory states like variables and in-memory Python objects are permanently cleared, and subsequent requests referencing the expired ID fail. Organizations are subject to a rate limit of 100 RPM for code interpreter operations.

On Google Gemini, multi-step code execution operates through interaction graphs linked by previous_interaction_id. The execution state persists across subsequent conversational turns, allowing gemini-3.8-flash to generate multi-step algorithms, observe runtime stdout output, and iteratively correct syntax errors until reaching an answer.

File Input, Output Handling, and Visual Inspection

Real-world data analysis tasks require moving unstructured files into and out of execution environments. Each provider approaches filesystem plumbing with distinct integration patterns. For broader context on shared asset storage, see /posts/files-apis-reusing-uploads-across-claude-gpt-and-gemini/.

Anthropic connects file processing directly with the Anthropic Files API. Users upload CSV, Excel (.xlsx, .xls), JSON, XML, text, or image files (JPEG, PNG, GIF, WebP), receive an upload ID, and insert a container_upload block into the message content. When Claude executes Bash commands or scripts that generate output artifacts, the file IDs appear in the tool execution results, allowing developers to retrieve them via the Files API. Note that Claude Haiku 4.5 (claude-haiku-4-5-20251001) does not support programmatic tool calling or the REPL state persistence built on it, meaning newer tool versions fall back to base code_execution_20250825 behavior on that model.

OpenAI allows direct file attachment through file_ids lists in container definitions or via /v1/containers/{container_id}/files. When Code Interpreter produces data files or visual plots, the model includes container_file_citation elements within message annotations. These annotations detail the container_id, file_id, and filename, which can then be fetched via container file content retrieval endpoints. Supported container file extensions span data formats, source code, archives, and documents, including .csv, .xlsx, .json, .pdf, .png, .jpeg, .tar, and .zip.

Gemini introduces multimodal execution in gemini-3.8-flash, termed code execution with images. When paired with Thinking, the model inspects images supplied via base64 encoding or URLs. Instead of relying solely on raw vision representations, the model writes Python scripts using image processing libraries to zoom into fine details, crop specific bounding coordinates, rotate orientations, or perform visual math like summing receipt line items. Generated image outputs can be emitted as image content steps directly in the interaction stream.

Pricing Implications and Token Cost Considerations

Code execution alters the token profile of API interactions because generated scripts, execution logs, stdout streams, and error tracebacks consume input and output tokens. For modeling custom infrastructure budgets across token types, consult /tools/llm-api-cost-calculator/ alongside the live token rates on /tools/llm-api-pricing/.

Anthropic provides an explicit pricing rule for combined tool usage: code execution is entirely free when invoked alongside official web tools (web_search_20260209, web_fetch_20260209, or later versions). In these blended requests, users pay standard model token rates for claude-opus-5-5 ($4 input, $0.20 cached input, $20 output per 1M tokens) or claude-sonnet-5-5 ($2 input, $0.20 cached input, $10 output per 1M tokens), while code execution containers incur no tool fees. Furthermore, dynamic filtering inside web search runs in automatically provisioned containers without requiring an explicit code tool configuration.

OpenAI bills Code Interpreter according to built-in tools pricing and the chosen container memory tier (1g, 4g, 16g, or 64g), alongside base token pricing for models like gpt-6-astra ($10 input, $1 cached input, $50 output per 1M tokens) or gpt-6.1-sol ($2 input, $0.10 cached input, $10 output per 1M tokens). On Google Gemini, operations using gemini-3.8-flash incur token charges at $0.75 per 1M input tokens, $0.075 per 1M cached input tokens, and $3.75 per 1M output tokens as execution code and inspection steps populate the interaction context. To protect against runaway loops, implement client guardrails as detailed in /posts/per-run-cost-cap-for-llm-api-calls/.

Feature Comparison: Claude vs OpenAI vs Gemini

The following table contrasts the technical limits, runtime environments, and file handling mechanisms across the three provider implementations based on official specifications.

Feature Dimension Anthropic Claude API OpenAI Responses API Google Gemini API
Supported Runtime Bash shell container with pre-installed Python Python sandbox environment (python tool) Python execution environment only
Tool Declaration Type code_execution_20250825, code_execution_20260120, code_execution_20260521 code_interpreter code_execution
Container Sizing Provider-managed container Configurable memory tiers: 1g, 4g, 16g, 64g Provider-managed runtime
Lifecycle & Timeouts 90-second cell limit (code_execution_20260521); reusable container ID 20-minute inactivity expiration window; 100 RPM org rate limit Chained turns via previous_interaction_id
File Operations container_upload content blocks via Files API; downloadable result IDs container_file_citation annotations; /v1/containers/{id}/files API Base64 image ingestion; output images in interaction steps
Distinctive Capability Free tool usage when combined with web_search_20260209 or web_fetch_20260209 Explicit container management endpoint with RAM sizing up to 64g Gemini 3 Flash automated visual crop, zoom, and inspection

Implementation Checklist for Production Workflows

When building production agent workflows that rely on hosted code execution, verify these implementation requirements across your target providers:

  • Model validation: Query capabilities.server_tools.code_execution.supported before calling Claude models, and account for claude-haiku-4-5-20251001 falling back to base execution without persistent REPL state.
  • Container reuse: Store returned container IDs across turns to preserve cached files and Python variables instead of instantiating new execution sandboxes on every request.
  • Inactivity management: For OpenAI containers, schedule operations within the 20-minute sliding window or re-upload source assets once containers enter expired status.
  • Cell budgeting: When using Anthropic tool version code_execution_20260521, instruct models to structure scripts to complete within the 90-second execution window to avoid detection_timeout returns.
  • File extraction: Parse container_file_citation metadata from OpenAI message annotations and tool result file IDs from Claude outputs to download charts or CSVs before containers expire.
  • Visual inspection: On gemini-3.8-flash, pair the code execution tool with Thinking mode when processing high-resolution diagrams to enable automated code-driven image zooming.
Sources
  1. Code execution tool
  2. Code Interpreter
  3. https://ai.google.dev/gemini-api/docs/code-execution

Related