Supervised learning tells you the right answer on every example. Reinforcement Learning only gives you a delayed score after you act—and your actions change the future states you see next.
1. The Author vs. The Player (Separating Roles)
When LLMs and RL appear in the same project, it is easy to blur who is doing what. Always separate the three components:
- The Environment (Referee): Pure code or physics simulator (
reset()/step(action)). Deterministic given a random seed; zero AI required inside. - The RL Policy \(\pi_\theta\) (Player): A compact, fast math function (often a few thousand to a few million weights) that maps an observation \(o_t\) to an action \(a_t\) in microseconds (inference).
- The LLM (Optional Author / Designer): May write the training script, reward function, or scenario spec once before training—or may itself be the policy being trained (see Chapter 5).
2. Partial Observability & Factored Actions
Real-world problems almost never hand you a clean chessboard:
- POMDPs: You rarely see the true hidden state (hidden customer intent, unseen software bugs, occluded obstacles). You act on a lossy observation \(o_t = O(s_t)\), which means memory (history window, RNN, or Transformer context) matters.
- MultiDiscrete Action Spaces: Many operational decisions require setting several knobs at once (e.g., inventory reorder + pricing tier + staffing stance). Predicting \(k\) independent heads scales as \(\sum |\mathcal{A}_j|\) instead of exploding combinatorially as \(\prod |\mathcal{A}_j|\).
3. Discounting & Delayed Credit Assignment
The single hardest problem in sequential decision-making is Delayed Credit Assignment:
- You skip preventative maintenance or inventory restocking on Day 1 → immediate reward looks great (
+cash). - On Day 6, the machine breaks or stock runs out → catastrophic penalty arrives days later.
The Discount Factor (\(\gamma\)) controls how far ahead the agent cares:
\[G_t = \sum_{k \ge 0} \gamma^k r_{t+k}\]
If \(\gamma\) is too low, the agent becomes myopic and harvests short-term cash until it crashes. If \(\gamma \approx 0.99\), it wants to plan ahead—but linking Day-1 causes to Day-6 effects through noisy intermediate steps requires solid credit-assignment math.
4. Why Naive RL Loses to Random Guessing
In practice, it is surprisingly common for a hand-rolled RL agent to score worse than a random baseline:
- Zero-initialized Q-Learning trap: If unvisited \(Q(s, a)\) starts at
0.0while real step rewards are negative, every good action you try drops below0.0. Greedyargmaxthen abandons good moves to chase unvisited0.0garbage actions! - Consistent myopia vs. accidental balance: Random mashing accidentally reorders stock or runs maintenance often enough to survive. A broken linear learner discovers "doing nothing saves money today" and becomes consistently wrong.
- Linear policies can't learn sharp thresholds: Operational rules often have
if/elsecliffs (e.g.,inventory < 350orqueue_depth >= 80%). A straight-line policy blurs those cliffs together; a tiny 2-layer MLP (64x64) carves them out cleanly.
5. Two Practical Superpowers: Residual RL & Domain Randomization
Before training from scratch in a single fixed scenario, use two field-tested shortcuts:
- Residual RL: Start with a safe rule-based heuristic or classical controller and let RL learn small corrective overrides on top of it. You skip the catastrophic "learning not to walk off a cliff" phase.
- Domain Randomization: Randomize noise, demand spikes, friction, and random seeds during training so the policy learns robust habits instead of memorizing one sunny-day trajectory.