Skip to content

Python REPL

Runs model-generated Python through the agent’s sandbox by calling sandbox.execute_code(code, "python3") (sandbox.executeCode(code, 'python3') in TypeScript). It returns output (stdout), error (stderr, including tracebacks), and exit_code. Each call runs in a fresh interpreter, so variables, imports, and definitions do not persist across calls. Files written to the sandbox filesystem do persist, so the model can checkpoint intermediate results to disk and load them in a later call.

Supported in: Node.js on Unix/Linux/macOS (TypeScript), Python.

import { Agent } from '@strands-agents/sdk'
import { pythonRepl } from '@strands-agents/sdk/vended-tools/python-repl'
const agent = new Agent({ tools: [pythonRepl] })
await agent.invoke(
'Use python_repl to compute the first 10 Fibonacci numbers and print them.'
)

Bound sandbox or custom interpreter:

import { Agent } from '@strands-agents/sdk'
import { DockerSandbox } from '@strands-agents/sdk/sandbox/docker'
import { makePythonRepl } from '@strands-agents/sdk/vended-tools/python-repl'
const pythonRepl = makePythonRepl(new DockerSandbox({ container: 'my-container' }), {
language: 'python3.12',
})
const agent = new Agent({ tools: [pythonRepl] })

Full API reference