AI cost optimization is mostly structural. Most token-reduction advice is about shorter prompts, and that is the smallest lever available. That is the smallest lever available, and it often makes the output worse.
The larger levers are structural: what stays stable between calls, how long a session runs, which model handles which task, and how much raw data a tool returns. These seven changes attack those, in the order I apply them.
1. Stabilize the prefix
What it does. Puts everything that does not change into a byte-identical block at the start of the prompt, so the provider can serve it from cache.
Why it works. Cache lookup is a prefix match. The moment a byte changes, everything from that byte onward is a miss.
How to do it. Move timestamps, run IDs, user names, and "current time is" strings to the end of the prompt. Keep the system block, skills, memory, and tool schemas stable between turns.
How to verify. Miss tokens divided by session count should be close to one prefix. If it is several prefixes per session, something is mutating.
What not to do. Do not remove rules to shrink the prefix. A shorter prompt that produces worse output is not a saving.
2. Run longer sessions
What it does. Amortizes the cold prefix over more calls.
Why it works. A cold start costs one full prefix. Over 100 turns it is noise. Over two turns it is the whole bill.
How to do it. Batch related work into one session. For repetitive background jobs that ask one question, use a deterministic script instead of an agent session, because a script costs zero tokens and pays no cold prefix.
How to verify. Look at the worst sessions by hit rate. If they are one to three calls, the pattern is confirmed.
3. Route by task
What it does. Sends classification and extraction to a small model, and reasoning to a frontier model.
Why it works. Model prices differ by more than an order of magnitude, and most routine work does not need the frontier.
How to do it. Write a routing table by task type, not by habit. Add a fallback chain that escalates only when the cheap tier fails or returns low confidence. Keep the router in control of escalation; the small model should never decide to promote itself.
How to verify. Track cost per successful task, by route. If the fallback rate is above 10 percent, the primary tier is too weak.
What not to do. Do not route sensitive data to a cheaper model just because it is cheaper. The data class decides the route before cost does.
4. Shrink tool payloads at the source
What it does. Filters, paginates, and summarizes tool responses before they enter the context.
Why it works. A tool that returns 20,000 tokens of raw JSON puts those tokens in the context, and every later turn resends them.
How to do it. Return only the fields the task needs. Paginate by default. Summarize long results at the server. Let an embedded router synthesize before returning to the host model.
How to verify. Measure the tokens per tool call. If one tool dominates, fix that tool first.
What not to do. Do not ask the model to "ignore" irrelevant output. It still pays for it.
5. Batch what can wait
What it does. Groups non-urgent requests into a batch endpoint.
Why it works. Many providers price batch work at roughly half the synchronous rate, because the work can be scheduled.
How to do it. Separate interactive work from background work. Reports, classification backfills, and bulk summaries go to the batch path.
How to verify. Compare the batch line with the synchronous line for the same volume.
What not to do. Do not batch anything a person is waiting on.
6. Compress with discipline
What it does. Summarizes or trims conversation history when the context approaches its limit.
Why it works. Less context means fewer input tokens per turn.
How to do it. Compress once, late, and never inside a cached prefix. Keep the decisions and the current task; drop the exploratory turns.
How to verify. Watch the hit rate around the compression event. A compression that rewrites history turns the next several calls into misses.
What not to do. Do not compress five times in one session. One event is a saving. Five is a miss pump.
7. Put a ceiling on the spend
What it does. Caps the cost of a task, a session, and a month.
Why it works. An agent with no budget can spend without a human noticing, especially when a retry loop is involved.
How to do it. A per-task token budget, a per-session ceiling, a monthly alarm at 80 percent, and a kill switch that has been tested. Log every route and every retry.
How to verify. The alarm fires in staging before it fires in production.
What not to do. Do not rely on a provider spending limit as your only control. It is a backstop, not a design.
The order matters
Apply them in this order: stabilize the prefix, lengthen sessions, route by task, shrink payloads, batch, compress, cap.
The first two change the shape of the bill. The next two change its size. The last three keep it from drifting.
Doing them out of order produces disappointing results. Shortening a prompt while the prefix is unstable is wasted effort, because the cache never hits anyway.
What the numbers look like when it works
On a production Hermes deployment measured from June 9 to July 8, 2026:
- 9,691,748,399 tokens processed.
- 96.61% served from cache.
- $210 total cost.
The same workload at public API pricing on GPT-6 Astra is estimated at roughly $14,900 with caching, or roughly $99,100 without. Those are estimates for one workload and one period, not a promise.
The point is not the provider comparison. It is the shape: a large stable prefix, a high cache hit rate, and a small bill for a large volume.
Where cost programs fail
They start with the prompt. The prompt is the smallest lever and the one most likely to hurt quality.
They switch models first. A new model changes quality and behavior, and the bill may not move much if the prefix is unstable.
They measure tokens, not cost per task. A cheap model that fails more often is not cheaper.
They skip the retry log. Retries are invisible in the total and often the largest avoidable line.
They optimize once. Model prices and behavior change. Re-check the routing table monthly.
Related
- AI token cost: the four lines on the bill.
- Prompt caching: the structural rules behind the biggest lever.
- Model routing: the routing table and the fallback chain.
- Context window management: what to keep and what to drop.
- Agent cost control: budgets and kill switches.
Questions owners ask
A stable prefix. If the system prompt contains a timestamp, removing it can move the hit rate from zero to most of the traffic in one change.
No. If it removes the rules that prevent retries and bad outputs, it costs more per successful task.
It depends on the task mix. If most of the work is classification, routing it to a small model can remove the majority of the frontier spend.
Only to work that can wait. Interactive agent turns cannot be batched. Background jobs can.
Monthly, or after any model change. Pricing and model behavior move faster than most budgets.

