Skip to main content
Combine the power of LLM orchestration with secure, isolated execution environments. This guide shows how to build a “swarm” of agents—where multiple worker agents generate and execute code in parallel sandboxes to analyze a problem from different perspectives, and a lead agent synthesizes their findings.

How it works

  1. Define Worker Agents: Create a function that uses an LLM to generate code for a specific perspective (e.g., Scientific, Economic).
  2. Execute in Sandboxes: Each worker spins up a secure Tensorlake Sandbox to run the generated code and capture the output.
  3. Map (Parallelize): Launch multiple instances of the worker agent in parallel.
  4. Reduce (Aggregate): A lead agent receives all the reports and synthesizes a final insight.

Prerequisites

TypeScript SDK starter

If your orchestrator already runs in Node.js, use the same pattern: LLM generates code, one sandbox executes it, and Promise.all() fans the scouts out in parallel.

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


This example uses 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:
The SDK will automatically use this key.

Production Tips

Reduce Latency with Snapshots

The example above runs pip 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.
See the Snapshots guide for details.

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.