Length Controlled Generation for Black-box LLMs

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

Abstract

Large Language Models (LLMs) often struggle with precise length constraints due to their subword tokenization and autoregressive nature. This paper introduces a training-free, iterative sampling framework that enables black-box LLMs to adhere to strict length requirements with near-perfect accuracy. By integrating the Metropolis-Hastings algorithm with an importance sampling acceleration strategy, the proposed method achieves significant improvements in length control without degrading generation quality or requiring access to internal model weights.

Introduction

Despite their impressive capabilities, LLMs are notoriously bad at "counting." If you ask an LLM to write a summary in exactly 46 words, it will likely give you 40 or 55. This happens because LLMs operate on subword tokens rather than words, and their autoregressive nature makes it difficult to plan the exact end-point of a sentence.

Existing solutions usually involve fine-tuning the model on length-constrained data. However, this is computationally expensive, can degrade the model's general reasoning abilities, and is impossible for proprietary "black-box" models accessed via APIs (like GPT-4 or Claude). There is a clear need for an inference-time method that can regulate length reliably across any model.

Methodology

The proposed framework is based on the Metropolis-Hastings (MH) algorithm, a classic Markov Chain Monte Carlo (MCMC) method. The process treats length control as a sampling problem rather than a training problem.

Overview of the iterative sampling framework using Metropolis-Hastings and Importance Sampling.

The framework operates through several key components:

  1. Target Distribution: A distribution is defined to balance language quality (LLM probability) and strict adherence to length constraints.
  2. Iterative Sampling: Starting from an initial response, the model iteratively proposes new versions of the text.
  3. Acceptance/Rejection: An acceptance probability determines whether to keep the new version, ensuring convergence toward the desired length while maintaining quality.
  4. Black-Box Adaptation: For models where internal probabilities (logits) are unavailable, an "LLM-as-a-Judge" approach uses self-scoring as a proxy for probability density.
  5. Importance Sampling (IS) Acceleration: To improve efficiency, specific feedback (e.g., "delete 5 words") nudges the proposal distribution closer to the target.

Experimental Evaluation

The framework was evaluated on several benchmarks, including CNN/DailyMail (CNNDM) for exact length control and Alpaca-Eval-LI for length-interval control.

Performance comparison of the proposed method (Ours) against standard instruction prompting (INST) across various LLMs.

The results demonstrate a massive leap over standard prompting (the "INST" baseline):

  • Near-Perfect Accuracy: On Llama-3.1, the framework achieved almost 100% success rates for exact length control on the CNNDM dataset within just 5 iterations.
  • Zero Quality Degradation: ROUGE scores remained virtually identical to the baseline, proving that forcing a specific length does not compromise logic or flow.
  • Superior Win Rates: On Alpaca-Eval-LI, the "win rate" for GPT-4 jumped from 30.2% to 92.0%, largely by eliminating length-related penalties.

Discussion and Conclusion

The core innovation of this work is treating length control as an inference-time optimization problem. By using self-correction via feedback, the LLM effectively performs surgical edits to meet constraints. While the iterative nature introduces some inference overhead, the gains in accuracy and the ability to apply the method to black-box models make it a powerful tool for precise text generation. Future work may focus on reducing the number of iterations required 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 Tat-Seng Chua and Bing Qin},
  title = {{Length Controlled Generation for Black-box LLMs}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
}
Figure