Skip to content

strands-code-agent

strands-code-agent provides a CodeAgent that replaces the tool-calling paradigm with code generation as the agent’s primary action interface. Rather than invoking structured tools, the agent writes Python code in a persistent REPL where domain capabilities are exposed as importable library functions. This keeps intermediate data as native Python objects in memory and lets the agent compose multi-step logic in a single code block.

In empirical evaluations on the Data Agent Benchmark, this approach achieves higher accuracy (+7%) while consuming 78% fewer input tokens, 67% fewer output tokens, completing tasks 56% faster, and requiring 35% fewer reasoning cycles compared to an equivalent tool-calling agent.

Terminal window
pip install strands-code-agent
from strands_code_agent import CodeAgent
agent = CodeAgent(system_prompt="You are a helpful data analyst.")
response = agent("What is 2 ** 10?")

The agent receives a python_repl tool automatically and solves tasks by writing and executing Python code.

A Toolkit bundles everything the REPL needs for a specific domain — authorized imports, initialization code, usage instructions, and domain-specific functions:

from strands_code_agent import CodeAgent
from strands_code_agent.toolkits import DATA_ANALYSIS_TOOLKIT, VISUALIZATION_TOOLKIT
agent = CodeAgent(
system_prompt="You are a data analyst.",
toolkits=[DATA_ANALYSIS_TOOLKIT, VISUALIZATION_TOOLKIT],
)

You can also expose your own functions as domain-specific code:

from strands_code_agent import CodeAgent, Toolkit
def calculate_roi(investment: float, returns: float) -> float:
"""Calculate return on investment as a percentage."""
return (returns - investment) / investment * 100
agent = CodeAgent(
system_prompt="You are a finance assistant.",
toolkits=[Toolkit(domain_specific_code=[calculate_roi])],
)