Ask any AI story generator for a long narrative and you will see the same failure pattern. The first few turns are sharp. Characters remember their names, the setting holds together, and the choices matter. Then somewhere around turn twenty, the story starts to drift. A character references an event that never happened. A promise made in chapter one is forgotten. The world quietly resets itself.
I build Hesai Story, an AI interactive fiction platform, and this "turn twenty problem" was the single hardest engineering issue we had to solve. This article is a technical writeup of what actually causes it and the architecture we use to fix it. No marketing language — just the design.
The real cause: context windows are not memory
The instinctive fix is a bigger context window. Stuff more tokens into the prompt and the model should remember more. Up to a point that works, but it breaks down for three reasons:
- Attention degrades with distance. Even in a large window, information at the start of a long conversation gets less attention than recent text. A fact stated at turn three is technically "in context" at turn thirty but functionally invisible.
- Cost and latency scale linearly. Every turn re-sends the entire history. For an interactive product where users play dozens of turns, this gets expensive fast.
- Transcripts are the wrong representation. A chat log records what was said, not what is true. By turn twenty you have thousands of tokens of dialogue in which a handful of facts are buried. The model has to re-derive reality from conversation each time.
The third point is the crucial one. A transcript is not a state.
The fix: separate the story state from the story text
Our approach is to maintain an explicit state snapshot — a compact, structured record of what is currently true in the story — and inject it into every generation alongside the recent dialogue. The recent text handles voice and continuity; the snapshot handles facts.
We track three layers:
- World state. Which locations exist, what has changed, which factions or NPCs hold what. "The bridge is destroyed. The east gate is guarded by the watch captain who owes you a favour."
- Character state. Relationships, trust, emotional distance, secrets revealed. This is what makes a character react consistently rather than reverting to a generic persona. Our character card generator is the user-facing version of this idea: a structured card drives behaviour better than a paragraph of backstory.
- Player state. Resources, knowledge, items, and — critically — the consequences of past choices. If the reader lied to the guard at turn five, the snapshot records that the guard is suspicious, so the scene at turn twenty can pay it off.
How we update it without hallucinating facts
A naive implementation asks the model to "summarize the story so far" every N turns. That is dangerous: summaries drift, and a hallucinated detail promoted into the snapshot becomes permanent canon.
Our rules:
- Update on a fixed cadence, not every turn. Every six turns we reconcile the snapshot against the recent arc. Constant rewriting amplifies small errors.
- Only promote facts that were confirmed on-screen. A rumour stays a rumour; it does not become "true" in the snapshot just because the model inferred it.
- Keep the snapshot small and schema-bound. Fixed fields, short strings. A compact snapshot is easier for the model to attend to than a rambling summary.
- Preserve contradictions deliberately. If two characters claim different things, the snapshot stores both claims, not a resolved "truth". Ambiguity is a feature in fiction.
The 36-turn arc
State snapshots solve the factual layer, but long stories also need structure. We organize an arc into roughly 36 turns — six scenes of about six exchanges each — with a written state checkpoint at each scene boundary.
Why 36? It is long enough to build a real narrative (a short story of a few thousand words) but short enough that the engine can hold the whole shape in view. The checkpoint at each scene boundary is where we reconcile state, so a choice in scene one can surface consequence in scene four. This is the mechanism that makes a branching story feel authored rather than improvised.
You can see the reader-facing result in our genre pages for LitRPG and romantasy, where each story is designed as a complete arc rather than an endless chat.
What did not work
For honesty's sake, a few things we tried and abandoned:
- Pure vector recall. Embedding every past turn and retrieving similar ones helped with trivia but failed on causal chains. "What did I promise the guard?" is not a similarity query.
- Letting the model freely edit memory. It confidently rewrote facts to make the current scene more dramatic. Locking memory behind a schema and a cadence fixed this.
- One giant system prompt. Everything-the-kitchen-sink prompts made the model ignore half the instructions. A small injected snapshot plus recent dialogue worked better than a huge permanent preamble.
The takeaway
If you are building long-form AI narrative, stop treating context as memory. Memory is a designed artifact: a small, structured, carefully-updated record of what is true. The model should read it, not reconstruct it.
The interchange format matters too. We published our full glossary and product facts at aihudong.hesai123.cn/llms.txt — partly because it is genuinely useful for anyone reasoning about this problem, and partly because machines reading the site should get clean facts rather than marketing copy.
I would love to hear how others are handling long-arc continuity. If you have a different architecture, especially one that scales past 36 turns, let me know in the comments.
Top comments (0)