CRANE: Reasoning with constrained LLM generation

Debangshu Banerjee, Tarun Suresh, Shubham Ugare, Sasa Misailovic, Gagandeep Singh · DOI

Introduction

Large Language Models (LLMs) are increasingly integrated into complex software ecosystems where they must interact with external tools like Python interpreters, SQL databases, or formal logic solvers. These interactions demand perfect adherence to specific formal grammars; even a single syntax error can render an output useless. To address this, researchers often employ constrained decoding, a technique that forces the model to generate tokens within a predefined grammar.

However, a significant trade-off has emerged: while constrained decoding ensures syntactic validity, it frequently degrades the model's "functional correctness"—its ability to reason through a problem and arrive at the correct answer. This paper investigates this phenomenon and introduces CRANE (Constrained Reasoning Augmented geNEration), a novel decoding strategy designed to provide both flawless syntax and high-level reasoning capabilities.

Comparison of different decoding strategies on a symbolic math problem. CRANE achieves both syntactic and functional correctness where others fail.

The Reasoning-Constraint Paradox

The authors provide a theoretical foundation for why strict constraints hinder performance. Using circuit complexity theory, they prove that constant-layer LLMs restricted to a tight grammar (such as outputting only a final answer) are mathematically limited to the complexity class $TC^0$. By stripping away the model's ability to generate intermediate "scratchpad" or Chain-of-Thought (CoT) tokens, constrained decoding effectively reduces the computational depth available to the model. In essence, forcing an LLM to output only a valid formula is akin to asking a human to solve a multi-step calculus problem without using scratch paper.

The CRANE Algorithm

To resolve this paradox, the authors propose CRANE. The core insight is that constraints should only be applied when the model is generating the final, structured answer, leaving it free to "think" in an unconstrained manner beforehand.

CRANE implements a dynamic switching mechanism using start and end delimiters (e.g., << and >>):

  1. Unconstrained Phase: The LLM begins by generating free-form Chain-of-Thought reasoning. During this phase, no logit masking is applied, allowing the model to utilize its full computational expressivity.
  2. Constrained Phase: When the model generates a start delimiter, CRANE switches to a constrained mode. It utilizes an underlying constrained decoder (such as IterGen or Syncode) to mask invalid tokens, ensuring the subsequent output strictly follows the required formal grammar.
  3. Reversion: Upon reaching an end delimiter, the system can switch back to unconstrained generation if necessary.
The CRANE algorithm dynamically identifies a "constrained window" within the generation stream to apply grammar-based logit masking.

This approach effectively augments the grammar to include a reasoning prefix ($G_{augmented} \to Reasoning + Final\_Answer$), restoring the model's reasoning power without sacrificing the final output's integrity.

Experimental Results

The authors evaluated CRANE against standard unconstrained CoT and purely constrained generation using several open-source models, including Qwen2.5, Llama-3.1, and DeepSeek-R1-Distill.

Symbolic Reasoning (GSM-Symbolic)

On the GSM-Symbolic benchmark, which requires models to output symbolic mathematical expressions, CRANE demonstrated significant gains. Using Qwen2.5-Math-7B-Instruct, CRANE achieved 38% accuracy, a substantial improvement over the 29% achieved by both standard constrained decoding and unconstrained CoT.

Logical Reasoning (FOLIO)

In the FOLIO task, where natural language premises are translated into First-Order Logic (FOL) formulas, CRANE again outperformed its peers. With Llama-3.1-8B-Instruct, CRANE reached 46.3% accuracy, compared to 39.4% for purely constrained generation and 32.0% for unconstrained CoT.

Syntactic Reliability

Crucially, CRANE maintained the syntax guarantees of constrained decoding. While unconstrained CoT often suffered from parse errors (up to 10% in some models), CRANE achieved near 100% parse rates for the final answer blocks, ensuring compatibility with downstream solvers like Z3 or Prover9.

Conclusion

CRANE offers a practical and theoretically grounded solution to the tension between reasoning and constraints in LLM generation. By dynamically toggling constraints, it allows models to leverage Chain-of-Thought reasoning while guaranteeing syntactically valid outputs for software integration. While currently limited to models where logit access is available, CRANE represents a significant step toward more reliable and capable AI systems.

Summary updated

BibTeX
@inproceedings{banerjee2025crane,
  author = {Debangshu Banerjee and Tarun Suresh and Shubham Ugare and Sasa Misailovic and Gagandeep Singh},
  title = {{CRANE: Reasoning with constrained LLM generation}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=wKs9fHYxCV},
}
Figure