NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Guiding LLMs The Right Way: Fast, Non-Invasive Constrained Generation

Luca Beurer-Kellner, Marc Fischer, Martin Vechev

Introduction

As Large Language Models (LLMs) are increasingly integrated into production pipelines, the demand for structured outputs—such as JSON, XML, or specific programming languages—has grown. To ensure these models adhere to strict formats, developers use "constrained decoding," a technique that masks out invalid tokens during the generation process. However, traditional constrained decoding methods suffer from two major flaws: significant computational overhead and "token misalignment."

Token misalignment occurs because LLMs process text as subword tokens rather than individual characters. When a constraint forces a model to pick a sub-optimal token sequence to satisfy a grammar, it can derail the model's internal logic, leading to higher uncertainty and a drop in reasoning accuracy. This paper introduces DOMINO, a novel algorithm designed to be "minimally invasive" and highly efficient, ensuring that LLMs follow complex formats without sacrificing speed or intelligence.

The DOMINO Architecture

DOMINO is built on the principle of being minimally invasive—meaning it produces the exact same token sequence as an unconstrained model whenever that sequence is valid under the grammar. Its architecture consists of several key innovations:

  • Scanner/Parser Separation: Borrowing from compiler design, DOMINO splits the task into a "scanner" for low-level regex (e.g., identifying a string or integer) and a "parser" for high-level structure (the Context-Free Grammar).
  • Vocabulary-Aligned Subterminal Trees: This is the core mechanism of DOMINO. The algorithm pre-computes how every token in an LLM's vocabulary fits into the grammar. By storing these in prefix trees, the engine can determine valid tokens at runtime with almost zero overhead.
  • Speculative Decoding: Structured formats like JSON are often highly predictable. DOMINO uses a simple, count-based model to "guess" the next few tokens. If these guesses are valid, the LLM verifies them in a single forward pass, which significantly boosts throughput.
  • Opportunistic Masking: To save time, DOMINO first checks if the LLM’s top choice is valid. If it is, the algorithm proceeds immediately without calculating a full mask.

Experimental Evaluation

The researchers evaluated DOMINO using benchmarks converted into structured JSON formats, including GSM8K (grade-school math word problems) and CoNLL-2003 (named-entity recognition). They also tested the system against complex grammars for the C programming language and various XML schemas.

Key Insights

  • The Bridge Token Discovery: The authors found that many tokens "bridge" multiple parts of a grammar (e.g., a single token representing a comma, a newline, and a space). If a decoder is not subword-aligned, it misses these efficient tokens, forcing the model into an "unnatural" state that degrades performance.
  • Constraints as an Accelerator: Counter-intuitively, constraints can make LLMs faster. Because the grammar limits the search space, speculative decoding can skip over predictable structural elements (like ": " in JSON) without requiring the full power of the LLM for every character.

Results

DOMINO demonstrates that constrained generation does not have to come at a cost to performance:

  • Accuracy: On the GSM8K benchmark with Mistral 7B, DOMINO achieved 41.8% accuracy, slightly outperforming unconstrained decoding (41.5%). In contrast, other constrained methods like Guidance dropped to 34.5% due to token misalignment.
  • Speedup: The efficiency gains were substantial. On the CoNLL2003 task with Llama-2 13B, DOMINO was 2.71x faster than unconstrained decoding.
  • Zero Overhead: Unlike previous state-of-the-art tools, DOMINO adds virtually no latency to the generation process thanks to its pre-computation strategy.

Limitations and Future Work

While DOMINO is highly efficient at runtime, the initial "compilation" of the grammar and vocabulary alignment can take between 1 and 20 seconds. This makes it less ideal for applications where the grammar changes with every request. Additionally, the algorithm relies on a lookahead parameter ($k$) to avoid tokenization "dead ends"; if $k$ is too low, sub-optimal token choices may still occur. Future work may involve using more sophisticated speculative models to drive even higher speedups for complex languages like C or Python.

Summary updated

BibTeX
@inproceedings{beurerkellner2024guiding,
  author = {Luca Beurer-Kellner and Marc Fischer and Martin Vechev},
  title = {{Guiding LLMs The Right Way: Fast, Non-Invasive Constrained Generation}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=pXaEYzrFae},
}
Figure