Inspiration

I grew up playing games like AI Dungeon, fascinated by the promise of infinite stories. But the fascination always turned to frustration. The AI would forget who I was after 20 turns, the villain would randomly change names, and the world felt like a dream—inconsistent and weightless. It wasn't a simulation; it was just text prediction.

My curiosity wasn't just to read a story; I wanted to live in alternate timelines. I wanted a world where if I betrayed a character, their "Trust" stat would mathematically drop and stay low. I wanted a simulation where the tone could shift from "Hope" to "Despair" based on my actions.

I realized that to fix the "amnesia problem" and create a true simulation, I didn't need a better chatbot. I needed a Narrative Engine—one that could act as both a creative writer and a strict Dungeon Master simultaneously. That desire to blend creative chaos with mathematical order was the spark for Chronos.

How I Built It

Chronos is built on a React/TypeScript frontend that communicates directly with the Google Gemini 3 Flash API.

The core innovation is what I call "Dual-Phase State Streaming." In a traditional app, you might make one API call to generate text and another to analyze it for stats. That’s too slow for a game. instead, I engineered a system where Gemini performs two distinct cognitive tasks in a single stream:

  1. Phase 1 (The Writer): It streams the narrative response in real-time, grounded in the current context.
  2. Phase 2 (The Computer): Immediately after the text, it outputs a hidden JSON payload containing mathematically precise updates to the world state.

This is powered by a custom geminiService.ts engine that parses the stream on the fly. I also built a "Story Card" system—a lightweight RAG (Retrieval-Augmented Generation) mechanism—that dynamically injects lore into the context window only when specific keywords are triggered, keeping the simulation coherent over thousands of turns.

Challenges I faced (And How Gemini Solved Them)

Building a stateful engine on top of a probabilistic model introduced three massive hurdles that would have killed this project on any other model:

1. The "Latency vs. Complexity" Deadlock

  • The Problem: I needed to track over 20 variables per turn (Character Emotions, Story DNA, Global Tension, Fact Database). Asking an LLM to write a story and calculate these stats usually results in a 10-second delay, which ruins the immersion of a game.
  • The Solution: This was only solvable thanks to the sheer inference speed of Gemini 3 Flash. It is fast enough to handle my massive prompt—which includes the entire history, character psychology profiles, and director instructions—and output both the story and the JSON state updates in near real-time.

2. The "Hallucination" of State

  • The Problem: Most LLMs struggle to switch modes. If you ask them to be creative, they get sloppy with JSON syntax. If you ask them to be logical, their writing becomes dry. I needed a model that could be a poet in the first half of the response and a compiler in the second.
  • The Solution: Gemini’s superior instruction following was critical. I implemented a strict separator protocol (___METADATA___). Gemini 3 is the first model I've used that respects this boundary 100% of the time without bleeding narrative text into the JSON or breaking the data structure, even when the "Temperature" is set high for creativity.

3. The Context Horizon

  • The Problem: My inspiration, AI Dungeon, failed because it ran out of context memory. A simulation requires remembering a secret whispered 500 turns ago.
  • The Solution: Gemini’s massive context window allowed me to feed the entire relevant state and history into every turn. I didn't have to aggressively summarize (and lose detail) like I would with other models. This allows characters in Chronos to hold grudges or remember promises from the very start of the timeline.

What I learned

  • LLMs as State Machines: I learned that with the right prompting architecture, an LLM isn't just a text generator—it can be a database administrator.
  • The "Director" Concept: I discovered that users don't just want to play; they want to control. Building the Director Overlay taught me that exposing the AI's "brain" (the tension sliders and pacing controls) is more engaging than hiding it.
  • Event Sourcing: I learned how to build a frontend that is purely driven by an append-only log of events, making the game state resilient and easy to save/load.

Chronos isn't just a game to me; it's a prototype for the future of interactive media, where the story doesn't just happen to you—it evolves with you.

Built With

Share this project:

Updates