An LLM can make a non-player character sound flexible. It should not become the authority on whether the bridge is open, who owns the sword or which quest reward the player received.
The durable design keeps game truth in deterministic systems and uses the model as a constrained dialogue planner. That separation makes the feature testable, cacheable and safe to turn off.
Updated
The architecture is provider-neutral. Model context limits, caching behavior, pricing and moderation controls change frequently; pin the deployed model and review its current documentation.
Tested with
Generative Agents paper · Godot HTTPRequest · OpenAI latency and prompt-caching guidance
Separate state by owner:
- Authoritative game state contains quest flags, inventory, relationship values, locations and permissions. Only game code writes it.
- Narrative memory contains reviewed summaries of prior interactions that may influence tone or recall. It is derived and can be rebuilt.
- Conversation context contains the recent turns required to answer the current line. It is short-lived.
The model receives a projection of these stores. It does not receive a database connection or a generic set_state tool.
{
"npc": "Mara",
"scene": "workshop_after_power_failure",
"facts": ["generator_is_broken", "player_has_spare_fuse"],
"relationship": "cautious_ally",
"allowed_intents": ["ask_for_fuse", "explain_failure", "end_conversation"]
}This projection is small enough to inspect and strict enough to validate.
Ask the model for structured output containing an allowed intent, emotional delivery and candidate line. The game validates the intent, performs any state transition itself and only then displays approved prose.
player input + state projection
-> model proposal
-> schema and policy validation
-> deterministic game transition
-> rendered line and logged outcomeIf the model proposes give_legendary_sword when that intent is not allowed, reject it. A fallback line can preserve the scene without corrupting the save.
Key takeaway
Generated dialogue may interpret state; it must not invent state. The game owns facts and transitions, while the model proposes language inside a finite set of narrative actions.
Dialogue is interactive, so average latency is not enough. Set a target for time to first visible response and a separate ceiling for total completion.
Reduce perceived delay with techniques that do not weaken correctness:
- start a deterministic listening animation immediately;
- stream text only after the structured envelope is validated;
- keep stable character and policy instructions at the beginning of the prompt;
- retrieve only memories relevant to the current character and scene;
- cap output length to what the subtitle window can display;
- cache safe, repeated ambient lines;
- precompute likely greetings during scene load.
Provider prompt caching can reduce repeated prefix work, but it is an optimization rather than game state. A cache miss must not change behavior.
The Generative Agents research architecture combines observations, retrieval, planning and reflection. A shipped game needs stronger controls around each write.
Do not store every player utterance forever. Extract memory candidates, classify their scope and require thresholds for salience, safety and contradiction. A useful record includes the source event and the save-game version that produced it.
Examples:
- “The player returned Mara’s toolkit” can become a durable relationship memory.
- “The player is standing near the door” belongs in current scene state.
- A model-generated claim that Mara has a sister is discarded unless authored world data permits it.
Summaries should point back to source events so a bug report can explain why the character “remembered” something.
Game ratings and community expectations are product constraints, not prompt suggestions. Use layered controls:
- restrict the topics and actions available in a scene;
- validate player input before adding it to durable memory;
- validate structured model output before rendering;
- block personal data and disallowed content according to the game policy;
- provide a deterministic fallback line;
- expose reporting and reset controls;
- avoid presenting fictional characters as professional authorities.
For a game used by children, the safest scope may be authored branches with AI only helping writers offline.
Track cost as a distribution:
dialogue cost = input + retrieved memory + generated output
+ moderation + retries + failed requestsMeasure per scene and per active player hour. Long character biographies copied into every request can dominate input even when lines are short. Stable prefixes, compact state projections and bounded memory retrieval help both cost and latency.
Add hard limits: requests per minute, tokens per scene and daily spend per environment. When a limit is reached, the game should continue with authored lines.
- Define the smallest scene where flexible language creates real player value.
- List authoritative state fields and allowed narrative intents.
- Build a structured proposal schema and deterministic fallback.
- Add one bounded memory type with source-event references.
- Measure p50 and p95 first-response time, total time and tokens.
- Red-team input, state contradiction, prompt injection and network failure.
- Playtest narrative quality with and without the model before expanding scope.
Use authored dialogue for tutorial instructions, legal disclosures, purchases, irreversible choices and scenes whose timing depends on exact lines. Generated dialogue fits optional conversation, flavor reactions and repeatable ambient interactions when failure has a safe fallback.
The design goal is not an NPC that can say anything. It is a character that can say more while remaining inside the game’s rules.
- Generative Agents: Interactive Simulacra of Human Behavior — the observation, memory, retrieval, reflection and planning architecture.
- Godot HTTPRequest — the engine node for HTTP requests and platform-specific networking notes.
- OpenAI latency optimization — current guidance on output length, request structure and perceived latency.
- OpenAI prompt caching — behavior and observability for reusable prompt prefixes.
