Token Optimizer
Reduce your token usage by 40-50% in production
Created on 15th February 2026
•
Token Optimizer
Reduce your token usage by 40-50% in production
The problem Token Optimizer solves
The Problem It Solves
AI agents waste tokens (and money) because they routinely ship oversized prompts to LLM providers: full chat history, verbose tool schemas, repeated logs, and large RAG dumps, even when only a small slice is needed. This creates three production pain points:
High and unpredictable cost: token usage balloons with context size, and cost scales linearly with every extra chunk of text.
Latency and reliability issues: larger prompts slow requests, increase timeouts, and make systems harder to scale.
Quality regressions from noise: irrelevant context can distract the model, causing worse answers despite higher spend.
This middleware layer sits between agents and LLM providers to reduce prompt size and cost without reducing quality. It does so by:
- Removing obvious waste deterministically (whitespace, duplicates, filler, noisy logs, bloated tool schemas).
- Selecting only the most relevant context when over budget (semantic selection over RAG/tool outputs).
- Compressing content when needed (including large single-message blobs when configured).
- Caching optimized prompts and results so repeated or similar requests avoid repeated work and spend.
Challenges we ran into
One of the hardest parts of building the optimizer wasn’t the models or the middleware—it was making the system behave consistently across very different prompts and use cases. In early tests, the same compression or routing strategy that worked well for short customer-support queries would completely break longer analytical prompts or tool-heavy agent chains. We kept seeing trade-offs: more aggressive compression saved tokens but hurt accuracy, while safer settings preserved quality but reduced the cost savings.
This forced us into multiple cycles of real-world testing, logging token flows, and manually inspecting outputs. We had to carefully tune how the token budget is split between user, assistant, tool, and document context, decide how much of the conversation history to keep, and set faithfulness thresholds so compression wouldn’t distort meaning. Even small changes—like shifting the assistant budget from 0.3 to 0.25 or increasing the safety margin—would impact both quality and cost across different agents.
The current production configuration is the result of those iterations: a balance between aggressive compression (50% target), strict faithfulness checks, limited conversation history, and carefully allocated token budgets per message type. It wasn’t derived from theory alone—it came from repeated tuning across real prompts until we found settings that consistently reduced cost without breaking outputs.
Tracks Applied (1)