Ruler: A Model-Agnostic Method to Control Generated Length for Large Language Models

Jiaming Li, Lei Zhang, Yunshui Li, Ziqiang Liu, Yuelin Bai, Run Luo, Longze Chen, Min Yang

Abstract

The instruction-following ability of large language models enables humans to interact with AI agents in a natural way. However, when required to generate responses of a specific length, large language models often struggle to meet users’ needs due to their inherent difficulty in accurately perceiving numerical constraints. To explore the ability of large language models to control the length of generated responses, we propose the Target Length Generation Task (TLG) and design two metrics, Precise Match (PM) and Flexible Match (FM) to evaluate the model’s performance in adhering to specified response lengths. Furthermore, we introduce a novel, model-agnostic approach called Ruler, which employs Meta Length Tokens (MLTs) to enhance the instruction-following ability of large language models under length-constrained instructions. Specifically, Ruler equips LLMs with the ability to generate responses of a specified length based on length constraints within the instructions. Moreover, Ruler can automatically generate appropriate MLT when length constraints are not explicitly provided, demonstrating excellent versatility and generalization. Comprehensive experiments show the effectiveness of Ruler across different LLMs on Target Length Generation Task, e.g., at All Level 27.97 average gain on PM, 29.57 average gain on FM. In addition, we conduct extensive ablation experiments to further substantiate the efficacy and generalization of Ruler.

1. Introduction: The Challenge of Length Control

Large Language Models (LLMs) have revolutionized human-AI interaction through their sophisticated instruction-following capabilities. Despite these advances, LLMs consistently struggle with numerical constraints, particularly regarding the length of generated responses. A common user frustration involves requesting a specific word count (e.g., "summarize in 30 words") only to receive a significantly longer or shorter output.

An illustration of an LLM failing to follow a 30-word length constraint.

This "length control" problem arises from two fundamental issues. First, there is a tokenization mismatch: LLMs process text as sub-word tokens, which do not map linearly to human word counts. Second, standard training objectives, such as next-token prediction, do not explicitly penalize models for deviating from requested lengths; they prioritize the statistical likelihood of the next token within a local context.

2. The RULER Framework

To address these limitations, we introduce RULER, a model-agnostic framework designed to bridge the gap between internal token processing and external length constraints. The core innovation of RULER is the Meta Length Token (MLT).

MLTs are special tokens added to the model's vocabulary (e.g., [MLT:10], [MLT:500]) that represent specific word-count ranges. The framework operates in two phases:

  • Training Phase: The researchers curated a dataset ($D_{MLT}$) of over 120,000 entries derived from OpenHermes 2.5, LongForm, and ELI5. Each response is prefixed with an MLT corresponding to its actual word count. By fine-tuning the model to predict the MLT before the response, the model learns to "budget" its generation based on the preceding token.
  • Inference Phase: In the Target Length Generation (TLG) scenario, a user's length constraint is converted into the corresponding MLT and injected into the prompt. In non-constrained scenarios, the model is trained to autonomously generate an appropriate MLT first, which then guides the length of the subsequent response, ensuring internal consistency.

3. Experimental Setup and Evaluation

We evaluated RULER using a newly developed benchmark for the TLG task, consisting of 2,000 queries across nine distinct target lengths, ranging from 10 to over 800 words. These are categorized into three levels of difficulty.

Distribution of samples across different target length levels in the TLG benchmark.

To measure success, we defined two primary metrics:

  • Precise Match (PM): Evaluates strict adherence to the target length within a very narrow margin.
  • Flexible Match (FM): Evaluates adherence within a broader, level-dependent range, accounting for the natural variance in human language.

4. Results and Analysis

Comprehensive experiments across multiple open-source LLMs—including Llama-3, Mistral, Gemma, Yi, and Qwen—demonstrate the effectiveness of RULER. Across all tested models and levels, RULER achieved an average gain of 27.97 points in PM and 29.57 points in FM.

For instance, Llama-3-8B's "All Level" FM score improved from a baseline of 44.25 to 68.95 with RULER. Importantly, these gains in length control did not come at the cost of general performance. Evaluation on standard benchmarks like MMLU, GSM8K, and ARC showed that RULER-enhanced models maintained nearly identical reasoning and knowledge capabilities compared to their vanilla counterparts. While closed-source models like GPT-4-Turbo still lead in some categories, RULER-enhanced open-source models now significantly outperform baseline versions of much larger models in length adherence.

5. Conclusion and Future Work

RULER provides a robust, model-agnostic solution for controlling the generated length of LLMs. By introducing Meta Length Tokens, we enable models to perceive and adhere to numerical constraints more accurately. Future work will focus on addressing data imbalances for extremely long-form generation and refining the granularity of MLTs to further improve precision.

Summary updated

BibTeX
@inproceedings{li2024ruler,
  author = {Jiaming Li and Lei Zhang and Yunshui Li and Ziqiang Liu and Yuelin Bai and Run Luo and Longze Chen and Min Yang},
  title = {{Ruler: A Model-Agnostic Method to Control Generated Length for Large Language Models}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  url = {https://openreview.net/forum?id=BjP0XIhDxj},
}
Figure