By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Large language models are unreliable at computation. Ask one to multiply two 9-digit numbers or summarize a dataset and it will guess. The model has no calculator, no memory of arithmetic rules that hold at scale. You need the answer to be computed, not imagined. Running arbitrary code on your own infrastructure is dangerous, but Code Interpreter solves this by giving the agent a sandboxed environment where it can write and execute code safely.
When an agent needs to perform a calculation or transform data, it writes code and sends it to Code Interpreter instead of your production system. The interpreter runs that code in an isolated session with its own runtime, file system, and memory. Results flow back to the model as stdout, files, or charts. The model reads those results and answers your question.
Code Interpreter supports Python, JavaScript, and TypeScript execution. Each session maintains state, so variables and imports persist across multiple code calls. This matters for multi-step analysis where one call prepares data and the next call analyzes it.
Each session gets its own file system. You can upload files to it, download results, and read from or write to Amazon S3. CPU and memory are capped so a runaway loop in one session cannot starve other workloads. Network access is configurable: you can lock the sandbox down entirely, or enable public internet and wider AWS permissions when the task requires them.
Execution logs and metrics go to CloudWatch, so you can see what code ran, how long it took, and whether it succeeded.
The SDK lets you create a session and invoke code explicitly:
# Shape of the SDK usage - check the Code Interpreter quickstart for your version
from bedrock_agentcore.tools.code_interpreter_client import code_session
with code_session("us-west-2") as ci:
ci.invoke("executeCode", {
"language": "python",
"code": "import statistics as s; data=[3,5,8,13]; print(s.mean(data))",
})
# same session: variable 'data' still exists
ci.invoke("executeCode", {"language": "python", "code": "print(max(data))"})
The session persists across calls, so the second invoke can reference variables created in the first.
Most production setups wrap Code Interpreter as a tool for the agent framework. Strands and LangChain ship ready-made integrations with tools like execute_code. The model decides when to call it based on the task. Your code passes the results back into the agent loop and the model continues reasoning.
Use Code Interpreter for data analysis and charting from uploaded CSVs, exact arithmetic and unit conversion, file transformation such as PDF or image processing, and letting a coding agent run and test its own code without risk to your systems.
Keep the sandbox network mode as restrictive as the task allows, and give the execution role only the AWS permissions the code truly needs.
Network mode is your first control. If the code doesn't need internet or AWS API calls, lock it down. If it does, enable only what it needs. The execution role should follow the same principle: grant only the IAM permissions required for the specific task. This limits the blast radius if the model generates code that misbehaves or an attacker manipulates the agent.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.