Book a free call

What Is a Decision Transformer and How Does It Work?

A decision transformer treats reinforcement learning as sequence prediction: it reads past states and a target return, then predicts the next action.

Hertiv engineering teamReviewed by a senior engineer
7 min read
What Is a Decision Transformer and How Does It Work?
On this page

Short answer

A decision transformer is a model that treats reinforcement learning as a sequence prediction problem. Instead of learning a value function, it reads a history of past states, actions and rewards, is told the total return it should aim for, and predicts the next action that would lead to that return. It is trained with supervised learning on logged trajectories, so it needs no live interaction during training.

What is a decision transformer?

A decision transformer is a model that treats reinforcement learning as a sequence prediction problem. It takes a trajectory, which is a recorded sequence of states, actions and rewards from a past run, and learns to predict the next action from what came before, given a target return to aim for.

The idea comes from a 2021 paper by researchers at UC Berkeley, Google and Brown, who showed that a transformer of the kind used for language can learn control policies from offline data. The model reads three kinds of tokens in order: the return to go, which is the total reward still to be collected from that point on, the state the agent is in, and the action it took.

At run time you give the model a desired return, such as the score of a good past run, and it generates actions one step at a time, updating the return to go as rewards come in. The model never estimates how good a state is on its own; it only predicts what action a run that reaches the target return would take next.

If you want the background, reinforcement learning explains the problem it is solving.

How is it different from other reinforcement learning?

A decision transformer differs from standard reinforcement learning because it removes the value function and the trial-and-error loop. Most reinforcement learning methods learn by estimating future rewards and updating those estimates as the agent acts, which needs many live interactions and can be unstable.

The table below shows the difference:

Method What it learns Training data Live interaction
Q-learning Value of each action in each state Live or logged experience Usually needed
Policy gradient A policy, updated by reward signals Live rollouts Needed
Decision transformer Next action from past sequence Logged trajectories only Not needed for training
Behaviour cloning Next action from current state Logged trajectories only Not needed for training

Behaviour cloning is the closest cousin: it also copies logged actions. The difference is that a decision transformer is conditioned on the return to go, so the same model can aim for a good run or a mediocre one, and it sees the full history instead of only the current state. That history lets it learn from long sequences where the payoff comes many steps after the key decision.

How does a decision transformer read a trajectory?

A decision transformer reads a trajectory as a sequence of tokens, three for each step: the return to go, the state, and the action. A run of 40 steps becomes 120 tokens, and the transformer predicts the action token at each step from all the tokens before it.

The return to go is the key idea. At the start of a run it equals the total reward the run will collect. After each step, you subtract the reward just received, so the number always says how much reward is still to come.

A run that ends well starts with a high return to go, and a run that ends badly starts with a low one, so the model learns which actions belong to which kind of run.

Because the model is a transformer, it can look back across the whole sequence when choosing the next action. That matters in tasks where an early decision shapes the outcome much later, such as a game where the opening moves decide the ending. The model is built on the same attention mechanism as language models, which is why deep learning is the background you need to follow the design.

How do you train a decision transformer?

You train a decision transformer with supervised learning on logged trajectories, which makes the training loop far simpler than a reinforcement learning loop. There is no environment to run, no reward signal to shape during training, and no value estimate to keep stable.

  1. Collect trajectories. Record states, actions and rewards from real runs, from human operators, from an existing controller, or from a mix of both.
  2. Compute the return to go. For each step, add up the rewards from that step to the end of the run.
  3. Feed the sequence to the transformer. Give it the return to go, state and action tokens in order, and ask it to predict each action from the tokens before it.
  4. Train with a standard loss. Use cross-entropy for discrete actions and mean squared error for continuous ones, the same losses used for ordinary supervised learning.
  5. Test on held-out runs. Check the model on trajectories it has not seen, and measure the return it actually achieves when you let it act.
  6. Set the target return at run time. Give the model the return of a good past run, then let it generate actions step by step.

The training data decides the ceiling. A decision transformer can only aim for returns it has seen in the logs, so a dataset of poor runs produces a model that copies poor runs. If you have no good runs to learn from, fix the data first.

Where does it work well, and where does it struggle?

A decision transformer works well where you already have logged trajectories and the task rewards long-term planning. Offline control problems, game playing from recorded games, and robot tasks with recorded demonstrations are the natural fit, because the data already exists and live trial and error would be slow or costly.

It also works well where you want one model to aim for different outcomes. Because the target return is an input, the same trained model can be asked to play safely or aggressively, simply by changing the number you give it at the start. That is useful when the goal changes between runs and retraining would take too long.

It struggles where the logged data is narrow or the environment changes. If every logged run follows the same strategy, the model never sees a better one, so it cannot invent one. If the environment shifts after training, the old trajectories no longer describe the world, and the model's predictions go stale. In those cases a method that keeps learning from live interaction fits better.

Example: a warehouse sorting arm

Example (hypothetical): a warehouse runs a sorting arm that picks parcels from a belt and drops them into bins. The team has 5,000 recorded runs, each 40 steps long, which is 200,000 steps of state, action and reward data. The average run sorts 30 parcels without a mistake, and the best runs sort 38.

The team trains a decision transformer on those runs. At run time they set the target return to 38, the score of a good past run. The model then predicts each pick from the history so far and the return still to come. When it makes a mistake and the return to go drops, the model adjusts its later picks to the new target, just as a good run that had a bad start would.

The result is not magic: the model can only copy the best behaviour already in the logs, and it sorts 36 parcels on a typical run, better than the average of 30 but short of the target of 38.

The team then records the model's own runs, adds the best ones to the training data, and retrains. Each round of new data lifts the ceiling a little, which is the practical way to improve a decision transformer.

What are the common mistakes?

The most common mistakes with decision transformers come from treating them as ordinary reinforcement learning, or from trusting the target return more than the data.

  • Setting a target return the data never reached. Asking for a return above the best logged run makes the model guess, not perform.
  • Training on narrow data. Logs from one strategy teach one strategy; the model cannot discover a better one on its own.
  • Ignoring the reward design. A badly shaped reward teaches the model to chase the number, not the real goal.
  • Skipping held-out testing. A model that fits the training runs can still fail on new ones, so test before deploying.
  • Using it where live learning is cheap. If you can run the environment safely and often, a method that keeps learning will usually do better.

If you are choosing between this and a simpler approach, machine learning covers the wider set of methods, and a small supervised model is often enough when the task has a clear input and output.

Key takeaways

  • A decision transformer predicts the next action from past states and a target return
  • It trains by supervised learning on logged trajectories, with no live interaction
  • The return to go tells the model which kind of run to copy
  • It fits tasks with existing logs and long-term payoffs, and fails on narrow data
  • Set the target return to a value the logs actually reached, or the model guesses

Frequently asked questions

What is the return to go in a decision transformer?

The return to go is the total reward still to be collected from the current step to the end of the run. It starts as the run's total reward and falls by the reward received at each step. The model reads it as an input, so it learns which actions belong to runs that end well and which belong to runs that end badly.

Do I need live interaction to train a decision transformer?

No. Training uses only logged trajectories of states, actions and rewards, which is the main advantage over most reinforcement learning methods. You need live interaction only when you test the trained model, to measure the return it actually achieves on new runs.

How is a decision transformer different from behaviour cloning?

Behaviour cloning copies the action for the current state, with no idea of the outcome. A decision transformer is conditioned on the return to go and sees the full history, so the same model can aim for a good run or a poor one, and it can learn from sequences where the payoff comes many steps after the key decision.

What data do I need to train one?

You need recorded trajectories: states, actions and rewards from real runs, human operators or an existing controller. The data should cover a range of outcomes, including some good runs, because the model can only aim for returns it has seen. Narrow data from one strategy teaches only that strategy.

Can a decision transformer invent a better strategy than the logs show?

No. It copies behaviour from the logs, conditioned on the return to go, so its ceiling is the best behaviour already in the data. To beat the logs you need either new data from a better strategy or a method that keeps learning from live interaction.

When should I use a decision transformer instead of Q-learning?

Use a decision transformer when you have good logged trajectories, live interaction is slow or costly, and the task rewards long-term planning. Use Q-learning or another live method when you can run the environment safely and often, or when the logged data is narrow and you need the agent to discover better behaviour.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: AI and agent systems in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Thinking about sequence models for control or planning? Talk to Hertiv.

A senior engineer replies within one business day. Or see our Machine Learning Development service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to [email protected].

Prefer email? Write to [email protected]
Estimate costBook a call