Reinforcement Learning For Language Models
Reinforcement learning is a way to train a system by a score, rather than ground truth. This allows us to align the model to existing inferences rather than an outcome. Let's look at a tiny game called Grid World as an example of RL.
For this explanation you do not need a math background. You only need to know what a program is and what a model's weights are. Every new term is explained once and then used the same way for the rest of the post.
Grid World, the smallest game
Imagine a small board made of squares. A piece stands in one square. On each turn it can move up, down, left or right. In one corner there is a goal. Next to it there is a hazard. A few squares are walls that block the way.
Nobody tells the piece where to go. It has to find out by randomly picking the next tile. Reaching the goal is good. Touching the hazard is bad.
Agent
The agent is the player in the gridworld who acts and learns. In Grid World it is the piece on the board. The player/agent looks at the grid, randomly picks a move then does it again.
The agent is not the whole system. It is only the decision maker. Everything it can not directly control belongs to something else, which we call the environment.
Environment
The environment is everything the agent interacts with. In Grid World it is the board: the squares, the walls, the goal, the hazard and the rules for what a move does. If the agent walks into a wall, the environment decides it stays where it is.
The two talk in a loop. The agent sends an action. The environment sends back what changed, plus a score. Then the next turn begins. This loop is the core picture of RL, and the loop in the figure below is the one we will relabel for language models later.
State
The state is a description of where things stand right now. In Grid World it is the agent's position, for example row 2, column 2. If the state had enough detail, you could predict what each move would do next.
Sometimes the agent does not get the full state. It gets only a part of it, which is called an observation. Think of a fog that hides distant squares. The agent then has to decide using only what it can see. Keep this word in mind, because language models always work from an observation.
Action
An action is a choice the agent makes at one moment. In Grid World there are four: up, down, left and right. The set of all allowed actions is called the action space. Here it is small and discrete, which means you can count the options.
An action changes the state. Move right, and the agent is in a new square. Some actions do nothing useful, like pushing into a wall. The agent still has to choose, and it learns from the result.
Policy
The policy is the agent's rule for choosing an action. It takes the state and gives back a choice. A policy can be strict, always picking the same move in the same square. Or it can be a set of chances, such as 70 percent right, 15 percent left, 10 percent up and 5 percent down.
Learning means changing the policy. At first the chances are close to even, so the agent wanders. After training, the chances lean toward the moves that tend to lead somewhere good. In a neural network, the policy is stored in the weights, the numbers the model learns. Changing the policy means changing the weights.
Reward
A reward is a number the environment hands back after an action. In Grid World the goal gives +1 and the hazard gives -1. Most squares give 0. The reward is the only feedback the agent gets. It never sees a label that says "the correct move here was right".
This is what makes RL different from copying examples. The designer decides what counts as good and writes it down as a number. The agent has to work out, by itself, which of its moves earned that number. Choosing the reward well is hard, because the agent will chase exactly what you score, not what you meant.
Return and trajectory
A trajectory is the full record of one run: every state, every action, every reward, from the start to the end. It is also called an episode or a rollout. The return is the total reward collected along the way, often with later rewards counted a little less.
The return matters because a single move is rarely the whole story. The move that ends at the goal got the +1, but the earlier moves made that possible. RL has to share credit for the final result across many earlier choices. This is known as the credit assignment problem, and it comes back later in this post.
Entropy is Exploration
When performing on the grid, all agents are tasked to randomly pick the next tile. This is called entropy or chaos/randomness. This exploration creates the alignment data necessary to update the weights, policy or probability of a future action of the agent.
This simple exploration/entropy is the cornerstone of Reinforcement Learning in that it is the data generation mechanism for the agent to update based on the score of the exploration itself.
How the agent actually learns
The agent plays many runs and gets a reward. The agent then backpropagates its policy so that the moves from high-return runs become a bit more likely, and the moves from low-return runs become a bit less likely. Then it plays again with the new policy.
This family of methods is called policy gradient. A gradient is the direction in which a number changes fastest, and training follows it a small step at a time. The classic version is called REINFORCE, from Williams in 1992. It says to push up the probability of an action in proportion to how good the run turned out.
Notice what is missing. Nobody gave the agent a list of correct moves. It only needed a way to score what happened. Hold on to that, because it is the whole reason RL is useful for language models.
RL for language models
A language model is a program that reads text and predicts the next small piece of text, called a token. Every inference the model produces a probability for each possible next token, then one token is picked.
This is the exact same thing as grid world except the action output is not a tile coordinate but a token. The model is no longer the probability of choosing a tile but the probability of choosing a next token.
Here they are written out:
Claude Code Example
- Agent: the language model, producing text.
- Environment: The computer.
- State: That specific instance of the computer.
- Observation: The context window, what the LLM can see
- Action: The next token.
- Policy: the model's weights, which turn the context into chances for each token.
- Reward: a score for the answer, given by a labeller.
- Trajectory and return: the whole generated sequence and its total score.
The policy is the language model
In this view the language model is the policy. Give it a context and it gives back a distribution over the next token. Sample from it, append the token, and repeat. When the model writes a whole answer, it has taken one action per token.
A trajectory, then, is the whole answer: every token, and for an agent, every tool call too. At the end the environment hands over a reward. That reward is the training signal, and the weights are what get updated.
The action is one token
The basic idea is simple. If a token led to a good result, make that token more likely in that context. The weights are changed with backpropagation, which is the same step that pretraining and supervised fine-tuning, or SFT, use. The step is small, because the learning rate is set low.
That is why the token is called the action. Each token is one choice among thousands, and each choice can be pushed up or down. But here is the part that is easy to get wrong. The reward does not usually arrive per token. In the common setup, one reward arrives for the whole trajectory, and every token in that trajectory is treated as if it earned it. A pass lifts all of its tokens. A fail pushes them all down.
One reward, many tokens
Finer credit is possible. You can score each step of a long task, or train a model that estimates how good each token was. It needs extra machinery, and it is harder to get right. In many language model setups, the simple version is the starting point: score the run, apply that score to every token in it.
This is a simplification worth being clear about. "Per token" describes what is being adjusted, the probability of each token chosen. "Per trajectory" describes where the score comes from. Both are true at once.
How this differs from SFT
SFT shows the model a correct answer written by a person and raises the probability of those tokens. It needs the ground truth text. RL does not. The model writes its own answers, a scorer grades them, and the tokens from good answers are made more likely. The "labels" are the model's own samples, chosen by score.
So RL needs only a way to tell good from bad, not a stored right answer. That is a big difference. It is easy to check whether code passes its tests or whether a math answer matches, even when writing the full solution is hard. A close cousin of this idea is rejection sampling: generate many answers, keep the good ones, and fine-tune on them.
What a zero can and cannot teach
The simplest setup gives a 1 for a good result and a 0 for a bad one. Taken literally, a 0 does not teach much. Multiply the push by zero and nothing moves. The model learns from the wins, but the losses are silent.
Practical methods fix this. They subtract a baseline, an estimate of the typical score, so that the push is the reward minus the baseline. This difference is called the advantage. A result better than usual is pushed up. A result worse than usual is pushed down.
One popular method, GRPO, builds the baseline from a group. It samples several answers to the same prompt and compares each score to the group average. In the figure, two of six answers pass and the average is 0.33, so passes get +0.67 and fails get -0.33. The full method also divides by how spread out the group's scores are, which this sketch skips. This method comes from the DeepSeekMath paper and does not need a second large model to estimate value.
Small steps and a short leash
Why is the learning rate so low? The data is noisy. One lucky answer can look good for the wrong reasons. A big step on noisy data can wreck what the model already knows. And because the model produces its own training data, a large change in the policy changes what data it sees next.
So methods add guards. PPO clips each update so that a token's probability can not move too far in one step. GRPO adds a penalty for drifting away from a reference copy of the model, using a measure called KL divergence. The result is steady, careful movement, not a leap.
Passing credit to the steps of a program
For code, the reward has a nice property. It can be checked by a machine. Unit tests pass or fail, and that gives a verifiable reward without a human in the loop. The CodeRL paper used test outcomes this way, and DeepSeek-R1 used compiler checks against predefined test cases as one of its rule-based rewards.
The simplest option gives the whole program one score. But a program is built from decisions: which approach to take, how to write a function, whether to handle an edge case. These decision points are the pivots where the model chose one path over another. A pass or a fail says little about which pivot mattered.
Spreading one result over hundreds of tokens is noisy. It lifts the good tokens and the useless ones alike. Several tools help. Partial results, such as the share of tests that pass, give a finer score than pass or fail. Checks after each step can reward the pivots directly. Advantage estimates compare each choice with what was typical. CodeRL, for example, trained a critic to predict whether a program would be correct and used it to give the generating model denser feedback.
RL Is Multimodal
The backpropagation of RL rewards only respects the score. All modalities can be rewarded or punished to update the weights. The update only needs two things: how likely the policy was to take the actions it took, and one number that scores the outcome. It does not matter whether the outcome is a code file or a picture. This is what is meant by saying the reward is agnostic to the modality.
Take image generation with diffusion models. A diffusion model starts from random noise and removes a little of it at each step until an image appears. The paper "Training Diffusion Models with Reinforcement Learning" treats that denoising process as a multi-step decision problem. Each denoising step is an action, and the finished image is judged at the end. The authors call the method DDPO.
The reward is a score for the final image. The paper tries image compressibility, aesthetic quality, and how well the image matches its prompt, judged with feedback from a vision-language model. Then the policy gradient raises the probability of the denoising steps that led to high-scoring images.
There is one real difference. A language model picks a token from a fixed list. A denoising step produces a value in a continuous space, so it is not a pick from a fixed list of token chances. The plumbing around it is the same. One scalar score is spread back over the steps that produced the result.
Summary
Reinforcement learning is learning from a score without a ground truth, this is the power RL has over supervised fine-tuning. The agent acts, the environment answers, and the reward says how it went. The LLM weights are nudged toward the actions that led to positive reward.
For language models, the policy is the model, the action is a token, and the reward usually comes from a whole trajectory, updating the LLM token by token from that one reward. The update looks like SFT on the model's own samples, picked by score, with small steps and guards to keep it stable. Code tests give a clean score, credit assignment is the hard part, and the same reward idea carries over to any modality.
Sources
- Richard Sutton and Andrew Barto, "Reinforcement Learning: An Introduction," second edition: http://incompleteideas.net/book/the-book-2nd.html
- OpenAI Spinning Up, "Part 1: Key Concepts in RL": https://spinningup.openai.com/en/latest/spinningup/rl_intro.html
- Ronald J. Williams, "Simple statistical gradient-following algorithms for connectionist reinforcement learning," Machine Learning, 1992: https://link.springer.com/article/10.1007/BF00992696
- Ouyang et al., "Training language models to follow instructions with human feedback": https://arxiv.org/abs/2203.02155
- Schulman et al., "Proximal Policy Optimization Algorithms": https://arxiv.org/abs/1707.06347
- Shao et al., "DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models" (introduces GRPO): https://arxiv.org/abs/2402.03300
- DeepSeek-AI, "DeepSeek-R1: Incentivizing Reasoning Capability in LLMs via Reinforcement Learning": https://arxiv.org/abs/2501.12948
- Le et al., "CodeRL: Mastering Code Generation through Pretrained Models and Deep Reinforcement Learning": https://arxiv.org/abs/2207.01780
- Black et al., "Training Diffusion Models with Reinforcement Learning": https://arxiv.org/abs/2305.13301
- Our earlier post, "Become the Greatest Clean English Writer," on next-token prediction: 5.md