Code Execution Tool
Run Python and Bash in Anthropic's server-side sandbox from an Agno agent.
from agno.agent import Agent
from agno.models.anthropic import Claude
agent = Agent(
model=Claude(id="claude-sonnet-4-5-20250929"),
tools=[
{
"type": "code_execution_20250825",
"name": "code_execution",
}
],
markdown=True,
)
if __name__ == "__main__":
agent.print_response(
"Use the code execution tool to calculate the mean and standard deviation of "
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].",
stream=True,
)code_execution_20250825 is generally available and does not need a beta header. It gives Claude Bash, file operations, and Python through the sandbox. During streaming, tool input can arrive incrementally, but each execution-result block arrives as a complete block.
Anthropic offers code execution on the Claude API, Claude Platform on AWS, and Hosted-on-Anthropic Microsoft Foundry deployments. It is unavailable on Amazon Bedrock and Google Cloud.
Code execution is not eligible for Zero Data Retention. Anthropic retains container data, including uploaded files and execution output, for up to 30 days. Files created in the Files API remain until they are deleted.
See Anthropic's code-execution documentation for model compatibility, runtime limits, pricing, and retention details.
Run the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U "agno[anthropic]"Set your API key
export ANTHROPIC_API_KEY="your_anthropic_api_key"Save and run
Save the example as code_execution.py, then run:
python code_execution.py