Ground Truth.
AI, checked against the source.

Learn · Intermediate

Constrained Decoding: Forcing an AI to Stay Inside the Lines

Constrained decoding forces a language model's output to follow a fixed structure by blocking, at each step, any next word that would break the rules. It is the machinery behind features like "guaranteed valid JSON" and strict tool-call formats: rather than hoping the model formats its answer correctly, you make incorrectly formatted output impossible. As AI systems increasingly call tools and hand data to other programs, this shift from hoping to guaranteeing has become one of the most practical reliability techniques in the field.

Key facts

To see why this is needed, recall how a language model actually writes. At every step it produces a probability for every possible next token, and then one is chosen (see how AI picks its next word). Left to its own devices, the model might pick a token that starts a perfectly fluent sentence but breaks the JSON you asked for: a missing quote, an extra field, a trailing comma. When that output is being fed straight into another program, one stray character can crash the whole pipeline.

Constrained decoding fixes this at the source. Alongside the model sits a set of rules, often written as a grammar (a formal description of what strings are allowed, like the rules that define valid JSON). At each step, the software looks at what has been generated so far, works out which next tokens could still lead to a valid result, and sets the probability of every other token to zero. The model then chooses only among the legal options. A useful analogy is a GPS that will not let you turn onto a one-way street the wrong direction: you are still driving, but the illegal moves are simply not available. Geng and colleagues (2023) showed this grammar-based approach can produce well-structured output even without any extra fine-tuning.

This is very different from just asking for a format in the prompt. A prompt instruction is a suggestion the model may follow, ignore, or bungle, especially deep into a long conversation. Constrained decoding is a wall, not a request. That distinction became a live issue in July 2026 when Flask's creator reported that newer Anthropic models were inventing extra, invalid fields in their tool calls and that turning on strict, schema-constrained sampling made the problem vanish entirely. That is constrained decoding doing exactly its job: the model wanted to add a made-up field, and the constraint made that token impossible to emit.

Why does this matter? Because reliable structure is what lets AI plug into real software. Tool use, function calling, database queries, form filling, and agent workflows all depend on the model emitting something a machine can parse without a human checking it. Constrained decoding is also a partial defense against a category of hallucination: the model cannot invent a field name or a category label that is not in the allowed set. It does not make the content true, but it does make the shape trustworthy.

The honest caveats are real. First, constraints guarantee form, not correctness: a model can emit perfectly valid JSON that says something false. Second, there is evidence that forcing output into a rigid mold can make a model reason less well, because it can no longer think out loud in its own words before committing to structure. A common compromise is to let the model reason freely first and only constrain the final structured answer. Third, writing a correct grammar for a deeply nested format is genuinely hard, which is why some providers cap how complex a strict schema is allowed to be, a limit that, as the Anthropic tool-call story showed, can block the technique for exactly the complicated tools that need it most. Used with those trade-offs in mind, constrained decoding remains one of the cleanest ways to make a probabilistic model behave predictably where it meets the rest of your code.

Key papers
Efficient Guided Generation for Large Language Models (Willard and Louf, 2023)
Grammar-Constrained Decoding for Structured NLP Tasks without Finetuning (Geng et al., 2023)

Key questions

What is constrained decoding?

Constrained decoding is a technique that forces a model's output to match a required format by filtering the model's word choices at each step so it can only pick words that keep the output valid. It is how features like guaranteed-valid JSON output work.

How is constrained decoding different from just asking for JSON in the prompt?

Asking in the prompt is a request the model can ignore or get wrong, while constrained decoding is a hard guarantee enforced by the software around the model. The prompt persuades; constrained decoding makes invalid output literally impossible.

What is the downside of constrained decoding?

Forcing the output into a rigid structure can sometimes make the model reason worse, because it can no longer phrase things freely, and building a correct rule set for complex nested formats is hard. Some providers cap how complex a constrained schema can be for this reason.
Cite this

APA

Ground Truth. (2026, July 4). Constrained Decoding: Forcing an AI to Stay Inside the Lines. Ground Truth. https://groundtruth.day/learn/constrained-decoding.html

BibTeX

@misc{groundtruth:constrained-decoding,
  title  = {Constrained Decoding: Forcing an AI to Stay Inside the Lines},
  author = {{Ground Truth}},
  year   = {2026},
  month  = {jul},
  url    = {https://groundtruth.day/learn/constrained-decoding.html}
}

Topics: constrained decoding · structured output · JSON mode · tool use · grammars