DEV Community

EvvyTools
EvvyTools

Posted on

How to Cut Token Usage Without Making Your Prompts Worse

The easy way to reduce token cost is to make prompts shorter. The hard part is doing it without quietly degrading output quality in a way that doesn't show up until a user complains weeks later. A few techniques actually reduce token count while keeping the model's behavior intact, and a few popular ones don't work as well as they look like they should.

Why Token Reduction Is Worth the Engineering Effort

It's tempting to treat token optimization as a nice-to-have you'll get to eventually, but the math tends to favor doing it earlier than that. A 20 percent reduction in average prompt size applies to every single call an application makes for as long as it runs, which compounds very differently than a one-time engineering cost. For anything expected to run at meaningful volume for months or years, the cumulative savings from a focused optimization pass, done once, usually dwarfs the time spent doing it.

Start With What's Actually Taking Up Space

Before trimming anything, it helps to know where the tokens are actually going: the system prompt, few-shot examples, retrieved context in a RAG setup, or conversation history carried forward from earlier turns. Guessing which part is the bulk of the cost usually gets it wrong. Running the full assembled prompt through an actual tokenizer, not eyeballing word count, shows the real breakdown before you start cutting.

Technique 1: Trim Few-Shot Examples to the Minimum That Still Works

Few-shot examples are one of the most effective ways to steer model behavior, and also one of the most expensive, since each example gets sent in full on every single call. The fix isn't removing them outright, it's testing how few you actually need. Many prompts that ship with four or five examples perform identically with two, and the difference is pure token savings with no quality change. This only works if you actually test it against a representative set of cases rather than assuming fewer examples is automatically fine.

Technique 2: Summarize Conversation History Instead of Replaying It in Full

Chat applications that pass the entire conversation back on every turn pay for that full history again and again as a conversation grows longer. A common fix is periodically summarizing older turns into a compact recap and dropping the verbatim exchange, keeping only the most recent few turns in full. This trades a small amount of fidelity on very old context for a meaningful reduction in tokens sent on every later message, and for most conversational use cases the tradeoff is barely noticeable to the user.

Technique 3: Retrieve Less, More Precisely, in RAG Pipelines

Retrieval-augmented setups often over-retrieve, pulling in more chunks of context than a query actually needs "just in case," which pads every call with tokens the model doesn't use meaningfully. Tightening the retrieval step, either by reducing the number of chunks returned or by using a re-ranking pass to keep only the most relevant ones, cuts input tokens directly. The LangChain documentation covers several retrieval and re-ranking patterns if you're building a RAG pipeline and haven't tuned this yet, and Hugging Face's documentation covers embedding and re-ranking models if you're assembling a retrieval pipeline from lower-level pieces instead of a framework.

Measuring how many of your retrieved chunks the model actually references in its output, versus how many are just along for the ride unused, is a useful diagnostic here. If a retrieval step consistently returns chunks the model never draws on, that's a fairly direct signal the retrieval count can come down without a quality hit.

Technique 4: Cache Static Instructions Instead of Re-Sending Them

Several providers now support prompt caching for content that stays identical across calls, typically the system prompt and any fixed few-shot examples, documented for Claude models in Anthropic's prompt caching guide. When the cacheable portion of a prompt doesn't change between requests, repeat calls get billed at a steep discount on that portion instead of full price every time. This changes the tradeoff calculus meaningfully: a longer, carefully tuned system prompt that would be expensive sent raw on every call becomes much cheaper once caching applies, so it's worth checking whether your provider and setup qualify before spending engineering time shrinking a prompt caching would have handled for free.

Technique 5: Compress Retrieved Context Before It Enters the Prompt

In RAG pipelines specifically, retrieved chunks often carry formatting, boilerplate, or repeated headers that add tokens without adding information the model actually needs. Stripping this before it's inserted into the prompt, keeping just the substantive content, reduces token count without touching the actual information available to the model. This is a smaller win than tightening the retrieval step itself, but it's a genuinely free one since it doesn't change what gets retrieved, only how verbosely it's formatted once it does.

What Doesn't Work as Well as It Looks Like It Should

Aggressively abbreviating instructions, stripping articles and connecting words to save a few tokens, rarely helps and sometimes hurts. Models are trained on natural language, and unnaturally compressed instructions can produce less reliable output for a token savings that's usually tiny compared to trimming actual redundant content like unnecessary examples or stale conversation history.

Similarly, switching to a cheaper model purely for lower per-token pricing without checking whether it needs a longer, more detailed prompt to hit the same quality bar can end up costing about the same, or more, once the prompt length difference is accounted for.

Removing entire capabilities to save tokens, like dropping a tool definition an application rarely uses, is also worth scrutinizing before doing it purely for cost. If that tool handles even a small percentage of real user requests well, the token savings from removing it has to be weighed against the cost of those requests failing or falling back to a worse experience, which is a product decision, not just a token-count optimization.

When Optimization Isn't Worth the Engineering Time Yet

Not every project is at the stage where this effort pays off. If you're still validating whether a feature is worth building at all, spending a week tuning prompt length is premature; the bigger question of whether the feature works and whether people want it hasn't been answered yet. Token optimization earns its place once a feature has product-market signal and is heading toward meaningful volume, not before.

Measuring Prompt Quality, Not Just Length

None of these techniques matter if you're only measuring token count and not checking whether output quality actually held up. Building even a small, informal set of representative test inputs with expected outputs, and running it against both the original and the trimmed prompt, catches regressions that a shorter-looking prompt wouldn't otherwise reveal until a real user hit the edge case in production.

Verify the Savings Before Assuming It Worked

Whatever techniques you apply, the only way to know they actually reduced cost is to measure the before-and-after token count on the same representative prompt, not assume a shorter-looking prompt used fewer tokens. Running both versions through this free tool shows the real numbers side by side, including how the change affects estimated cost across different target models, since a trim that helps GPT-4o's tokenizer doesn't necessarily help Claude's by the same margin.

For more on why AI API costs are so easy to misjudge in the first place, this breakdown of common estimation mistakes covers the tokenization and pricing gaps that make guesswork unreliable.

Top comments (0)