Choose Your AIdventure
AI & GamingFeatured

Choose Your AIdventure

An AI-powered text RPG built for the Cloudflare AI Challenge, where every scene is narrated and illustrated by the same models running it.

May 15, 2024
AI & Gaming
Technologies
Cloudflare Workers AINext.jsPrompt EngineeringEdge FunctionsImage Generation

Built for the Cloudflare AI Challenge, this is a text-based RPG where the story, the choices, and the visuals are all generated on the fly by AI models running at the edge.

What it is

Players pick a scenario — medieval fantasy or cyberpunk sci-fi — choose a character class, and get dropped into a story that unfolds turn by turn. Each scene is narrated by Llama 3.1 and illustrated by SDXL Lightning, generating a widescreen pixel art image to match whatever just happened. NPCs get their own AI-generated 8-bit portraits. The whole thing runs on Cloudflare Workers AI for inference, with API calls hitting edge functions instead of a traditional server.

The interesting parts

The biggest challenge wasn't getting AI to tell a story — it was getting it to tell a good one. Left unconstrained, LLMs tend to ramble. So I built a pacing system that injects hidden directives into the system prompt based on turn count: a hook phase, a midgame conflict, a climax, and a forced resolution by turn ten. The AI doesn't "know" this is happening — it just receives increasingly urgent instructions about consequences and finality.

Structuring the output was the other non-trivial piece. The model returns labelled text (NPC:, NARRATIVE:, Choices:) which the frontend parses to extract NPC names for portrait generation, strip formatting from the displayed text, and populate the choice buttons — all from a single response.

The scenario system is config-driven, so adding a new adventure type is mostly just writing a new entry in scenarios.ts and a system prompt.

Revived with Gemini CLI in 2026

In early 2026 I used Gemini CLI to bring this codebase and the SEO AI Toolbox up to date: refactored architecture, updated dependencies and new features, done in spare time between client work. I wrote about how that went in "With Gemini CLI, I'm able to keep my pet projects alive and kicking", which DEV picked as one of the five winners of its Built with Google Gemini Writing Challenge in March 2026.

What I learned

This started as a submission to understand Cloudflare's AI ecosystem — Workers, Pages, and their hosted models. It taught me how to work within the constraints of a specific provider's model catalog rather than defaulting to OpenAI, and how prompt engineering decisions compound quickly when the AI output is doing multiple jobs at once (narrative, UI data, image prompt).

The dev.to write-up reached ~500 readers in the week following the challenge.