Plenty of us here have tried giving a game character an LLM brain, and the first version usually starts inventing its own world within ten minutes. This post is about a fix that is mostly a writing problem rather than a code problem: how you structure lore so the model gets the right facts at the right moment.
How Retrieval Picks What the Model Sees
Pasting your whole world into a system prompt stops working once the world gets big. The usual answer is retrieval. Lore is split into chunks, each chunk gets an embedding, and on every player message the server finds the chunks closest in meaning and passes only those to the model alongside the conversation.
The catch is that retrieval can only return what your chunks contain, and a chunk's embedding is an average of everything in it. A chunk about one tavern has a sharp fingerprint. A chunk that covers the tavern, the war, three characters and a currency system has a blurry one, and it either matches everything a little or nothing well.
One Topic per Chunk
So write lore the way an encyclopedia is written. One entry for each place, one for each character, one for each rule of the world, each short enough to stand on its own. Name the subject in the entry itself, not just in a heading, so the chunk still makes sense when it is pulled out alone.
Novel style writing works against you here. A beautiful chapter that introduces five characters through a conversation is hard for retrieval to use, because the facts about any one character are scattered across it. Keep the prose for the narrator to produce, and keep the source material plain.
Tags help too. Put the world bible in one collection, item and rules references in another, and point each bot only at what it needs. When a character fails to know something, search the tag directly with the same prompt and you will usually see the problem in seconds: the fact was never chunked on its own.
Keep Temporary State Out of the Lore
Lore should hold what is always true. What is true for this player, or true only tonight, belongs somewhere else. Per player memories handle the first, and a per request rule string like "the player is wounded and cannot fight" handles the second without rewriting the world.
Mixing them is how lore rots. Once the knowledge base contains "the bridge is burned" from one player's session, every other player inherits it.
Wrapping Up
If you want to try this without building the plumbing, AbraTabia AI Storyteller is a free open source server, one PHP app and one SQLite file, that handles chunking, retrieval, per player memory and turn based play for up to 10 players behind one chat endpoint. Write the lore as entries, let retrieval do the selecting, and your narrator will stay inside your world.