Guide · updated November 2026
AI NPCs: how game characters think.
An NPC is any character the player doesn't control. For forty years their "AI" was clever rules; now language models let them hold real conversations. Here's how both work, when to use which, and a small NPC you can talk to right on this page.
- Live NPC demo
- FSM to LLM
- Code examples
- Design tips
What is an NPC in games?
An NPC (non-player character) is any character in a game that the player doesn't control: shopkeepers, enemies, quest givers, villagers. NPC AI decides what they do, traditionally with state machines or behaviour trees, and increasingly with language models that generate dialogue in response to what the player says.
NPC AI techniques, from simple to smart
| Technique | How it works | Good for | Downside |
|---|---|---|---|
| Finite state machine | States (idle, patrol, chase, attack) with rules to switch | Simple enemies, guards | Gets tangled past ~10 states |
| Behaviour tree | A tree of tasks checked top-down each tick | Most action-game AI | Can feel scripted |
| Utility AI | Scores every possible action, picks the best | Sims-style needs, tactical choices | Tuning scores takes time |
| GOAP | Plans a sequence of actions to reach a goal | Squad tactics, emergent behaviour | Harder to debug |
| Dialogue trees | Hand-written branching conversations | Story-driven games | Lots of writing; limited choices |
| LLM dialogue | A language model writes replies from a character prompt and game state | Open conversation, emergent stories | Cost, latency, staying in character |
A guard in 20 lines: a state machine
The classic. Each frame, the guard checks its state and the world, and maybe switches.
type State = "patrol" | "chase" | "search";
function updateGuard(g: Guard, player: Player, dt: number) {
const sees = canSee(g, player);
switch (g.state) {
case "patrol":
walkToward(g, g.waypoints[g.next], dt);
if (arrived(g)) g.next = (g.next + 1) % g.waypoints.length;
if (sees) g.state = "chase";
break;
case "chase":
walkToward(g, player.position, dt * 1.6);
if (!sees) { g.state = "search"; g.lastSeen = player.position.clone(); g.timer = 4; }
break;
case "search":
walkToward(g, g.lastSeen, dt);
g.timer -= dt;
if (sees) g.state = "chase";
else if (g.timer <= 0) g.state = "patrol";
}
}An LLM NPC: the prompt is the character
With a language model, you describe the character and pass in the game state each turn. Keep game rules in code, and let the model only talk.
const system = `You are Brann, the village blacksmith in a fantasy game.
Personality: gruff, kind underneath, proud of his work.
You know: the dragon burned the north farm 3 days ago; swords cost 40 gold.
Rules: reply in 1-3 sentences, stay in character, never invent items not in INVENTORY.
If the player wants to buy, reply and add [OFFER:item] at the end.`;
const user = `INVENTORY: iron sword (40g), shield (25g)
PLAYER GOLD: ${player.gold}
RELATIONSHIP: ${npc.trust}/10
PLAYER SAYS: "${input}"`;
// Your game parses [OFFER:...] and runs the real shop logic itself.Making LLM NPCs work in a real game
- Keep authority in code: prices, quests and damage come from game state, never from the model
- Give each NPC a short card: personality, what they know, what they don't, how they speak
- Pass only relevant state each turn; long histories get slow and expensive
- Summarise memory every few turns instead of sending the whole conversation
- Cap reply length and stream text so players aren't waiting
- Have a scripted fallback line for errors and offline play
- Filter player input and model output; players will try to break your NPC
- Use small, fast models for chatter and bigger ones only for key story moments
Do players want AI NPCs?
In playtests, open conversation is exciting for about ten minutes and then players want it to matter: the NPC should remember them, react to what they did and change something in the world. Free chat without consequences becomes a novelty.
The best current designs mix both worlds: hand-made quests and structure, with language models filling in reactions, gossip and small talk around them. Mysteries and social games, where talking is the gameplay, are where LLM NPCs shine most.
Frequently asked questions
What does NPC stand for?
Non-player character: any character in a game not controlled by a player. The term comes from tabletop role-playing games.
How does NPC AI work?
Most NPCs use state machines or behaviour trees: rules that pick an action based on what the NPC senses. Some use utility scoring or planning (GOAP). Newer games use language models for dialogue on top of that.
Are AI NPCs used in real games?
Yes, increasingly, mostly for dialogue and companions. Major studios and many indies are experimenting with language-model characters, usually alongside traditional AI for movement and combat.
How much does an LLM NPC cost to run?
It depends on model and usage. Short replies with a small model cost a fraction of a cent each; long memories and large models add up quickly with many players. Cache, summarise and cap replies.
Can an NPC run without the internet?
Rule-based NPCs always can. Small language models can run on-device too, but they need memory and a capable GPU or NPU; most games still call a server.
EvonX AI game maker
Got the asset? Now make the game.
Describe your game in one sentence and EvonX builds it: code, art, sound and a link you can share. Free to start, no download, plays on any phone.