Abstract
Large Language Models (LLMs) frequently struggle to adhere to precise output length constraints, often failing to generalize beyond the lengths encountered during training. We present Hansel, a lightweight, architecture-agnostic finetuning framework that equips LLMs with an internal "clock" to track output progress. By injecting periodic hidden special tokens that act as a countdown budget, Hansel achieves significantly higher precision in length control and demonstrates superior extrapolation capabilities to unseen lengths compared to standard prompt-based methods.
Introduction
Despite their linguistic prowess, LLMs are notoriously poor at counting. A request to "summarize in exactly 20 words" often results in outputs of varying lengths because models process tokens rather than words and lack a dedicated mechanism to monitor progress against numerical constraints. While previous attempts to solve this involved modifying positional encodings or attention mechanisms—often requiring expensive retraining—modern prompt-based approaches (simply specifying length in the prompt) are brittle and fail to generalize. Hansel addresses these gaps by treating length control as a structural task integrated into the finetuning process.
The Hansel Framework
The core innovation of Hansel is the use of "Hidden Arrangements" of special tokens. During finetuning, the target text is augmented with periodic "countdown" tokens that guide the model's generation.
Hidden Arrangements and Periodic Reminders
Special tokens, formatted as $|x\rangle\langle y|$, are inserted into the training data. In this notation, $x$ represents the number of "strides" (intervals of $\Delta$ words) remaining, and $y$ is the remainder. For instance, with a stride $\Delta=10$, a token $|2\rangle\langle 5|$ indicates exactly 25 words remain in the budget. These tokens appear every $\Delta$ words, acting as periodic reminders that allow the model to adjust its generation pace dynamically.
Natural Termination
To prevent the model from cutting off abruptly upon reaching the zero-token ($|0\rangle$), the authors introduce a hyperparameter $\delta$. This ensures the model is trained on examples where a few words follow the final countdown token, teaching it to conclude sentences naturally rather than stopping mid-thought.
Experimental Setup
The framework was evaluated using the Phi-2 (2.7B) model across several diverse tasks:
- Summarization: CNN/DM (long-form) and XSum (abstractive/short).
- Dialogue: DailyDialog (multi-turn) and MultiWOZ (task-oriented).
- Cross-Task Generalization: WMT19 (German-to-English) was used to verify if length-control skills could transfer between domains.
Hansel was compared against two baselines: Vanilla (standard finetuning) and Gretel (prompt-based length-aware finetuning).
Key Results and Discussion
Hansel demonstrated a marked improvement in both precision and robustness across all tested datasets.
Precision and Extrapolation
Hansel reduced the Mean Absolute Error (MAE) of output length by 45% to 75% relative to the Gretel baseline. The most significant advantage appeared during extrapolation tests. When tasked with generating 130 words—a length far exceeding the typical 10-20 word responses in the DailyDialog training set—Hansel maintained a tight MAE of 2.07, whereas Gretel's error spiked to 18.51.
Quality and Reliability
Crucially, the strict length constraints did not degrade text quality. ROUGE and G-Eval scores remained competitive, indicating that the model successfully balances linguistic fluency with numerical accuracy. Furthermore, Hansel effectively eliminated the "infinite generation" bug common in smaller LLMs, as the model is trained to terminate strictly after reaching the end of its countdown budget.
Conclusion and Limitations
Hansel provides a robust, plug-and-play solution for length control that works across various positional encoding schemes (e.g., Rotary, ALiBi). While the framework introduces a minor token overhead due to the hidden countdown tokens, the gains in precision and extrapolation are substantial. Future research may explore applying this method to finer units, such as character-level control, and further refining the balance between strict numerical adherence and natural linguistic completion.
Summary updated