AI engineering paper · 2023

Guided generation

Willard and Louf · arXiv 2023

How to make a language model write only text that matches a pattern or grammar, by blocking bad options with a table built in advance.

Before the model picks, a mask (one 1 or 0 per token) blocks every token that would break the pattern. Here “A” is blocked and four of five tokens stay.

You’ll learn to

  • Explain how a mask of 1s and 0s makes a language model write only text that matches a pattern.
  • Trace how a pattern becomes a small machine and an index that lists the allowed tokens in each state.
  • Work out why the index turns a scan of 50,257 tokens into one lookup per step.
  • Name what later engines and vendor APIs added, and where forcing a format can hurt answers.

The idea in short

A language model writes text one small piece at a time. Each piece is called a token, and the model picks it from tens of thousands of options. Programs that read the output often need a strict shape, such as valid JSON or a date in one format. Instead of asking nicely and hoping, you can block every token that would break the shape before the model picks.

The slow part is deciding which tokens are allowed. The authors turn the pattern into a small machine that is always in one state. Before any text is written, they work out once a table, called the index, that lists the tokens each state allows. After that, every step is a lookup in the table instead of a scan of the whole list.

The problem

A program that calls a language model often needs a reply with a strict shape. A date must read 2026-11-06, not “the sixth of November”. A record must be JSON with the keys the next function expects. A snippet must be valid SQL. If the shape is wrong, the program stops or, worse, silently reads the wrong thing.

The usual first fix is to ask: “Reply in JSON.” That works most of the time, and “most of the time” is the problem. Over thousands of calls, some replies add a friendly sentence, drop a key or leave a bracket open. The next fix is to check the reply and ask again, which costs time and money on every failure.

Willard and Louf’s 2023 paper, “Efficient Guided Generation for Large Language Models”, goes after the cause of those failures: the model is free to write anything at every step. To see how they remove that freedom, you need to know how a model writes text.

A language model does not write a whole reply at once. It writes it in small pieces called : whole words, parts of words or single characters. The fixed list of every token the model knows is its . For GPT-2, the model used in the paper, the vocabulary holds 50,257 tokens. We call that size N.

At each step the model reads everything written so far and gives every token in the vocabulary a score, called a . Higher means more likely. The scores become probabilities, then one token is drawn at random in proportion to those probabilities. This draw is . The chosen token is added to the text, and the model repeats the process for the next token. It stops when it draws a special end-of-text token.

Nothing in that loop knows about your format. The model can draw a token that breaks it at any step. The fix the authors build on is blunt and reliable: before the draw, remove every token that would break the format. This is called , or guided generation. The output then matches the format every time, by construction.

The hard part is cost. Deciding which of 50,257 tokens are allowed, at every step, can cost more than the model itself. So the question the paper answers is: can the allowed tokens be found fast enough that guided generation is nearly free?

The design choices

The paper rests on three decisions.

Our biggest sale ever · Black Friday

Flat 50% Off

Pro

Continue reading

Unlock every course, now and later

  • System Design, live
  • AI Engineering, live
  • Data Engineering, live
  • Machine Learning Engineering, live
  • Inference Engineering, coming soon
  • AI Research, coming soon
  • Performance Engineering, coming soon
  • Agent & Harness Engineering, coming soon
  • LLM Fine-tuning, coming soon
  • LLM Pre-training & Post-training, coming soon
  • Reinforcement Learning, coming soon
  • AI Infrastructure & Platform Engineering, coming soon
  • AI Evaluation & Safety, coming soon
  • Forward Deployed Engineering, coming soon
  • Cloud Computing, coming soon
  • DevOps, coming soon

50% off · Annual and Lifetime

BLACKFRIDAY50Applied at checkout

Choose a plan

More AI engineering papers

All papers