← ContentsClaude API · advanced

Code execution tool (API)

The code execution tool is an API feature that lets Claude run Bash commands and manipulate files inside a secure, sandboxed environment. Developers enable it by declaring a specific tool type in their API request, after which Claude can write and execute code across multiple programming languages, read and write files, install packages, and chain shell commands — all without the developer managing a separate execution runtime. The sandbox runs on Anthropic-managed infrastructure. Each request can spin up a fresh container or reuse a container from a prior request (via container ID), which lets state, files, and installed packages persist across multi-turn conversations. The tool is distinct from Claude's ability to write code as text: code execution actually runs the code and returns real output, errors, and generated files. The tool is aimed at API developers building agentic or data-processing workflows. Typical applications include automated data analysis, report generation, environment setup, multi-step Bash automation, and orchestrated multi-tool pipelines. It is not the same as the code execution available in Claude.ai chat; this entry covers only the API integration.

When you’d use it

  • Automated data analysisA developer uploads a CSV of sales records via the Files API and asks Claude to compute monthly totals, identify outliers, and write a summary report. Claude writes Python, executes it in the sandbox, and returns the computed statistics and a text report file.
  • Multi-language build and test pipelinesAn engineering team wants Claude to scaffold a new microservice, install dependencies with pip or npm, run unit tests, and return pass/fail results. Claude issues Bash commands inside the container to complete each step and reports the test output.
  • Configuration file generation and validationAn infrastructure team asks Claude to generate a YAML or JSON config from a template, patch specific values, and verify the result with a linter or grep. Claude chains file writes and Bash validation commands, returning the confirmed config.
  • Agent Skills for office document generationUsing the Agent Skills framework (launched January 2026), Claude reads tabular data and generates professionally formatted Excel spreadsheets with formulas, PowerPoint presentations, or Word documents. The code execution sandbox provides the secure environment skills run in.
  • Programmatic tool calling in multi-tool agentic workflowsInstead of looping Claude through every intermediate tool call, a developer enables programmatic tool calling so Claude writes a Python orchestration script that calls external tools from inside the sandbox. This reduces latency and token usage in complex pipelines with many tool invocations.

What changed recently

  • 2025-08Code Execution Tool v2 released in public beta (tool type: code_execution_20250825). Replaced the Python-only v1 (code_execution_20250522) with a Bash-capable, multi-language environment that also supports file read/write operations. No price difference between versions.
  • 2025-10Code execution made free when used alongside the web search or web fetch tools in the same API request, reducing costs for agents that combine web retrieval with computation.
  • 2026-01-06Agent Skills launched. Skills use the code execution sandbox as their secure runtime and enable Claude to read and generate Excel spreadsheets (with formulas), PowerPoint presentations, Word documents, and fillable PDFs via Anthropic-managed skill definitions.
  • 2026-02Security hardening disclosed: a vulnerability in Claude Code allowed project-local configuration hooks to execute before the user trust prompt was shown. Fixed so that project-local config is not parsed until after trust is established. Separately, prompt injection risks in the code execution sandbox were documented; a prompt injection classifier was added to detect and halt malicious execution attempts.
This is the short version

The full chapter has three worked examples, the common pitfalls, and the workflow that makes it pay — plus the other 84 features, kept current.

Get Claude Master — $97
CLAUDEMASTER
An independent publication.
Independent product. Not affiliated with or endorsed by Anthropic. “Claude” is a trademark of Anthropic, used here only to describe the subject of this manual.
© 2026 Claude Master — All rights reserved.