Code Execution Tool

Execute Python code in a sandboxed environment with Anthropic's code execution tool.

With Anthropic's code execution tool, your model can execute Python code in a secure, sandboxed environment. This is useful for your model to perform tasks as analyzing data, creating visualizations, or performing complex calculations.

Working example

1from kern.agent import Agent
2from kern.models.anthropic import Claude
3
4agent = Agent(
5 model=Claude(
6 id="claude-sonnet-4-20250514",
7 betas=["code-execution-2025-05-22"],
8 ),
9 tools=[
10 {
11 "type": "code_execution_20250522",
12 "name": "code_execution",
13 }
14 ],
15 markdown=True,
16)
17
18agent.print_response(
19 "Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
20 stream=True,
21)