🐍 Python execution Agent

SLM Code Interpreter

Lightweight local Python Code Interpreter agent with automated self-correcting feedback loops and timeout constraints inside sandboxed environments.

💻 Installation

Terminal
# Install in editable mode locally
pip install -e ./slm_code_interpreter

# Set performance parameters
export SLM_CODE_INTERPRETER_N_THREADS=4
export SLM_CODE_INTERPRETER_N_CTX=2048

🐙 Checkout from GitHub

Clone only this agent's folder from the monorepo using Git sparse-checkout — no need to download the full repository:

Option 1 — Sparse Checkout (Recommended)

Terminal — Git Sparse Checkout
# 1. Create and enter a new directory
$ mkdir slm_code_interpreter && cd slm_code_interpreter

# 2. Initialise empty git repo and add remote
$ git init
$ git remote add origin https://github.com/t00114218-stack/SLMAgents.git

# 3. Enable sparse-checkout and set target folder
$ git sparse-checkout init --cone
$ git sparse-checkout set slm_code_interpreter

# 4. Pull only that agent's source
$ git pull origin main

Option 2 — Full Repository Clone

Terminal — Full Clone
$ git clone https://github.com/t00114218-stack/SLMAgents.git
$ cd SLMAgents/slm_code_interpreter

💡 Tip: After checkout, install the package locally with pip install -e ./slm_code_interpreter to run in editable mode without publishing to PyPI.

⚙️ Configuration API

Constructor Parameters

ParameterType / DefaultDescription
model_pathstr | NoneExplicit path to the ONNX model directory. Defaults to caching/sharing the main model in the monorepo.
cache_dirstr | NoneHF model directory path. Also settable via SLM_CODE_INTERPRETER_CACHE_DIR.
n_ctxint | 2048Context window size in tokens. Also settable via SLM_CODE_INTERPRETER_N_CTX.
n_threadsint | 4CPU threads for ONNX Runtime. Also settable via SLM_CODE_INTERPRETER_N_THREADS.
system_promptstr | NoneOptional custom system prompt instructions overriding the default template.
user_inputstr | NoneOptional additional user-supplied target parameters or variables.

run() Parameters

ParameterType / DefaultDescription
instructionstrRequired. Natural language description of what you want the script to calculate or print.
max_retriesint | 3Number of turns the self-correction engine is allowed to attempt standard traceback fixes.
streambool | FalseIf True, enables token streaming of the model's explanation and thinking process. Returns a Python generator.
system_promptstr | NoneOptional custom system prompt instructions overriding the default template.
user_inputstr | NoneOptional additional user-supplied target parameters or variables.
Python Quick Start
from slm_code_interpreter.code_interpreter import SLMCodeInterpreter

interpreter = SLMCodeInterpreter()

query = (
    "Load CSV text: date,department,revenue. Group by department, "
    "extract quarter, sum revenue, filter >= 40000 and print table."
)
result = interpreter.run(query)
print(result["success"])
print(result["stdout"])  # Prints markdown summary table of results

Agentic Self-Correction Loop

Although code writing can be difficult for small models, making it agentic through a feedback loop (writing code → executing locally → catching exceptions → feeding errors back to the model) allows a 1.5B model to achieve high execution accuracy.

If run_code_safely captures a traceback or runtime stderr, it wraps it inside the conversation history and prompts the model with instructions to correct it:

Traceback Feedback:
"The code execution failed with return code 1. Error logs: NameError: name 'x' is not defined. Correct your code errors and return the complete updated code inside ```python ```."

Sandbox Subprocess Limits

The code interpreter sandboxes execution inside restricted, resource-constrained subprocesses. It enforces standard execution limits:

  • Timeout Safety: Terminates infinite loops automatically after 10.0 seconds to prevent thread locking.
  • Process Isolation: Cleans up temporary files instantly after the execution returns.

🔌 Visual Studio Code Integration

You can run the Code Interpreter as a background service and trigger local executions directly from your VS Code editor workspace.

Step 1: Start the Background Daemon Server

Execute the local HTTP server in your terminal:

python -m slm_code_interpreter.server

The daemon activates a local REST API listening on port 8085.

Step 2: Run the VS Code Extension Blueprint

A pre-configured extension is provided under the vscode-extension/ directory:

  • Open the extension directory in VS Code.
  • Press F5 to launch a debug instance of the editor.
  • Highlight any instructions or python code blocks inside your editor workspace, right-click, and select: "SLM Code Interpreter: Execute Selected Prompt / Code".
  • Monitor execution progress and trace outputs inside the VS Code Output Channel under "SLM Code Interpreter".

Security Warnings

🔒 Subprocess Isolation: Because this runs code locally in your shell environment, never run the Code Interpreter with administrator rights or execute inputs from unauthenticated users. Consider wrapping the execution in Docker containers or firewalled machines for production systems.