DSPy RLMs
Run the Python that DSPy recursive language models write in an E2B sandbox. You get full CPython with SQLite, and code that loops forever gets killed.
- E2B
- E2BInterpreter via interpreter_factory
- dspy 3.4.0
- Python
Get started
Paste the prompt into your coding agent, or follow the steps yourself.
Paste this into your coding agent
Add E2B sandboxes to this DSPy project.
- 1.Install the dspy-rlms-python example from the E2B cookbook into the project using its package manager. E2BInterpreter comes from that local project, not from PyPI.
- 2.The interpreter needs the E2B_API_KEY environment variable. If it isn't set, ask me for my API key from https://console.e2b.dev/?tab=keys. Never hardcode it.
- 3.Follow the DSPy RLMs guide at https://docs.e2b.dev/agents/dspy and pass an interpreter_factory that returns a new E2BInterpreter to dspy.RLM, so the Python the model writes runs in an E2B sandbox.
- 4.Run the RLM once and show me the output that came from the sandbox.
Or set it up yourself
1. Get the example
2. Set your keys in .env
3. Give the RLM an E2B interpreter
4. Run the incident analysis
What runs in the sandbox
The Python your RLM writes runs in a network-off E2B sandbox. Model calls and keys stay on your host.
- Generated Python runs in full CPython on remote Linux, with sqlite3 and subprocess and no outbound network.
- Imports, variables and SQLite connections persist across steps within one invocation. Each invocation gets its own sandbox.
- execution_timeout bounds each step and sandbox_timeout the session. shutdown() deletes the sandbox when DSPy is done.
- Provider keys stay on the host. llm_query calls from the sandbox are sent back to the host, which calls the model.
More training & eval tools
Other RL and eval tools that run their rollouts on E2B.
- Harbor / Terminal-BenchRun Harbor and Terminal-Bench trials on E2B and scale evals and rollouts to many parallel sandboxes.
- Hugging Face OpenEnvRun OpenEnv environments like Terminus on E2B and get a first reward without a GPU.
- Hugging Face TRLCompute code rewards for TRL GRPO training in E2B sandboxes.
