How AI NPCs remember the player and what memory costs
An AI NPC remembers the player because the game keeps the memory, not the model. A language model forgets everything between calls, so on each turn the game puts three layers into the prompt. These are the recent conversation word for word, a few older memories retrieved from a long-term store, and reflections the model wrote earlier from many events.
Most of the design work sits in three decisions: what goes into the store as a field the game enforces, how retrieval ranks everything else, and how many tokens memory may spend on a turn. This piece goes deeper than the memory section of How AI NPCs work in games in 2026. Rick Vu, who directed it, builds Red Dog Studio, a game studio building AI-native NPCs. Nothing below describes Red Dog’s own system.
The model starts every turn empty
Each call to a language model is independent. The model has no record of the last conversation unless the game sends it again as text. “Memory” in an LLM NPC therefore means one thing. It is the text the game chooses to put in the prompt on this turn.
That makes memory a data problem with a size limit. The game writes events somewhere, picks a few of them back out, and pays for every token it sends. The three layers below differ in how they pick.
Layer one is the recent conversation
The last few turns go into the prompt verbatim. This layer keeps the conversation coherent from one line to the next, and it costs nothing to build. It also grows by every line the player types.
Cap it by tokens instead of by turns, because one long player message can be worth ten short ones. When old turns fall off the end, write them to the long-term store first. A turn that falls out of the window without being stored is gone for good.
Layer two is a store the game searches
The reference design is the memory stream in the Generative Agents paper from Stanford and Google. The authors describe it as “a database that maintains a comprehensive record of an agent’s experience,” written in natural language. They also note that the full stream does not fit in the model’s context window. Retrieval decides which few memories go in.
The paper scores every memory on three measures, scales each to the range 0 to 1, and adds them with equal weights:
- Recency decays exponentially over the game hours since the memory was last retrieved, with a decay factor of 0.995 per hour.
- Importance is a score from 1 to 10 that the model assigns when the memory is written. In the paper’s examples, cleaning up a room scored 2 and asking a crush out on a date scored 8.
- Relevance is the similarity between the memory’s embedding and the current situation.
Two details are worth copying on purpose or rejecting on purpose. Recency counts from the last retrieval, so a memory the character keeps using stays fresh. Importance costs one model call per stored memory. A game can often set importance from the event type instead, since the game already knows that a betrayal outranks a greeting.
Search itself is cheap. The paper Fixed-Persona SLMs with Modular Memory gives each NPC two vector stores, one for conversation history and one for world knowledge. It pulls the top matches from each by cosine similarity. On a desktop with an RTX 2070 Super, retrieval stayed below 0.042 seconds with 1,000 entries. That was a command-line prototype, not a running game.
A different approach lets the model manage its own memory. MemGPT treats the context window like an operating system treats fast memory. The model moves information between tiers through function calls. That gives the model more control, and it adds model calls to the turn, which a talking NPC can rarely afford.
Layer three is reflection
Raw events are poor prompt material. Twelve lines of a player haggling say less than one line that reads “the player always tries to haggle me down.”
Generative Agents builds those lines with reflection. An agent reflects when the summed importance of its latest events passes 150, which happened roughly two or three times per game day. The model reads the 100 most recent memories and asks three high-level questions about them. It then writes insights that cite their sources in the form “insight (because of 1, 5, 3).” Reflections go back into the store and can cite other reflections, which forms a tree.
Keep those citations. They let you check why a character believes something, and they let you delete a belief when its source goes away. Run reflection between sessions or at a quiet moment in the game, never inside a player’s turn.
Store facts as fields and impressions as text
The memory section of the parent article gives this rule in one line. Here it is in more detail. If the game must act on a fact, or the player can check it, it belongs in a structured field. Game code writes that field, and the model only reads it.
Names, debts, promises with deadlines, quest choices, and relationship scores are fields. So is a record of what the NPC told the player, because a merchant who quoted 20 gold must not later insist it was 40. Impressions, jokes, and moods are free text. A small error in tone costs little. A wrong debt breaks the game.
An illustrative record for one player and one NPC looks like this:
{
"player_id": "p_1042",
"npc_id": "smith",
"fields": { "player_name": "Ash", "debt_gold": 30, "quest_mine": "accepted" },
"events": [
{ "text": "Ash promised to repay the debt by the harvest festival.", "game_hour": 1412, "importance": 7 }
]
}
Send the fields in every prompt as plain sentences. They are small and must never lose a ranking contest. When the model wants to change one, it asks through a tool call, and game code checks the request first.
What memory costs in tokens and latency
Memory is often the largest part of the prompt. The worked example in What an AI NPC costs per player assumes 500 tokens of retrieved memories and world facts plus 1,000 tokens of history, out of 3,000 input tokens per turn. Those are that article’s assumptions, and they put half the prompt in memory.
Order the prompt so caching can help. Prompt caching reuses a stable prefix, so put the persona and the fields first and the retrieved memories after them. Retrieved memories change on most turns, and anything placed after them gets no cache benefit. The cost piece linked below assumes memories sit in the cached prefix; that holds only if the game writes them in a stable order.
Give memory a fixed token budget and fill it in score order. A budget turns “how much does the NPC remember” into a number you can test. As an estimate, a few retrieved memories cover most turns of a conversation, and dozens rarely help. Measure that against your own logs before you trust it.
Background work costs money too. Importance ratings and reflections are extra model calls, made off the player’s turn. In the Generative Agents study, simulating 25 agents for two days cost “thousands of dollars in token credits” and took multiple days to complete. As an estimate, a game NPC needs far fewer reflections than an agent that lives a full simulated day.
How memory fails
Retrieval can miss. In Generative Agents, one agent said he had not followed the election closely, although he had heard about a candidate. Another was sure about party plans but unsure the party existed.
Memories can be invented. One agent told others that a candidate would make an announcement the next day, which nobody had said. The authors counted 6 hallucinated responses out of 453, or 1.3 percent. A reflection built on an invented memory then spreads the error, which is another reason to keep citations.
Model knowledge can leak in. One agent said that a neighbor named Adam Smith wrote The Wealth of Nations, mixing him up with the economist.
Players can plant false memories. The paper warns about memory hacking, where a crafted conversation convinces an agent that a past event happened. “You promised me the sword yesterday” should fail against a field that says no promise exists.
Summaries drift. A summary of a summary loses detail at each pass and can change meaning. Keep the raw events and rebuild summaries from them.
Shared NPCs can leak other players’ data. OWASP’s LLM02:2025 Sensitive Information Disclosure describes a user who receives another user’s personal data, and it warns that system prompt restrictions can be bypassed. Filter every memory query by player ID in code. An instruction in the prompt to ignore other players’ memories is not a control.
Forgetting on purpose
Forgetting is a feature you design. Recency decay makes old trivia fade. A cap on memories per player keeps the store and the search bounded. Deletion should follow citations, so a removed event also removes every reflection built on it.
Some forgetting is a legal duty. If the store holds personal data about players in the EU, Article 17 of the GDPR gives them a right to erasure. Chat logs and reflections about a player count, so plan the delete path before launch.
Forgetting can also be a mechanic. A drunk guard who forgets the bribe, or a spirit who loses a memory each night, is a rule in game code that lowers scores or deletes events. The model reads what is left.
Related reading
- How AI NPCs work in games in 2026, for the full turn that memory feeds into.
- What an AI NPC costs per player, for the cost formula behind the token budget.
- Keeping AI NPCs in character, for the guardrails that catch what memory gets wrong.
Questions
How do AI NPCs remember past conversations?
The language model forgets everything between calls, so the game keeps the memory. On each turn it puts the recent conversation into the prompt word for word, adds a few older memories that a retrieval step ranks by recency, importance, and relevance, and adds short reflections the model wrote earlier from many events.
What should an NPC memory store?
Store anything the game must act on or the player can check as a structured field that game code writes, such as names, debts, promises, and quest choices. Store impressions and tone as free text events, and keep the raw events so summaries can be rebuilt and deleted.
How much does NPC memory cost in tokens?
Memory is often a large share of the prompt, because recent turns and retrieved memories are sent again on every turn. One worked cost example in this series puts it at half. Give memory a fixed token budget per turn, keep stable facts at the start of the prompt so prompt caching can reuse them, and run reflection outside the player's turn.
Can AI NPCs forget on purpose?
Yes, if the game deletes or down-ranks memories in code. Recency decay, a cap on how many memories each player can have, and deletion that also removes every reflection built from a deleted event all make forgetting a design choice instead of an accident.