How it works
- Define Worker Agents: Create a function that uses an LLM to generate code for a specific perspective (e.g., Scientific, Economic).
- Execute in Sandboxes: Each worker spins up a secure Tensorlake Sandbox to run the generated code and capture the output.
- Map (Parallelize): Launch multiple instances of the worker agent in parallel.
- Reduce (Aggregate): A lead agent receives all the reports and synthesizes a final insight.
Prerequisites
Full example
This example simulates a Mars mission planning scenario where “scout” agents analyze different risks (Scientific, Economic, Ethical, etc.) by writing and running simulations in isolated sandboxes.Workflow: Step-by-Step Execution
| Step | Component | Action |
|---|---|---|
| 1 | Orchestrator | Triggers 5 parallel scout tasks using the scout_agent.map() function. |
| 2 | Scout Agent | Leverages GPT-4o to draft a custom simulation script based on a specific perspective. |
| 3 | Sandbox | Securely installs numpy, handles dependencies, and executes the script in isolation. |
| 4 | Scout Agent | Compiles simulation data into a structured ScoutReport for return. |
| 5 | Lead Agent | Aggregates all reports and prompts GPT-4o for a final Go/No-Go decision. |
This example uses the The
python-dotenv library to load your Tensorlake API key from a .env file. Create a file named .env in your project root and add your key:SandboxClient will automatically use this key.Production Tips
Reduce Latency with Snapshots
The example above runspip install numpy inside every scout’s sandbox. In a real swarm with dozens of agents, this adds unnecessary latency and bandwidth usage.
For production, create a “base” sandbox, install your common dependencies, and create a Snapshot. Then, have your agents initialize from that snapshot instantly.
Security: Lock down the network
Since the scouts run code generated by an LLM, it is safer to disable internet access to prevent data exfiltration or malicious downloads.What to build next
AI Code Execution
Learn how to build a stateful code interpreter for a single agent.
Snapshots
Optimize your swarm’s startup time by pre-baking dependencies.