Skip to content

Code Interpretation

Run Python locally or in a remote sandbox. Gate python_repl with execution_permissions.

← Prebuilt Tools overview

ToolExtraDescription
python_replbasePersistent local Python REPL. Not supported on Windows.
code_interpreteragent-core-code-interpreterSandboxed execution on Bedrock AgentCore. Register AgentCoreCodeInterpreter(...).code_interpreter.

python_repl

Persistent local Python REPL. Not supported on Windows.

python
from elsai_tools.python_repl import python_repl
ParameterTypeRequiredDescription
codestrYesPython code to execute
interactiveboolNoInteractive REPL mode
reset_stateboolNoReset REPL state before running

Persistence and A2A isolation

python_repl keeps a persistent namespace between calls. Where that state is stored depends on context:

ScenarioPersistence path
Standalone agent (no A2A context_id)./repl_state/ in the working directory, or $PYTHON_REPL_PERSISTENCE_DIR/repl_state/ when set
A2A server (context_id present)$ELSAI_A2A_SESSION_DIR/<context_id>/repl/ (default base: {tempdir}/elsai-a2a-sessions)

On A2A servers, python_repl declares PER_CONTEXT session scope. Each context_id receives its own REPL namespace and pickle file, so variables from one conversation cannot leak into another.

Requires elsai-agents-tools==0.3.0 with elsai-agents>=0.3.1 (A2A session store and PrefixedTemplateSessionsStore).

Using on an A2A server

python
from elsai import Agent

# Register the tool on the template agent; PER_CONTEXT cloning is automatic.
agent = Agent(tools=[...])  # e.g. tools=[python_repl], tools=[graph], …

Full A2AServer + session-manager wiring lives in Concepts — see Stateful prebuilt tools — examples.

  • PER_CONTEXT is built in — register python_repl on the template agent; cloning happens automatically per context_id.
  • State path: $ELSAI_A2A_SESSION_DIR/<context_id>/repl/repl_state.pkl
  • See Stateful prebuilt tools — examples for a shared server pattern and multi-tool setup.

code_interpreter

Sandboxed execution on Bedrock AgentCore. Register AgentCoreCodeInterpreter(...).code_interpreter.

python
from elsai_tools.code_interpreter import AgentCoreCodeInterpreter

interpreter = AgentCoreCodeInterpreter(region="us-west-2")
agent = Agent(tools=[interpreter.code_interpreter])

Extra: agent-core-code-interpreter

ParameterTypeRequiredDescription
code_interpreter_input.actionstrYesAction type (executeCode, etc.)
code_interpreter_input.codestrYesCode to run
code_interpreter_input.languagestrNopython, javascript, or typescript
code_interpreter_input.session_namestrNoSandbox session name

A2A isolation

On A2A servers, code_interpreter (via AgentCoreCodeInterpreter) declares PER_CONTEXT scope. Each context_id receives a cloned interpreter instance. Sandbox session names are prefixed with the context_id so remote sessions cannot collide across concurrent A2A conversations.

Using on an A2A server

python
from elsai import Agent

# Register the tool on the template agent; PER_CONTEXT cloning is automatic.
agent = Agent(tools=[...])  # e.g. tools=[python_repl], tools=[graph], …

Full A2AServer + session-manager wiring lives in Concepts — see Stateful prebuilt tools — examples.

  • Requires elsai-agents-tools[agent-core-code-interpreter] and AWS credentials for Bedrock AgentCore.
  • Remote sandbox session names are prefixed with context_id automatically.

See Stateful tools on A2A and Stateful prebuilt tools — examples.

Copyright © 2026 elsai foundry.