← TinyGPT · docs · devlog · roadmap · speedup
source: docs/learn/session-07-behavior-learning.md · view on GitHub ↗

Session 7 — How models learn behavior (supervised, imitation, reinforcement)

Sessions 1–6 covered models that predict the next TOKEN. But agents (NPCs in a game, robots, game-playing AIs) don’t predict tokens — they pick ACTIONS. This session covers how models learn to act, not just to write. Direct application to Aliveville’s NPC AI architecture.

Where we are

A language model’s job is: given the text so far, predict the next token. An agent’s job is: given the situation, pick an action. Both are “functions on data” — the math we built in Sessions 1–5 still applies — but the training signal is different.

This session covers the three main paradigms for learning behavior:

  1. Supervised / imitation — train on (state, correct_action) pairs.
  2. Reinforcement — learn from reward signals.
  3. Hybrid — almost every modern agent combines both.

The setup: agents and the decision loop

An agent lives in a loop:

state₀ → (pick action) → state₁ → (pick action) → state₂ → ...
                  ↑                          ↑
              decision                  decision

The agent’s “model” — what we train — is a function from state to action. The question is: how do we train it?

Language models live inside this loop too. The “state” is the conversation so far; the “action” is which next token to emit. Same loop, different state/action spaces.


Paradigm 1: Supervised / Imitation learning

Collect (state, action) pairs from a demonstrator (often a human expert). Train a model to predict the action given the state.

Mathematically identical to supervised learning. The “label” is just an action instead of a class or a token.

Concrete instances:

What you get: a model that mimics the demonstrator. Quality of model = quality of demos. If demos were good, model is competent. If demos were mediocre, model is mediocre.

This is sometimes called behavioral cloning (BC) or imitation learning when explicitly framed as “copy what a human did.” All the same thing mathematically.


The compounding error problem (imitation’s central flaw)

Pure behavioral cloning has a devastating failure mode. The setup:

Classic example: a self-driving car trained purely on perfect human driving demos doesn’t know how to recover from being half-off-road — because no demo ever drove half-off-road. The car drifts slightly, then totally fails.

For language models the equivalent: a chatbot trained purely on perfect human responses doesn’t know how to recover from a conversation it already messed up. Modern chatbots compensate with constant resets and “sorry, let me start over” patterns.

Mitigations:


Paradigm 2: Reinforcement learning

Instead of (state, correct_action) pairs, you have an environment, a reward signal, and a policy. The agent acts, observes rewards, updates the policy to favor high-reward actions.

The pieces:

The training loop:

  1. Policy acts in environment, generates trajectories.
  2. Compute “return” for each step (cumulative discounted future reward).
  3. Update policy: “make actions that led to high return more likely.”
  4. Repeat.

The math is more involved than supervised learning because you’re not just minimizing loss over fixed data. You’re dealing with:

Two main families of RL algorithms:

Q-learning

Learn a function Q(state, action) = “expected total future reward if I take this action from this state.” Pick actions that maximize Q. Examples: DQN (deep Q-network, Atari benchmark), tabular Q-learning for small problems.

Suited to discrete action spaces, well-defined state transitions.

Policy gradient

Directly learn a policy π(state) → action probabilities. Update by “make actions that led to high reward more probable, make actions that led to low reward less probable.” Examples: REINFORCE, A2C, PPO (used in RLHF and OpenAI Five), TRPO.

Suited to continuous action spaces, where Q-learning becomes infeasible. PPO is the modern workhorse — robust, easy to tune.

For our purposes, the family difference matters less than the core insight: RL learns from rewards, not labels.


When each paradigm fits

Imitation works when:

ConditionWhy
Lots of expert demos availabletraining signal is rich
Want human-like behavior (NPCs, dialogue)mimicking IS the goal
Deployment closely matches trainingminimal compounding-error risk
Need fast development cycleimitation training is just supervised learning

Imitation fails when:

ConditionWhy
Need superhuman performancecan’t exceed demonstrations
Demos are scarce or noisynot enough signal
Deployment will drift off-distributioncompounding errors
Need to discover novel strategiesmodel only knows what was shown

RL works when:

ConditionWhy
Clean simulator + clean rewardRL is in its element
Need superhuman performanceRL can surpass demonstrations
Recovery from mistakes mattersRL learns it naturally
You have unlimited “training rollouts” cheapRL is sample-hungry

RL fails when:

ConditionWhy
Sparse rewards (signal only at episode end)exploration is brutal
Hard to define rewardthe whole approach depends on it
Real-world deploymenttrial-and-error is costly
Limited simulator qualityRL exploits sim/real gap

The modern hybrid: warm-start + refine

Almost every successful modern agent combines both:

SystemImitation stageRL stage
AlphaGotrained on human professional Go gamesself-play RL refinement
AlphaStar (StarCraft II)imitation on replay archivesself-play RL refinement
OpenAI Five (Dota)minimal — mostly pure RL with curriculum + shaped rewards(the main course)
ChatGPT / Claude (RLHF)pretrain (SSL) + SFT (imitation on human demos)PPO on human preference judgments
Robotics arms (recent)sim + imitation from teleopRL in sim, sim-to-real transfer

The general pattern:

Neither alone gets you both fast convergence AND ability to surpass demonstrations. The combination beats either component used alone.


RLHF specifically (since this is what TinyGPT supports)

RLHF = Reinforcement Learning from Human Feedback. The three-stage recipe used to turn a base LM into ChatGPT/Claude:

  1. Pretrain. Self-supervised on web text. Model learns language structure. (TinyGPT’s tinygpt train does this.)
  2. SFT (Supervised Fine-Tune). Imitation on (prompt, ideal_response) pairs from humans. Teaches the assistant persona. (TinyGPT’s tinygpt sft does this.)
  3. RL on preferences. Humans rate pairs of responses; train a reward model on the ratings; then use PPO (policy gradient) to fine-tune the LM to produce responses the reward model rates highly. (TinyGPT’s tinygpt dpo is a related but simpler variant.)

What gets learned at each stage:

The whole recipe is imitation → RL exactly as described above.


For Aliveville specifically

Different NPC behavior layers want different approaches:

LayerApproachWhy
Dialogue / conversationLLM + prompt engineeringcharacter description + memory in prompt; works at any quality
Combat / decision-makingImitation from player footagewant HUMAN-LIKE NPCs, not optimal-but-weird
World event responsesLLM + state in promptflexible, plausible reactions to novel events
Long-term character consistencyImitation + light RLwarm start from human play + reward shaping for “in character” behavior
Quest dynamicsHand-coded + LLMmost rule-based, LLM for in-character interactions inside the rules

Two implications you might not have considered:

  1. Imitation from your own players’ footage = competitive moat. Anyone can hit an OpenAI/Anthropic API; nobody else has YOUR players’ actual playthroughs. NPCs trained on your data behave like Aliveville players, which is exactly the “alive” feel you want. This data becomes a primary asset of the product.
  2. The “perfect AI is uncanny” effect. Players will find an NPC that plays “optimally” weird — too fast, too aggressive, too predictable in its weakness exploitation. NPCs that play “like a person who sometimes makes mistakes” feel real. The classic RL critique that “imitation can’t surpass humans” becomes a feature here: you don’t want to surpass humans, you want to mimic them well.

Self-check

Don’t peek:

  1. Why is pure behavioral cloning often a bad choice for self-driving cars but fine for chatbot SFT?
  2. In RL terms, what’s the “reward” in language-model RLHF? (Hint: it’s not “did the model output the right next token.”)
  3. You want to train an NPC to behave like a wise old wizard. You have no game footage and no reward function — only 200 written conversations with this character. What approach?
  4. Trap question: if RL can surpass imitation, why do modern LLMs still rely on SFT (imitation) for the bulk of post-training, with only a thin RL layer on top?

Where this connects