Behavior Trees, A Star Pathfinding And Where LLM NPCs Actually Fit

Leader ●4 ●19 ●174
calendar_today • schedule3 min read

Most of us are writing agent code all day now, and it is worth knowing that game developers solved a version of the same decision problem twenty years ago without using machine learning for any of it. This is the architecture side of game AI, written for developers who have built decision systems before but have never looked at how games structure theirs.

Why Finite State Machines Stop Scaling

A finite state machine gives an NPC one active state at a time and a set of transition rules. A guard patrols, hears something, investigates, confirms the player, chases, attacks, loses sight, goes back to patrol. It is easy to draw, easy to test, and cheap at runtime, which is why it survived so long.

The problem is that every new state needs transitions defined to and from every existing state it can reach, so transitions grow roughly with the square of the state count. A combat NPC that handles melee, ranged, grenades, cover, retreating, healing, calling for reinforcements and surrendering ends up with dozens of states and hundreds of transitions. At that size nobody can change one behavior without breaking another. Hierarchical state machines help by nesting machines inside states, but they only push the wall further out.

Behavior Trees Took Over For A Reason

A behavior tree is evaluated from the root every tick and execution flows down through the branches. Composite nodes control flow, where a Sequence runs children in order and succeeds only if all succeed, and a Selector tries children in order and succeeds as soon as one does, which is how you express fallback chains. Decorators wrap a single child to invert it, repeat it, or gate it behind a cooldown. Leaves are the actual actions and conditions.

The reason it won is modularity. Each subtree is self contained, so a melee enemy and a ranged enemy share the same perception and navigation subtrees and differ only in the combat branch. Adding a behavior means inserting a branch at the right priority instead of wiring transitions to everything else. Unreal pairs its trees with a blackboard, a shared key value store for things like last known player position, which keeps state out of the tree structure.

The cost is debuggability. What runs depends on the state of every node above it, and since the tree re-evaluates from the root each tick, two branches that keep swapping priority make the NPC thrash between behaviors mid animation. Cooldown decorators, hysteresis on conditions, or letting a committed action finish before re-evaluating are the standard fixes.

Pathfinding Is A Graph Problem, Not A Geometry Problem

A star is still the backbone. It combines the known cost to reach a node with a heuristic estimate of the remaining distance, and explores in order of total estimated cost, which stays optimal as long as the heuristic never overestimates.

The engineering trick is what it runs on. Running A star against raw 3D geometry is hopeless, so levels get baked into a navigation mesh, a set of convex polygons covering everywhere an agent can walk. The search then runs on a polygon graph that is orders of magnitude smaller. On top of that sit the refinements: funnel algorithm string pulling to straighten paths through polygon corridors, hierarchical pathfinding between regions before pathing inside them, and flow fields when thousands of units head for the same destination and you want one precomputed direction field instead of thousands of searches. The full breakdown of the game AI toolkit walks through the steering layer that sits under all of this, the flocking rules that make group movement look natural, and the enemy patterns that get built from these pieces.

Where LLM NPCs Actually Fit

Classic game AI is deterministic, and that is a feature. Designers can author the experience, tune difficulty precisely, test every path, and know the NPC will never say something that breaks the fiction. The cost is that NPCs feel scripted, because they are.

Language models buy freeform dialogue, memory of past conversations and reactions to things no designer anticipated. They also bring latency measured in hundreds of milliseconds, per interaction cost, content safety work, and nondeterminism, which means you cannot guarantee a story beat or a difficulty curve. That last one is the real blocker for anything load bearing.

So the shipping pattern is hybrid. Classic systems run combat, navigation and world simulation, and generative models handle dialogue, quest text and reactive narration, the places where surprise is worth more than control.

The Takeaway

If you are building agent systems, the useful transfer is not the algorithms, it is the discipline. Games decided decades ago that predictable behavior beats clever behavior wherever failure is visible to the user, and they kept the unpredictable parts at the edges. That is a reasonable default for agents too.

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

A Textile Mill's Dyehouse Ramp Curve: Get One Step Wrong and the Sheet Belongs to the Master

aiomniu - Oct 18

Cisco's Amy Chang: A Model's "Passport" Doesn't Tell You Where It Actually Came From

Tom Smithverified - Aug 27

I’m a Senior Dev and I’ve Forgotten How to Think Without a Prompt

Karol Modelski - Mar 19

The Sovereign Vault — A Comprehensive Guide to Protocol-Driven AI

Ken W. Algerverified - Jun 4

Keeping LLM NPCs Fast and Affordable in a Real Game

AIAppsAPI - Oct 5
chevron_left
4.9k Points • 197 Badges
United States • t.co/5LlztlB5C5
117Posts
19Comments
32Connections
Our AI Apps are a self expanding AI SaaS ecosystem used to create the custom web application of your... Show more

Related Jobs

View all jobs →

Commenters (This Week)

2 comments

Contribute meaningful comments to climb the leaderboard and earn badges!