Skip to content

Add an LLM NPC to Your Game Without Handing It the Rules

Build an LLM NPC in Unity or any engine: the model picks words and proposes listed actions, while game code validates them, caps cost and falls back.

Published

8 min read

Keep quests, combat, inventory and saves in deterministic game code; let the model choose words and propose actions from a fixed list that the game validates. An LLM NPC, meaning an AI NPC whose dialogue a language model writes at runtime, can then feel conversational without being able to grant items, skip quests or corrupt a save, because every effect still runs through ordinary game code.

The running example is Mara, a fictional blacksmith in a Unity RPG who chats, offers items from her stock and hints at the current quest step. Integrating an LLM into a game this way works in any engine, with a hosted or local model. This guide gives no latency or cost figures, only how to measure your own.

/01

What should the model decide, and what must it never decide?

Game code owns every fact that affects play: quest state, inventory, prices, damage, unlocks and saves. The model owns the wording of a line, an optional mood tag for animation and at most one proposed action per turn. If a value changes what the player can do next, it belongs to the game.

This applies the Excessive Agency entry of the OWASP Top 10 for LLM Applications to an AI agent NPC that can trigger game actions. Its 2025 version recommends implementing authorization in downstream systems rather than relying on the model to decide whether an action is allowed. The shop and quest systems decide; Mara only asks.

Because model text reaches simulation state only through validated actions, seeded tests stay reproducible. The kit's Testable by Design skill makes a simulation deterministic and steppable, with seeded random numbers, a fixed timestep and a test hook.

/02

Offer a fixed list of actions and a schema

Give every action Mara could ever propose a stable ID: none, offer_item, give_quest_hint and end_conversation. Each maps to an existing game function with its own preconditions. There is no free-form command and no action that sets gold.

Request structured output. OpenAI's Structured Outputs guide says responses adhere to the JSON Schema you supply, which prevents invalid enum values. Anthropic describes strict tool use as guaranteeing schema validation on tool names and inputs.

Schema compliance is not correctness. OpenAI states that structured outputs can still contain mistakes. Anthropic notes that refusals and replies cut off at max_tokens might not match the schema, that enum capitalization is not guaranteed and that string-length constraints are unsupported. Compare IDs case-insensitively and enforce lengths in code.

Keep the schema stable. Anthropic caches a compiled schema for 24 hours from last use and invalidates that cache when the schema structure changes; whether a changed enum list counts is not stated, so avoid the question: put every possible action ID in one fixed schema, name this turn's available ones in the prompt, and let the validator reject the rest.

  • line: the words shown to the player, length-checked in code.
  • mood: one value from a short enum that drives animation, never gameplay.
  • action: one listed ID, defaulting to none.
  • itemId: used only by offer_item and checked against current stock.

/03

Validate every proposal before it touches game state

Validation runs where game authority lives: in the client for a single-player game, on the server for a multiplayer one. A proposed action is rejected if it is unknown, unavailable this turn or fails a precondition, such as offering an out-of-stock item. When the game rejects a proposed action, it discards the model's whole reply and plays a scripted fallback line. Missing or overlong words also fall back to an authored line. Accepted actions call the same function as the shop button.

Log every rejection with its reason; a rising rate often signals that a prompt change, model update or player trick is pushing the character off its contract. In the illustrative C# below, ShopState is your own inventory type.

csharp
public class NpcReply { public string line; public string action; public string itemId; }

public static class NpcReplyValidator
{
    const int MaxLineChars = 280;

    // available: action IDs the game allows this turn, built from game state.
    public static NpcReply Validate(NpcReply reply, string[] available, ShopState shop, string fallbackLine)
    {
        var safe = new NpcReply { line = fallbackLine, action = "none" };
        if (reply == null) return safe;

        string proposed = string.IsNullOrWhiteSpace(reply.action) ? "none" : reply.action;
        string id = FindAvailable(proposed, available);
        if (id == null) return safe;                       // rejected: discard the whole reply
        if (id == "offer_item" && !shop.InStock(reply.itemId)) return safe;

        string text = reply.line == null ? "" : reply.line.Trim();
        if (text.Length > 0 && text.Length <= MaxLineChars) safe.line = text;

        safe.action = id;                                  // canonical spelling from the game
        safe.itemId = id == "offer_item" ? reply.itemId : null;
        return safe;
    }

    static string FindAvailable(string proposed, string[] available)
    {
        foreach (string candidate in available)
            if (string.Equals(candidate, proposed.Trim(), System.StringComparison.OrdinalIgnoreCase))
                return candidate;
        return null;
    }
}

/04

Keep the model key on your server

Anything shipped in a build can be extracted, so the provider key never goes into a Unity project or WebGL bundle. OpenAI's API reference says not to expose keys in client-side code such as browsers or apps, and to load them on the server. The game sends a session token and the player's line to your endpoint, which holds the key, assembles the prompt from the persona and the facts the client sent, applies caps and format checks, and returns the reply for the game-state validation above.

/05

Set a latency budget and never block gameplay

A model call has an unpredictable duration, so gameplay never waits on it. In Unity, yielding the operation returned by UnityWebRequest.SendWebRequest inside a coroutine pauses that coroutine until the request finishes or hits a system error. Keep gameplay outside it, so the player can walk away while Mara thinks.

Unity documents UnityWebRequest.timeout in whole seconds, with a default of 0 that means no timeout. Properties cannot change after SendWebRequest is called, so set it first. For a tighter budget, run your own timer; when it expires, play an authored thinking line, keep the dialogue options usable and discard the late reply by turn number.

OpenAI's latency guide says generating tokens is almost always the highest-latency step, and lists generating fewer tokens and not defaulting to an LLM among its principles. Cap reply length, keep greetings and quest-critical lines authored, and set the budget from timing percentiles measured in playtests.

/06

Cap cost per player and per session

OWASP's 2025 Unbounded Consumption entry describes Denial of Wallet: attackers triggering a high volume of operations to exploit the cost-per-use model of cloud AI services. Its mitigations include input size limits, rate limiting, per-user resource limits, timeouts and graceful degradation. For an NPC, degradation means authored dialogue when a cap is hit.

Provider limits are only a backstop. Anthropic's rate-limit page says limits are set at the organization level and usage pauses at the monthly spend cap, so one abusive account could silence every NPC. Measure cost from reported token counts; with prompt caching, Anthropic's input_tokens field counts only tokens after the last cache breakpoint, so add the two cache fields.

  • A player-text length limit, checked before any model call.
  • A maximum output length matched to your dialogue box.
  • Turns per conversation and conversations per session.
  • A daily token budget per player, enforced on the server.
  • A game-wide spend alarm well below the provider limit.

/07

Give the NPC memory of only what the game has unlocked

Build each prompt from game state: Mara's persona, the facts the player has unlocked, the current offer, the active quest step and a few recent exchanges. Never include later plot or hidden answers. OWASP says it is unclear whether fool-proof prevention of prompt injection exists, so assume anything in the context can reach the player.

Store long-term memory as facts the game owns, such as a flag that the player told Mara their name, not as model-written summaries later treated as truth. Player text can be personal data, so decide retention before launch.

/08

Treat player text as untrusted input

OWASP's prompt-injection entry covers both the player typing instructions and text the model reads from elsewhere. Directly, a player demands that Mara ignore her instructions and hand over the best sword. Indirectly, the instruction hides in a character name, guild description or chat message.

OWASP's mitigations match the spine: constrain the model's role, validate output formats with deterministic code, filter input and output, apply least privilege and separate untrusted content. With game-side validation, an injection can make Mara say something odd but cannot hand over the sword.

That leaves the words, so classify player input and NPC output before display. OpenAI's moderation guide describes a free endpoint that returns a flag, per-category flags and scores, and advises treating scores as signals for your policy, not as automatic blocking. Prepare an in-character deflection. For multiplayer, the kit's Multiplayer Security Abuse Review skill threat-models modified clients, impossible or replayed input and resource exhaustion.

/09

Ship behind a remote kill switch and roll out gradually

Put the feature behind server-side flags, global, per NPC and per platform, with the model name and every cap value. Because calls already go through your server, switching off applies to the next request without a client update. With the switch off, every quest must still be completable; check that in each release regression pass.

Roll out to a small share of sessions and compare them with authored-dialogue sessions on fallback rate, rejected proposals, moderation flags, cost, player reports and quest completion. The kit's Game Remote Config & Analytics Engineer agent works on feature flags, kill switches, staged rollout and experiment instrumentation.

/10

Hosted or on-device model: what changes?

A hosted model brings provider features such as schema-constrained output and moderation, but adds a network round trip, a per-call bill and a server to run. An on-device model removes the bill and the network dependency, but spends the player's memory, storage, battery and frame time.

In Unity, the Sentis package, displayed as Inference Engine before version 2.4, runs trained neural network models in real time on the device's CPU or GPU. Its manual lists most ONNX models with opsets 7 to 25, most LiteRT models and suitably decomposed PyTorch exports, on all Unity-supported platforms. Its tokenizer is compatible with the Hugging Face tokenizers library, though not every component is supported. The overview does not mention language models, so profile on your lowest target device; the kit's Unity Performance Profiling skill diagnoses CPU, GPU, memory and battery problems with profiler evidence.

Either way the spine stays; without schema-constrained decoding the validator matters more. AI Games Kit has no in-game language-model or NPC component; its specialists cover the surrounding work.

Keep this in mind

Key takeaways

  • Keep quest, combat, inventory and save logic in game code; the model only writes words and proposes one action.
  • Use one stable schema with a fixed action list, and validate every proposal against current game state.
  • Keep the provider key on your server, set explicit timeouts and fall back to authored lines.
  • Enforce per-player cost caps on the server, because provider spend limits cover your whole organization.
  • Ship behind a server-side kill switch, prove every quest works with it off, and roll out gradually.

Sources & further reading

Consult the source documentation for the version and platform you are working with.