Strands Agents with Bedrock AgentCore Code Interpreter
Integrating Strands Agents with the Amazon Bedrock AgentCore Code Interpreter empowers your AI to solve complex computational problems by executing Python code dynamically in a secure sandbox. This capability allows agents to perform real-time data analysis, mathematical calculations, and even generate visualizations without manual intervention.
You can start with a default, network-isolated sandbox for maximum security, or configure a custom interpreter with public network access for tasks like retrieving live stock prices or installing external libraries.
A. Using the Default Sandboxed Interpreter
The quickest way to enable code execution is through the built-in AgentCoreCodeInterpreter tool. This environment is isolated from the network by default, making it ideal for processing sensitive data or performing local calculations.
from strands import Agent
from strands.models import BedrockModel
from strands_tools.code_interpreter import AgentCoreCodeInterpreter
# Initialize the default sandboxed code interpreter
agentcore_code_interpreter = AgentCoreCodeInterpreter()
# Create a code-gen assistant that validates answers through execution
agent = Agent(
model=BedrockModel(model_id="us.amazon.nova-pro-v1:0"),
system_prompt="You are a helpful AI assistant that validates all answers through code execution.",
tools=[agentcore_code_interpreter.code_interpreter],
)
# The agent will write and run Python code to solve this math problem
agent("What is the area of a circle with radius 8.26cm?")
B. Enabling Network Access for Live Data
For tasks requiring internet connectivity—such as fetching the latest Amazon stock price—you can create a custom interpreter with a PUBLIC network mode. By wrapping invoke_code_interpreter calls into custom tools, your Strands Agent can dynamically install packages like yfinance to accomplish its goal.
from strands import Agent, tool
from bedrock_agentcore.tools.code_interpreter_client import CodeInterpreter
# Initialize a custom interpreter session (pre-configured with PUBLIC network access)
ci_client = CodeInterpreter(region="us-east-1")
ci_client.start(identifier="YOUR_INTERPRETER_ID")
@tool
def execute_python(code: str) -> str:
"""Execute Python code in the custom sandbox."""
response = ci_client.invoke("executeCode", {"code": code, "language": "python"})
for event in response["stream"]:
return str(event["result"])
# Create an agent capable of downloading packages and accessing the web
agent = Agent(
model=BedrockModel(model_id="us.amazon.nova-pro-v1:0"),
system_prompt="Execute pip install if required to download packages.",
tools=[execute_python],
)
# Agent will install yfinance and fetch live data
agent("What is the stock price of Amazon today?")
Key Takeaway: The Bedrock AgentCore Code Interpreter transforms your agent from a text generator into a functional programmer. Whether running in a secure, isolated sandbox or a network-enabled environment, the agent maintains state across multiple executions, allowing for sophisticated, multi-step problem solving.

Top comments (0)