TLDR: A new research paper introduces CAPEL, a prompt-based, one-shot strategy that enables Large Language Models (LLMs) to generate text with exact length control. By appending countdown markers and explicit counting rules to prompts, the method externalizes the counting process, significantly improving length compliance (e.g., GPT-4.1 from <30% to >95%) without fine-tuning or iterative sampling, while preserving output quality. This offers a lightweight alternative to traditional training or decoding-based methods for precise text generation.
Large Language Models (LLMs) have become incredibly powerful, but one persistent challenge they face is generating text with an exact, specified length. Whether it’s a headline, a social media post with character limits, or a one-sentence summary, outputs that are too long or too short are often unusable. This seemingly simple task of counting and stopping at a precise number of words or characters has proven difficult for these advanced AI models.
Traditional methods to tackle this problem often involve complex solutions like retraining the models, adding special components, or using reinforcement learning. While these can work, they are usually expensive, require access to the model’s internal workings, and don’t always guarantee perfect accuracy. Other techniques involve multiple attempts or revisions, which can be slow and inefficient, especially for proprietary models where internal access is not possible.
A new research paper, titled “Prompt-Based One-Shot Exact Length-Controlled Generation with LLMs,” by Juncheng Xie and Hung-yi Lee, introduces a remarkably simple yet highly effective solution. Their method, called Countdown-Aided Prompting for Exact Length (CAPEL), achieves precise length control using only prompt engineering. This means no fine-tuning, no complex decoding, and no multiple attempts – just a cleverly designed prompt.
The core idea behind CAPEL is to externalize the counting process. Since LLMs struggle with internal counting, the researchers decided to make the counting visible to the model. They do this by appending a special “countdown-marker suffix” to the user’s prompt. This suffix includes clear rules and markers that the model must follow as it generates text.
Imagine you ask an LLM to write a 5-word sentence. With CAPEL, the prompt would instruct the model to print a marker like <5>, then one word, then <4>, then another word, and so on, until it prints <1> followed by the last word, and finally <0> to signal completion. The rules also specify that these markers and punctuation don’t count towards the target length, only the actual words do. This turns the generation process into a “writing while counting” exercise, where the model’s job is reduced to a pattern completion task.
For example, if the target length is 5, a correct output would look like: <5>Hello,<4>world!<3>How’s<2>everything?<1>Great.<0>. This visible countdown acts like an external scratchpad, guiding the model to produce the exact number of tokens.
The researchers put CAPEL to the test across various scenarios, including open-ended text generation, summarization (XSUM), instruction following (MT-Bench-LI), and strict length adherence (LIFEBENCH). They evaluated it on a range of LLMs, including models from OpenAI (like GPT-4.1 and GPT-4o) and open-source models (like Qwen3 and DeepSeek).
The results were striking. On MT-Bench-LI, for instance, GPT-4.1’s ability to strictly comply with length instructions jumped from below 30% with naive prompts to over 95% with the CAPEL countdown prompt. Similar significant improvements were observed across almost all models and tasks, in both English (word-length control) and Chinese (character-length control). Importantly, this precise length control did not compromise the quality of the generated text.
While CAPEL proved highly effective for most models, some smaller or highly safety-focused models, like o4-mini and gpt-4o-mini, occasionally struggled. They sometimes misinterpreted the countdown as a request for a “chain-of-thought” explanation or prematurely stopped. However, these were exceptions, and the overall trend showed massive gains in length compliance.
The success of CAPEL highlights a fundamental insight: LLMs are not inherently good at counting. Their self-attention mechanism lacks an explicit internal counter. By externalizing this counting process through the prompt, CAPEL transforms a difficult internal task into a simpler pattern-matching one, allowing the models to achieve near-perfect length control. This approach offers a lightweight and accessible alternative to more resource-intensive training or decoding methods.
Also Read:
- Language Models Learn to Think and Refine on the Fly
- Boosting LLM Logical Reasoning with Structured Chain-of-Thought
This research demonstrates that precise length control, a critical feature for many real-world AI applications, can be achieved effectively through clever prompt engineering alone. For more details, you can read the full research paper here.


