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.

A five by four grid with an agent at the bottom left, a goal worth plus one at the top right, a hazard worth minus one, two walls and an arrow showing one path

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.

The agent sends an action to the environment, and the environment sends back a new state and a reward

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.

A three by three grid with the agent in the middle and arrows for up, down, left and right, next to short definitions of state and action

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.

A bar chart of the chance of each move at one state: up 10 percent, down 5 percent, left 15 percent and right 70 percent

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.

Two runs drawn as chains of circles: run A ends at the goal with a return of plus one, and run B ends at the hazard with a return of minus one

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.

An agent choosing between a known route that pays a little and an untried route that might pay more or less, with a bar showing mostly exploit and a little explore

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.

Before training all four moves have a 25 percent chance, and after many scored runs the move right has 76 percent

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: Grid World terms mapped to a language model: agent is the model, environment is the user, tools and files, state is the text so far, action is the next token, policy is the weights, reward is a score for the answer, trajectory is the whole sequence

Claude Code Example

A loop between the LLM as policy and the machine as environment, with the LLM context as the observation, the machine as the state, and a reward of 0 or 1 for the whole run

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.

Two runs of code tokens: in the passing run every token carries a reward of 1, in the failing run every token carries a reward of 0

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.

SFT flows from a person-written answer to raising the odds of its tokens, while RL flows from the model's own samples through a score to raising the odds of the tokens that scored well

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.

Six answers to one prompt: the two that pass get a positive advantage and the four that fail get a negative advantage

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.

Three code-writing steps feed a test run with two passes and one fail, and dashed arrows of different thickness carry credit back to each step

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.

Five panels going from noise to a clean image through denoising steps, with a single reward model score applied back to every step

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