Migration: Anthropic Code Execution Tool to AgentCore Code Interpreter

Overview

This snippet shows how to migrate from the Anthropic API Code Execution tool to Amazon Bedrock + AgentCore Code Interpreter. It provides side-by-side working examples: the original Anthropic approach (one API call, everything handled server-side) and multiple equivalent implementations on AWS.

All examples (00-04, 06) use the same prompt (a simple math calculation) — “Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]” — so you can directly compare the code and output across approaches.

Key Architectural Difference

Anthropic AWS (Bedrock + AgentCore)
One API call does everything Two services composed together
No agentic loop needed Orchestration layer required (or use Strands)
No session management Explicit session start/stop lifecycle

Tags

migration, code-interpreter, bedrock, agentcore, anthropic, strands, prompt-caching, claude-code, programmatic-tool-calling

Technologies

Difficulty

Intermediate

Sample Files

File Approach Description
00_anthropic_code_execution.py Anthropic API BEFORE — The original. One API call, Claude handles code execution server-side. No loop, no session management.
01_converse_api_with_code_interpreter.py Converse API + AgentCore Custom agentic loop using the AWS-recommended Converse API. Model-agnostic format that works across Claude, Nova, etc.
02_invoke_model_with_code_interpreter.py InvokeModel API + AgentCore Custom agentic loop using native Anthropic Messages format. Easiest to port from existing Anthropic code.
03_strands_agent_with_code_interpreter.py Strands SDK + AgentCore RECOMMENDED — Strands handles the agentic loop automatically. Closest to the Anthropic “one call” simplicity.
04_code_interpreter_highlevel_api.py AgentCore SDK (direct) Execute code directly without a model. Useful for batch jobs, testing, or when you already know what code to run.
05_claude_code_in_code_interpreter/ Claude Code in sandbox Run Claude Code CLI inside a Code Interpreter sandbox. Requires a custom Code Interpreter with PUBLIC network mode and an IAM execution role. See 05 README.
06_invoke_model_with_caching.py InvokeModel + Prompt Caching Demonstrates Bedrock prompt caching to reduce cost and latency in agentic loops. Uses a regional model ID (required for caching).
07_programmatic_tool_calling.py Programmatic Tool Calling Demonstrates the programmatic tool calling pattern on Bedrock: pre-loads tools into the sandbox so the model can call multiple tools in a single code execution — reducing round-trips and token usage.

Choosing the Right Approach

Network Mode Impact

Most examples (01-04, 06, 07) use the default sandbox (aws.codeinterpreter.v1) — no network access, no custom setup. This is sufficient for computation, data analysis, and demos with static data.

For real tool integrations (APIs, databases, AWS services), use a custom Code Interpreter with PUBLIC network mode and an IAM execution role. This lets your pre-loaded functions make real HTTP/SDK calls directly from inside the sandbox. See 05_claude_code_in_code_interpreter/ for setup.

Network Mode Use Case Setup
Default (no network) Computation, data analysis, demos None — works out of the box
PUBLIC + IAM role Real API calls, DB queries, AWS SDK Custom Code Interpreter (setup guide)

Model ID Notes

Prerequisites

Setup

# Navigate to the snippet directory
cd snippets/migration-from-code-execution-tool-to-agentcore-code-interpreter

# Create and activate virtual environment
python3 -m venv .venv
source .venv/bin/activate

# Install dependencies
pip install -r requirements.txt

Run the examples

# Recommended: Strands SDK (simplest, closest to Anthropic experience)
python 03_strands_agent_with_code_interpreter.py

# Converse API with custom agentic loop
python 01_converse_api_with_code_interpreter.py

# InvokeModel API with custom agentic loop (native Anthropic format)
python 02_invoke_model_with_code_interpreter.py

# Direct code execution (no model)
python 04_code_interpreter_highlevel_api.py

# InvokeModel with prompt caching
python 06_invoke_model_with_caching.py

# Programmatic tool calling (multiple tools in one code execution)
python 07_programmatic_tool_calling.py

# Claude Code in sandbox (see 05_claude_code_in_code_interpreter/README.md)
cd 05_claude_code_in_code_interpreter
python setup_infrastructure.py  # one-time
python run.py <code-interpreter-id> "Your prompt"

# Anthropic reference (requires ANTHROPIC_API_KEY)
# export ANTHROPIC_API_KEY=your_key
# python 00_anthropic_code_execution.py

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.