SLM Code Interpreter
Lightweight local Python Code Interpreter agent with automated self-correcting feedback loops and timeout constraints inside sandboxed environments.
💻 Installation
# 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)
Option 2 — Full Repository Clone
💡 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
| Parameter | Type / Default | Description |
|---|---|---|
| model_path | str | None | Explicit path to the ONNX model directory. Defaults to caching/sharing the main model in the monorepo. |
| cache_dir | str | None | HF model directory path. Also settable via SLM_CODE_INTERPRETER_CACHE_DIR. |
| n_ctx | int | 2048 | Context window size in tokens. Also settable via SLM_CODE_INTERPRETER_N_CTX. |
| n_threads | int | 4 | CPU threads for ONNX Runtime. Also settable via SLM_CODE_INTERPRETER_N_THREADS. |
| system_prompt | str | None | Optional custom system prompt instructions overriding the default template. |
| user_input | str | None | Optional additional user-supplied target parameters or variables. |
run() Parameters
| Parameter | Type / Default | Description |
|---|---|---|
| instruction | str | Required. Natural language description of what you want the script to calculate or print. |
| max_retries | int | 3 | Number of turns the self-correction engine is allowed to attempt standard traceback fixes. |
| stream | bool | False | If True, enables token streaming of the model's explanation and thinking process. Returns a Python generator. |
| system_prompt | str | None | Optional custom system prompt instructions overriding the default template. |
| user_input | str | None | Optional additional user-supplied target parameters or variables. |
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:
"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
F5to 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".