Skip to main content
Create a dynamic, unpredictable storytelling game using a swarm of AI agents. This guide demonstrates how to build a Dungeons & Dragons-style RPG where multiple “Scene Agents” draft possible outcomes in parallel, and a “Dungeon Master” agent weaves them into a coherent narrative based on player choice. This pattern uses a “Map-Reduce” model: parallel workers generate possibilities (map), and a lead agent synthesizes them (reduce).

How it works

  1. Branching Possibilities: For a given player choice, the application imagines several potential actions (e.g., “Fight,” “Flee,” “Negotiate”).
  2. Map (Parallel Scene Writers): A scene_agent is spawned for each potential action. These run in parallel, each in its own sandbox.
  3. Sandbox Execution: Each scene_agent uses an LLM to generate a Python script that simulates a dice roll and determines the outcome of its assigned action. The script runs securely in the sandbox and outputs a JSON with narrative text, consequences, and an ASCII art illustration.
  4. Reduce (Dungeon Master): A dungeon_master agent receives the drafted scenes from all parallel workers.
  5. Narrate & Update State: The DM selects the draft corresponding to the player’s actual choice, applies the consequences (e.g., HP loss, new item), and uses an LLM to write the next part of the story, complete with new choices for the player.

Prerequisites

You’ll need the Tensorlake SDK, an OpenAI client, and the rich library for the terminal UI.
This example uses the python-dotenv library to load your API keys from a .env file. Create a file named .env in your project root and add your keys:
The clients will automatically use these keys.

TypeScript SDK starter

In Node.js, model each branch as LLM -> sandbox -> JSON scene draft, then reduce the drafts with your Dungeon Master step:
This gives you the same parallel-map stage as the Python example. Your Dungeon Master step can stay in Node.js and operate on the returned JSON drafts.

Full Example

The complete script below orchestrates the entire game loop. You can run it directly to play in your terminal.

What Happens Step-by-Step


How to Extend This Example

Generate Images

The agents already create image prompts for DALL-E. You could extend the dungeon_master to call an image generation API and display the resulting image, creating a true multimedia experience.

Add More Complex Logic

The sandbox is perfect for running more complex game mechanics. You could:
  • Implement a full combat system with multiple enemy types.
  • Create skill checks that depend on the player’s inventory or stats.
  • Generate dynamic loot tables or environmental puzzles.

Use Snapshots for Faster Turns

If your scene_agent sandboxes needed to install libraries like numpy for more complex simulations, the pip install on every turn would add latency. You can pre-install dependencies into a base sandbox and create a Snapshot. Future turns can then launch from that snapshot instantly.

What to build next

Agentic Swarm Intelligence

See another example of the Map-Reduce pattern with parallel agents.

Snapshots

Optimize your game’s turn speed by pre-baking dependencies.