Hansel: Output Length Controlling Framework for Large Language Models

Seoha Song, Junhyun Lee, Hyeonmok Ko

Abstract

Large Language Models (LLMs) excel at generating fluent text but struggle with precise constraints, particularly output length. We introduce Hansel, a novel fine-tuning framework that equips LLMs with an internal "countdown timer" using periodic hidden special tokens. Unlike standard prompt-based methods, Hansel enables models to track their progress in real-time. Our evaluation across summarization and dialogue tasks shows that Hansel reduces length error by up to 75% and maintains high precision even when extrapolating to lengths beyond the training distribution, all while maintaining text quality across various architectures.

Introduction

Despite their remarkable capabilities, LLMs are notoriously poor at counting. A request for an "exactly 50-word summary" often results in outputs of varying lengths because standard architectures lack a mechanism to track sequence length during generation. Existing solutions typically rely on prompt-based control (often referred to as "Gretel"), which is unreliable, or architectural modifications that require expensive pre-training. Hansel addresses these gaps with a fine-tuning-only approach that is architecture-agnostic and highly precise.

Methodology: The Hansel Framework

The core innovation of Hansel is the injection of Hidden Arrangements of special tokens—the "pebbles" in the trail—during training.

Countdown Tokens and Periodic Injection

Hansel introduces special tokens in the format $|x\rangle\langle y|$, where $x$ denotes blocks of a fixed stride ($\Delta$) and $y$ represents the remaining words. For a target length of 25 words with $\Delta=20$, the sequence begins with $|1\rangle\langle 5|$. These tokens are placed at regular intervals throughout the text, forcing the model to attend to its remaining "budget" at each step.

Comparison of training and inference phases for Vanilla, Gretel, and Hansel methods.

Balancing Precision and Fluency

To prevent abrupt cut-offs, a hyperparameter $\delta$ is used during training. This encourages the model to complete its current sentence naturally even after reaching the target length, ensuring that precision does not come at the cost of coherence. During inference, the model predicts these special tokens itself to guide the generation process.

Experimental Evaluation

We evaluated Hansel on summarization (CNN/DM, XSum) and dialogue (DailyDialog, MultiWOZ) tasks using diverse base models including Phi-2, BLOOM-3B, OPT-2.7B, and T5-3B.

Precision and Extrapolation

Hansel significantly outperforms prompt-based baselines. On the CNN/DM dataset with Phi-2, Hansel achieved a Mean Absolute Error (MAE) of just 0.39 words, compared to 1.56 words for the Gretel baseline. Crucially, Hansel demonstrates a unique ability to extrapolate. While standard models diverge wildly when asked for lengths outside their training range, Hansel maintains low error by following the learned countdown logic.

Mean Absolute Error (MAE) vs. target length for CNN/DM and DailyDialog datasets, showing Hansel's superior extrapolation.

Key Insights

  • A Global "Pulse": By providing periodic updates, Hansel gives the model a global context of its progress, solving the "lost in the woods" problem of local-only attention.
  • Architecture Agnostic: Because it operates at the token level, Hansel works across various positional encoding schemes (Rotary, ALiBi, etc.).
  • Eliminating Infinite Generation: The clear "zero" target effectively prevents smaller models from falling into repetitive loops or infinite generation.

Conclusion and Future Work

Hansel provides a robust, fine-tuning-based solution for precise length control in LLMs. While it requires a specific fine-tuning phase and introduces a minor token overhead, its benefits in precision and extrapolation are substantial. Future research will explore multi-constraint control, such as simultaneous word, sentence, and keyword constraints.

Summary updated

BibTeX
@inproceedings{song2025hansel,
  author = {Seoha Song and Junhyun Lee and Hyeonmok Ko},
  title = {{Hansel: Output Length Controlling Framework for Large Language Models}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2025},
  doi = {10.1609/aaai.v39i23.34700},
}
Figure