GitHub | Live demo | Video demo
Problem
Branching interactive narratives (like choose your own adventures, or visual novels) are difficult to make! Authors are required to mull over and define every possible action a player might take, requiring time-consuming playtesting. Players might get frustrated they can't do something creative they've come up with, like persuading a guard to let them through to a private area.
AI tries to solve this in some ways, enabling players to use natural language inputs for their actions. However, these AI games lack narrative structure and authorial control. Players can easily hijack the story, and authors can't edit specific story beats because they're regenerated every time.
Bonsai asks, "What if deploying a game could help improve and grow the game for you?". Inspired by a bonsai tree, the system grows by itself as players play through it, while authors can come back whenever they'd like to prune and shape its growth.
What it does
Bonsai is an human-AI co-authoring game engine, where games grow by themselves.
The platform features both an editor using a custom markdown language loosely inspired by Inky, and a playtesting player for viewing your game thus far.
Authors start by writing as much of the game as they'd like in the editor, as little as just a few lines, a decision point, and a few (optional) options. The decision point will prompt the player for input. In the player, whenever the player types whatever they'd like to do, and the story branches accordingly: either defaulting to a similar enough existing option ("run away" is akin to "sneak out"), linking to an existing branch, or generating a new branch permanently and navigating you there in real-time in the player.
The editor reflects changes in real-time, too, and authors can use version control to view auto-commits and prune unwanted branches. Authors can also set prompts (either scene-based or global) to guide narrative direction/constraints. Like r/place, authors and players are collaborating on this game intermediated by AI.
These features also double as built-in caching, making the game increasingly fast for playing as it expands and covers possibility space, only generating for new, unseen choices. This also prevents overwhelm for authors, as updates are staggered based on when a player has actually tried an option given the scene's context.
How we built it

Bonsai is a Next.js application with two core API routes powered by Grok AI. The system uses a three-tier matching strategy:
- Local matching - Fast exact/substring checks against existing options
- Fuzzy matching - Grok analyzes player input against option variants (temperature=0 for consistency)
- Dynamic generation - When no match is found (confidence < 0.45), Grok generates new story branches (temperature=0.3 for creativity)
The editor uses a custom markdown-inspired notation (- narrative, ? decisions, * options, # scenes, ↗ jumps, ! prompts) that's human-readable and -writeable but easily convertable to JSON to be generated and revised by AI.
Bonsai also includes result caching and version control via local storage. The context system keeps track of current scene, relevant prompts (including global- and scene-level prompts), and current game state, feeding all of this to Grok for consistent world-building.
Challenges & learnings we ran into
A markdown system for interactive narrative itself is challenging, and it was especially so when keeping in mind that it had to be both easily human- and AI-interpretable. Many small iterations had to be made for small microinteractions to improve the feeling of editing line-by-line, inspired by Notion.
Initially, I tried using semantic search (extremely fast and efficient) for checking if an input was similar enough to an existing option. However, semantic percentages were very inaccurate for my use case: “boat around” (6%) is less semantically similar to “learn to sail” than “go around town” (11%). If I had more time, I could explore fine-tuning a model for this purpose. For time, I used local substring matching first, then Grok fuzzy matching with confidence thresholds, and finally dynamic generation as a fallback.
Getting Grok to respect author voice and create satisfying generations was a tricky prompt engineering problem. The solution was emphasizing custom prompts (! lines) with "IMPORTANT:" prefixes in both system and user messages and implementing strict length constraints (320 chars) to keep AI responses focused. I also added a parse_error fallback to our legacy notation parser for reliability.
Getting Grok to respect the notation was also tricky. To increase speed, I reworked the branching prompt to not include anything about any line type except narrative and decision points, as I didn't actually need it to generate options for me (in fact, it would often hallucinate scenes if it generated options at all).
Accomplishments that we're proud of
Bonsai uses a complete three-tier matching system that preserves authors' creative control. The confidence threshold (0.45) creates a natural flow: familiar inputs follow hand-crafted branches, novel inputs generate new content that still respects the author's established voice and world rules. The editor was also iterated upon to bridge ease of use for both humans and AI, respecting both inputs with version control, while preventing overwhelm from authors reviewing completions.
What we learned
Balancing AI creativity with author control requires multiple guardrails working together. Temperature tuning matters: 0 for matching (consistency), 0.3 for generation (creativity but not chaos). Prompt engineering is very tricky but crucial as well, and requires important messages to be prepended as important and repeated. Expecting the LLM to only do a small job and obscuring/filtering context also improves performance. The notation system proved essential as a satisfying human-readable syntax that encodes structure makes collaborative human-AI authoring actually work.
What's next for bonsai: a growing game engine
- Sharable links so authors can share their games without revealing the editor.
- Better version control for approving/rejecting/editing permanent branch generations.
- Variables and state tracking (relationship scores, inventory).
- Learning author preferences: which branches players explore most, where they get stuck, which AI-generated content gets pruned - and using these to generate better branches
Built With
- grok
- next.js
- tailwind

Log in or sign up for Devpost to join the conversation.