Behavior AI Intermediate

While dialogue systems handle what NPCs say, behavior AI determines what they do. This lesson covers how to combine traditional behavior trees with LLM-powered reasoning to create NPCs that can plan, react to world changes, and adapt their behavior based on player interactions.

Behavior Trees + LLMs

The hybrid architecture uses behavior trees as the execution layer and LLMs as the reasoning layer. The behavior tree handles reliable, frame-by-frame actions (movement, combat, animations), while the LLM makes high-level decisions during "thinking" moments.

Decision Architecture

LayerEngineUpdate RateExamples
ReflexesBehavior TreeEvery frameDodge, flee from danger, follow path
TacticsUtility AIEvery few secondsChoose target, select weapon, take cover
StrategyLLMOn triggerPlan daily schedule, decide loyalty, set goals

LLM Decision Calls

When the NPC reaches a decision point (e.g., a player asks for help, a new threat appears), query the LLM with the NPC's current state, goals, and options. The LLM returns a structured decision that the behavior tree can execute.

Latency Management: LLM calls take 200ms-2s. Never block the game loop waiting for a response. Queue decisions asynchronously and have the NPC perform "thinking" animations (looking around, rubbing chin) while waiting. Pre-cache common decisions at level load.

Emergent Behavior

When NPCs share information through the memory system and make LLM-driven decisions, emergent social dynamics arise naturally. NPCs may form alliances, spread rumors, or avoid locations based on past experiences - creating living worlds without explicit scripting.

Schedules and Routines

Use the LLM to generate daily schedules for NPCs based on their role and personality. A blacksmith NPC might work at the forge in the morning, eat lunch at the tavern, and close shop at sunset. The behavior tree executes the schedule while the LLM handles interruptions.

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.