Abstract
Large Language Models (LLMs) frequently struggle with precise length constraints due to subword tokenization and autoregressive decoding. This paper presents a training-free, iterative sampling framework based on the Metropolis-Hastings algorithm. By treating length control as a search and refinement problem rather than a learning task, the proposed method enables black-box models to meet exact word counts or interval constraints with high success rates while maintaining linguistic quality.
Introduction
Despite their reasoning prowess, LLMs struggle with precise length control—a fundamental requirement for tasks like news summarization, fitting text into UI elements, or avoiding "length bias" in model evaluations. The root of the problem lies in subword tokenization, where words are split into chunks the model cannot easily count, and autoregressive decoding, where the model decides the next token without foresight of the final sequence length. Existing solutions typically involve fine-tuning, which is computationally expensive and impossible for proprietary "black-box" models like GPT-4.
Methodology: Iterative Metropolis-Hastings Sampling
The authors propose an iterative framework based on the Metropolis-Hastings (MH) algorithm, a classic Markov Chain Monte Carlo (MCMC) method. The framework balances two goals: high linguistic quality (the LLM's natural probability) and adherence to the length constraint (a scoring function).
Instead of one-shot generation, the framework starts with an initial draft and proposes new versions in each iteration. To handle black-box models where internal probabilities are unavailable, the authors use an "LLM-as-a-Judge" approach, prompting the model to perform pairwise comparisons between drafts. To accelerate convergence, an Importance Sampling (IS) strategy is employed, where the model receives specific feedback (e.g., "delete 5 words") to nudge the proposal distribution toward the target length.
Experimental Setup
The method was evaluated on three primary benchmarks:
- CNN/DailyMail (CNNDM): Focused on exact length control (e.g., "exactly 45 words").
- Alpaca-Eval-LI: Modified for length interval control (e.g., "under 50 words").
- MT-Bench-LI: A multi-turn benchmark adapted for length interval constraints.
Results and Discussion
The framework achieved near-perfect results across various state-of-the-art models. On the CNNDM dataset, Llama-3.1 achieved a 100% success rate in hitting exact word counts, a significant jump from the 7.7% achieved via standard prompting. GPT-4 reached a 99.2% success rate.
A key insight is that LLMs are significantly better at editing a draft to a specific length than generating it from scratch. By integrating explicit feedback through Importance Sampling, the system reached target lengths in an average of just 2.4 iterations for exact tasks. Crucially, metrics like ROUGE and BERTScore remained stable, indicating that linguistic quality was preserved during the refinement process.
Conclusion and Future Work
This research demonstrates that length control can be effectively treated as a search problem, preserving the versatility of black-box models without the need for fine-tuning. While highly effective, the iterative nature introduces inference overhead. Future work may explore parallel sampling or smaller "editor" models to reduce latency and improve scalability for real-time applications.
Summary updated