Length Controlled Generation for Black-box LLMs

Yuxuan Gu, Wenjie Wang, Xiaocheng Feng, Weihong Zhong, Kun Zhu, Lei Huang, Ting Liu, Bing Qin, Tat-Seng Chua

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).

The Metropolis-Hastings framework for length-controlled generation, illustrating the proposal and importance distributions used to refine text iteratively.

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.

Main experimental results across multiple LLMs, showing significant improvements in accuracy (ACC) and length error metrics (L1, L2) while maintaining text quality (ROUGE, BERTScore).

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

BibTeX
@inproceedings{gu2025length,
  author = {Yuxuan Gu and Wenjie Wang and Xiaocheng Feng and Weihong Zhong and Kun Zhu and Lei Huang and Ting Liu and Bing Qin and Tat-Seng Chua},
  title = {{Length Controlled Generation for Black-box LLMs}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
}
Figure