Reinforcement Learning: The AI That Learns from Trial and Error
A comprehensive guide to reinforcement learning — how it works, key algorithms, major breakthroughs, RLHF for LLM alignment, and how to get started with practical RL projects.
What is Reinforcement Learning?
Reinforcement learning (RL) is a type of machine learning where an agent learns to make decisions by interacting with an environment and receiving feedback in the form of rewards or penalties. Unlike supervised learning, which learns from labeled examples, RL learns from consequences — the agent discovers what works through trial and error.
Think of training a dog: you don’t explain to the dog what “sit” means. You give a treat when it sits (reward) and withhold treats when it doesn’t (penalty). Over time, the dog learns to associate sitting with treats. RL works the same way.
How RL Differs from Other Types of Machine Learning
| Type | What It Learns From | Example Use Case |
|---|---|---|
| Supervised Learning | Labeled data (input → correct output) | Spam detection, image classification |
| Unsupervised Learning | Unlabeled data (patterns and structure) | Customer segmentation, anomaly detection |
| Reinforcement Learning | Rewards and penalties from interaction | Game playing, robotics, autonomous driving |
RL is uniquely suited for problems where you can’t simply label every possible correct action — like playing chess, navigating a robot through a warehouse, or negotiating a business deal. The space of possibilities is too large, so the agent must learn to figure things out on its own.
The RL Framework: Agent, Environment, Reward
Every RL problem can be broken down into a loop:
- Agent observes the current state of the environment
- Agent takes an action
- Environment transitions to a new state and provides a reward signal
- The agent updates its policy (strategy) based on the outcome
- Repeat
Core Concepts
- Agent: The learner or decision-maker (e.g., a chess-playing AI)
- Environment: Everything the agent interacts with (e.g., the chess board and opponent)
- State: The current situation the agent observes
- Action: A move the agent can make
- Reward: A numerical signal indicating how good or bad the action was
- Policy: The agent’s strategy — a mapping from states to actions
- Value Function: An estimate of future cumulative reward from a given state
- Model (optional): The agent’s understanding of how the environment works
The Goal: Maximize Cumulative Reward
RL agents don’t just optimize for the next immediate reward — they plan for the long term. This is captured by the return, which is the sum of all future rewards, often with a discount factor (gamma, γ) that weights near-term rewards more heavily than distant ones.
Exploration vs Exploitation
This is the central tension in reinforcement learning:
- Exploitation: Choosing the action you know works well (sticking with what you know)
- Exploration: Trying actions you haven’t tried much (discovering potentially better strategies)
A purely exploitative agent will never discover better strategies. A purely exploratory agent will never capitalize on what it has learned. Every RL algorithm must balance these two forces.
Common Exploration Strategies
- Epsilon-greedy: With probability ε, take a random action; otherwise, take the best-known action
- Upper Confidence Bound (UCB): Prefer actions with high uncertainty and high potential
- Thompson Sampling: Maintain a probability distribution over the value of each action
- Entropy Bonus: Add a bonus to the reward for maintaining uncertainty (common in deep RL)
Key RL Algorithms
Tabular Methods (Small, Discrete Environments)
These work when the number of possible states is small enough to store in a table:
Q-Learning
- Learns a Q-value for every state-action pair: “How good is it to take this action in this state?”
- Model-free: doesn’t need to know how the environment works
- Off-policy: learns the optimal policy regardless of which actions it actually took during training
SARSA (State-Action-Reward-State-Action)
- Similar to Q-learning but on-policy: learns the value of the policy it actually follows
- More conservative and safer in environments where bad actions have real consequences
Policy Gradient Methods
Instead of learning action values, policy gradient methods directly optimize the policy. These are especially useful when the action space is continuous (e.g., how much torque to apply to a robot joint).
- REINFORCE: The simplest policy gradient algorithm — collect a full episode, then increase the probability of actions that led to high rewards
- Actor-Critic: Combines policy gradients (the “actor”) with a value function (the “critic”) for more stable and efficient learning
Modern Deep RL Algorithms
When neural networks are used to approximate the policy or value function, we enter the realm of deep reinforcement learning:
| Algorithm | Type | Key Innovation | Used For |
|---|---|---|---|
| DQN (Deep Q-Network) | Value-based | Experience replay + target network | Atari games, discrete action spaces |
| PPO (Proximal Policy Optimization) | Policy gradient | Clipped updates for stable learning | Robotics, game AI, RLHF |
| SAC (Soft Actor-Critic) | Actor-critic | Entropy maximization for exploration | Continuous control, robotics |
| A3C (Asynchronous Advantage Actor-Critic) | Actor-critic | Parallel training for faster learning | Games, navigation |
PPO has become the default algorithm in much of the industry because it’s simple, stable, and works well across a wide range of problems — including RLHF for training LLMs.
Deep Reinforcement Learning
Deep RL combines deep neural networks with reinforcement learning algorithms to handle high-dimensional, complex environments. This is what made AlphaGo and modern RL systems possible.
Why Neural Networks?
In real-world problems, the number of possible states is astronomical. A Go board has more possible configurations than atoms in the universe. You can’t store a Q-table for that. Neural networks act as function approximators — they learn to generalize across states and predict good actions for situations they’ve never seen before.
Key Innovations That Made Deep RL Work
-
Experience Replay (DQN): Store past experiences in a buffer and randomly sample from it during training. This breaks harmful correlations between consecutive experiences and allows the network to learn from rare but important events multiple times.
-
Target Networks (DQN): Maintain a separate, slowly-updated copy of the network to compute target values. Without this, the learning target keeps moving and training becomes unstable — like trying to hit a moving target while the target’s movement depends on your aim.
-
Generalized Advantage Estimation (GAE): A technique for balancing bias and variance in policy gradient estimates, making training both efficient and stable.
RLHF: How Reinforcement Learning Made LLMs Useful
Perhaps the most impactful RL application today is Reinforcement Learning from Human Feedback (RLHF) — the technique that transforms raw language models into helpful, harmless assistants.
How RLHF Works
- Pre-train a large language model on vast text corpora (standard next-token prediction)
- Collect human preferences: Show human raters pairs of model responses and ask which they prefer
- Train a reward model that predicts human preferences based on these comparisons
- Fine-tune the LLM with PPO: Use the reward model’s scores as the reward signal, optimizing the LLM to generate responses humans will prefer
- An additional KL penalty prevents the model from drifting too far from its original distribution, preserving its underlying knowledge and capabilities
Beyond Human Feedback: RLVR and Constitutional AI
The latest approaches reduce dependence on expensive human feedback:
-
RLVR (RL with Verifiable Rewards): Use automatically checkable objectives — does the code execute? Is the math proof valid? This scales RL training far beyond what human raters can provide.
-
Constitutional AI (Anthropic): Train models to follow a “constitution” of principles, using the model itself to generate critiques and revisions, reducing the need for human feedback.
Major Breakthroughs in RL
AlphaGo and AlphaZero (DeepMind, 2016-2018)
AlphaGo defeated world champion Lee Sedol using deep RL trained on millions of self-play games. Its successor AlphaZero learned chess, Go, and shogi from scratch — with no human data — reaching superhuman performance in each within hours of training.
OpenAI Five (2018)
An RL system that competed at a professional level in Dota 2, a complex multiplayer game requiring teamwork, long-term planning, and handling imperfect information. Trained on the equivalent of 45,000 human years of gameplay.
Practical Deployments
- Data center cooling: Google used RL to cut cooling costs by 40%
- Chip design: AI-designed chip floorplans now outperform human engineers
- Robotics: Dexterous manipulation, locomotion, and assembly tasks
- Trading: RL-based strategies deployed in quantitative hedge funds
- Self-driving: Trajectory planning and decision-making in autonomous vehicles
Challenges and Limitations
Sample Inefficiency
RL typically requires millions of interactions to learn a task — often impractical in the real world. Simulators help, but models often fail to transfer from simulation to reality (the “sim-to-real” gap).
Reward Design
Specifying the right reward function is notoriously difficult. A vacuum-cleaning robot rewarded for picking up dust might learn to dump dust back on the floor so it can pick it up again. This is reward hacking, and it’s a pervasive challenge.
Safety and Alignment
When RL agents are deployed in open-ended environments, they can discover unintended and potentially harmful strategies. Specifying exactly what we want — and what we don’t want — remains an open research problem.
Interpretability
Neural network-based RL agents are often black boxes. Understanding why an agent made a particular decision is difficult, which limits trust and adoption in safety-critical domains.
Getting Started with Reinforcement Learning
Prerequisites
- Basic Python programming
- Understanding of calculus (gradients) and probability
- Familiarity with NumPy and PyTorch or TensorFlow
Recommended Learning Path
- Start with the concepts: Read Sutton & Barto’s Reinforcement Learning: An Introduction (free online) — the canonical textbook
- Implement tabular methods from scratch: Q-learning and SARSA on simple environments like FrozenLake
- Learn OpenAI Gym / Gymnasium: The standard toolkit for RL environments
- Implement DQN: Train an agent to play Atari games
- Implement PPO: The workhorse algorithm for most modern RL applications
- Explore advanced topics: Model-based RL, multi-agent RL, offline RL, inverse RL
Key Libraries and Frameworks
| Library | Purpose |
|---|---|
| Gymnasium | Standard RL environment API |
| Stable-Baselines3 | Reliable implementations of major RL algorithms |
| RLlib | Distributed RL at scale (part of Ray) |
| TRL | Transformer Reinforcement Learning — RLHF for LLMs |
| PettingZoo | Multi-agent RL environments |
| Spinning Up | Educational resource by OpenAI |
A Quick Example: Q-Learning in ~30 Lines
import numpy as np
import gymnasium as gym
env = gym.make("FrozenLake-v1", is_slippery=False)
q_table = np.zeros((env.observation_space.n, env.action_space.n))
alpha, gamma, epsilon, episodes = 0.1, 0.99, 0.1, 10000
for episode in range(episodes):
state, _ = env.reset()
done = False
while not done:
if np.random.random() < epsilon:
action = env.action_space.sample()
else:
action = np.argmax(q_table[state])
next_state, reward, terminated, truncated, _ = env.step(action)
done = terminated or truncated
q_table[state, action] += alpha * (
reward + gamma * np.max(q_table[next_state]) - q_table[state, action]
)
state = next_state
print(f"Training complete. Q-table shape: {q_table.shape}")
Why Reinforcement Learning Matters in 2026
RL is no longer just an academic curiosity. It is the backbone of three of the most important AI trends today:
- AI Agents: Autonomous systems that plan, use tools, and self-correct depend on RL to learn effective multi-step strategies
- AI Alignment: RLHF and its successors are the only proven techniques for making powerful AI systems behave according to human values
- Embodied AI: Robots, self-driving cars, and drones all rely on RL for physical-world decision-making
As models become more capable and the cost of running them drops, RL is expanding beyond research labs into production systems across every industry.
Learn More
- Read our Reinforcement Learning glossary entry for a quick definition
- Understand RLHF — the technique that made ChatGPT possible
- Explore AI Agents, which build on RL for autonomous decision-making
- Check out Machine Learning Basics for the broader ML landscape
- Find RL-powered tools in our AI Tools directory
Published:
Get smarter about AI
The sharpest AI news, curated daily. Delivered free to your inbox.