Code execution: Claude runs the code it writes
Code execution lets Claude write Python, run it on Anthropic's side, and answer from what it prints.
The demo in this video was recorded in September 2026. It ran on Claude Sonnet 5 (claude-sonnet-5). Results may differ on newer models.
Some questions need math across many files. That's a job for code.
Take Meridian, a fictional engineering consultancy. It has 12 CSV files, one for each pump station, and each file holds a year of flow readings. It wants to know whether any station ever went over its design capacity, and if so, when.
Code execution is how Claude does that job. Claude gets a container where it can run the Python it writes and answer from what the code prints.
Add the tool to your request
Code execution is a server tool, a tool that runs on Anthropic's infrastructure. You write no handler for it, and your code sends back no tool_result.
Adding code execution takes one entry in your tools list. The date at the end of the entry's type is the tool's version, and Meridian uses code_execution_20260120. The current versions are listed in the Tool reference(opens in new tab), so copy the type value from there.
Meridian keeps the list in a constant named TOOLS, and its request passes it as tools=TOOLS:
TOOLS = [{"type": "code_execution_20260120", "name": "code_execution"}]As of October 2026, code execution(opens in new tab) runs on the Claude API, Claude Platform on AWS, the Claude models on Google Cloud that Anthropic manages, and Microsoft Foundry deployments hosted on Anthropic.
Put your files in the container
Claude's code runs in a container on Anthropic's side. Your files are still on your machine, so the container doesn't have them yet. Meridian's program gets them into the container in two steps:
- Upload each file with the Files API(opens in new tab), which gives you back a file ID.
- Put that file ID in your message, in a
container_uploadblock.
Meridian's program adds one container_upload block for each file, in the same message as its question. The files are then loaded onto the container, where Claude's code can open them.
Read Claude's answer
Your code sends one request and usually gets back the finished answer.
The API can pause a long-running turn and return a stop_reason of pause_turn. When it does, send the response's content back as an assistant message, with the same tools, and Claude continues.
Claude's answer for Meridian is that Station 7 went over its design capacity of 600 L/s twice, in April and May 2025. In the response, the answer comes after the commands Claude ran.
Put the program together
Meridian's program below adds the tool, puts the files in the container, and reads the answer. In the program, paths holds the 12 CSV files as pathlib.Path objects, and QUESTION holds Meridian's question.
Key takeaways
- Code execution is a server tool. You add one entry to
tools, and Claude's code runs in a container on Anthropic's side. - You upload each file with the Files API, then put its file ID in a
container_uploadblock.