Token Cost Optimization: A Practical Guide for LLM Teams
Cut LLM spend with proven token cost optimization tactics. Learn prompt caching, compression, batching, and routing with real ROI examples and measurable

Thursday afternoon, a four-engineer team changes one prompt in a customer-support summarizer. The edit adds a few examples and a longer policy block. Nothing looks dangerous in review. By Friday morning, the team is staring at a $41,000 OpenAI invoice, roughly four times its normal run rate.
That kind of surprise is why token cost optimization has moved from a finance footnote to a platform-engineering responsibility. Longer context windows encourage teams to pass more material than the task needs. Retrieval systems add document chunks, agents add tool definitions and traces, and multi-model pipelines multiply overhead across every call. Eventually, leadership asks why the LLM line item rivals a database cluster.
The practical answer starts by separating four things: input tokens, output tokens, cached tokens, and wasted tokens. Once those buckets are visible, the best sequence is usually clear. Fix prompt waste first, cache stable context next, constrain output, then tune batching, concurrency, and model routing. The goal isn't to perform clever token surgery. It's to identify the changes that save meaningful money and time without creating quality failures that erase the savings.
Table of Contents
- The Moment Your AI Bill Becomes a Real Problem
- How Token Cost Actually Adds Up in Production
- Shrinking Prompts and Templates Before Inference
- Caching Repeated Context for Big Wins
- Trimming Output, Batching Requests, and Tuning Concurrency
- Routing Workloads Without Breaking Quality
- Instrumentation, ROI Tracking, and Your 30-Day Plan
The Moment Your AI Bill Becomes a Real Problem
The team in that Friday-morning scenario didn't create a new product. It made a small prompt change inside a high-volume workflow, and the production system multiplied that change across every request. The new policy text repeated information already available elsewhere, the examples expanded the prompt, and the summarizer began producing longer responses because the instructions no longer defined a tight output shape.
Finance saw the invoice. Engineers saw a prompt diff. Neither view explained the full chain from one edit to a large bill. That gap is where most token waste survives.
Why small changes become platform incidents
A production LLM request isn't just a user question. It can include a system message, conversation history, retrieved passages, few-shot examples, JSON schemas, tool definitions, tool results, and retry context. A support agent may resend much of that material on every turn, even when most of it hasn't changed.
The economics have also shifted in two directions. An independent analysis reported that the cost of generating one million tokens from a frontier model fell from $60 in 2020 to $0.05 in 2025, a 1,200x decline, while newer infrastructure research found that effective cost per million tokens varied by up to 36.3x on identical hardware depending on request rate and serving configuration, based on measurements across 42 benchmark runs. Those figures point to the same operational lesson: cheaper list prices don't eliminate waste caused by workload shape, serving choices, or uncontrolled retries. The analysis of falling token prices and serving variability provides useful context for that distinction.
Practical rule: Treat every prompt-template change like a production configuration change. Review its token impact, quality impact, and projected monthly cost before rollout.
The four buckets that matter
For cost reviews, label every billed token as input, output, cached, or wasted. Input is the context sent to the model. Output is the generated response. Cached tokens represent reusable context that a provider can price differently. Wasted tokens include duplicated instructions, irrelevant retrieval, unnecessary tool traces, retries, and completions nobody uses.
The rest of this guide sequences those levers by likely return. A team that measures the four buckets separately can find the expensive behavior quickly. A team that only watches the monthly invoice may shorten a prompt while leaving long outputs and retry storms untouched.
How Token Cost Actually Adds Up in Production
A useful cost review begins with one workload, not a collection of abstract tips. Consider a retrieval-augmented support agent handling 1.2 million requests per month on GPT-4o. The exact invoice depends on provider pricing, token mix, and account terms, so the percentages and dollar amounts below are an illustrative allocation for diagnosing the workload, not a universal benchmark.
Suppose the team assigns its monthly cost index across four buckets: 38% input, 47% output, 11% waste, and 4% savings attributable to caching. If the pre-optimization monthly bill is represented as $10,000, that translates to $3,800 of input spend, $4,700 of output spend, $1,100 of identifiable waste, and $400 of avoided cost from cached reuse. The important point is not the chosen total. It's that output can dominate even when the team spends its engineering time trimming prompts.
Read the bill as a workload map
Input grows through verbose system instructions, oversized retrieval chunks, few-shot examples, large JSON schemas, and repeated tool definitions. Output grows when a summarizer has no length ceiling, an agent narrates intermediate work, or a weak stop condition permits an answer to continue after the useful content is complete.
Wasted tokens need their own label because they often hide inside otherwise successful requests. A retry after a timeout may resend the full context. A tool loop may repeat the same schema and result. A retrieval layer may inject overlapping passages. Those tokens were processed, but they didn't create proportional business value.
| Token Bucket | Typical Driver | % of Monthly Cost | Example Monthly $ |
|---|---|---|---|
| Input tokens | Prompts, retrieval, history, schemas | 38% | $3,800 |
| Output tokens | Long summaries, explanations, agent responses | 47% | $4,700 |
| Wasted tokens | Retries, duplication, irrelevant context | 11% | $1,100 |
| Cached savings | Reused stable prefixes | 4% avoided | $400 avoided |
The operational sequence is straightforward. First, calculate tokens and cost by feature, model, provider, and request type. Next, compare the largest bucket with the easiest quality-safe intervention. A team may discover that removing a duplicated tool schema saves less than adding an output ceiling, or that caching stable policy text matters more than rewriting user prompts.
For an accessible explanation of the underlying inference cost model, see what LLM inference costs include. Without separate bucket accounting, teams cut the most visible text rather than the most expensive behavior, then wonder why the quarterly target remains out of reach.
Shrinking Prompts and Templates Before Inference
Prompt compression is usually the first high-return intervention because it prevents tokens from entering the request at all. Start with a representative production prompt, tokenize it with the target provider, and preserve a small evaluation set that checks factual accuracy, formatting, refusal behavior, and edge cases.
Take a support summarizer with a 612-token prompt containing three few-shot exemplars and verbose system instructions. A refactor reduces it to 204 tokens, a 67% reduction. The change keeps one representative example, removes duplicated role language, replaces narrative constraints with compact style tags, and moves stable policy text into a shared template rather than repeating it in service-specific variants.
The monthly dollar saving depends on the provider rate and the workload's input share. If the original input portion costs $3,800 per month, applying the reduction uniformly to that portion would avoid about $2,546 per month, before accounting for cache behavior, changed output, or quality-related rework. That's a planning estimate, not a guaranteed invoice result. Measure the actual token count after deployment.
Refactor the prompt in controlled passes
Use a diff rather than rewriting everything at once.
- Remove repetition. Delete instructions that restate the role, format, or policy already encoded elsewhere. Boilerplate such as “You are a helpful assistant” rarely carries the same value as a specific task contract.
- Collapse examples. Keep the example that demonstrates the hardest formatting or classification boundary. Three similar examples often add tokens without adding coverage.
- Tighten constraints. Replace paragraphs describing tone with compact requirements such as structured output, concise summary, or required fields.
- Trim retrieval. Reduce overlapping chunks and exclude passages that don't support the current task. Don't shrink context blindly if the model needs domain nuance.
- Deduplicate tools. Maintain one canonical description for each function. Tool-definition bloat can affect every agent turn.
- Centralize templates. Store stable prompt components in a versioned registry, then inject only request-specific variables. This prevents multiple services from drifting into verbose, incompatible copies.
A shared registry also makes rollback possible. If version 14 improves cost but harms a niche workflow, the team can restore that workflow to version 13 instead of editing several scattered strings.
Compare the prompt as an engineering artifact
| Prompt Element | Before (tokens) | After (tokens) | Monthly $ Saved |
|---|---|---|---|
| System instructions | 318 | 96 | Based on measured input rate |
| Few-shot examples | 204 | 78 | Based on measured input rate |
| Role and boilerplate | 36 | 12 | Based on measured input rate |
| Retrieval and variable scaffolding | 54 | 18 | Based on measured input rate |
| Total | 612 | 204 | About $2,546 in the illustrative input allocation |
The table shows the token movement, but production validation decides whether the refactor is acceptable. Run the old and new templates against a held-out evaluation set. Microsoft Research's LLMLingua work reported up to 20x compression with about 1.5% quality loss on GSM8K, while related summaries describe 2x to 20x compression with under 2% degradation across several benchmarks. Those results support testing compression, not applying maximum compression to every task. The discussion of LLMLingua and compression trade-offs explains why hidden context dependencies can make aggressive shortening fail outside spot checks.
Teams comparing prompt examples should also distinguish few-shot value from repetition. Few-shot prompting guidance is useful when deciding which examples earn their place in a production template.
Caching Repeated Context for Big Wins
Prompt caching lowers the cost of sending repeated prefixes. A stable system prompt, tool catalog, or policy block can be reused under the provider's caching rules instead of incurring the normal input rate on every request. The savings depend on prefix stability, cache lifetime, write charges, and the share of requests that hit the cache.
Anthropic's published pricing lists cache writes at 1.25x the base input-token rate for a 5-minute cache and 2x for a 1-hour cache. Cache hits and refreshes cost 0.1x the base input-token rate. On Claude Sonnet 4, the listed base input rate is $3 per million tokens, so a 5-minute write costs $3.75, a 1-hour write costs $6, and a cache hit costs $0.30 per million tokens. Anthropic's prompt caching documentation provides the provider-side rules behind this calculation.
Find the prefix that repeats
A Project Discovery-style workload sends a 6,000-token system prompt across 50,000 daily requests. With a 70% cache hit rate, the example reduces monthly spend from $4,500 to roughly $1,800, saving about $2,700 per month. Treat those figures as a scenario, not a forecast. Provider pricing, cache lifetime, writes, misses, and request distribution determine the result. Project Discovery's production caching report shows how stable prefixes can produce meaningful savings when reuse is measured and maintained.
Put stable material first:
- System policies: Keep versioned instructions at the beginning of the request.
- Tool definitions: Place unchanged schemas in the reusable prefix.
- Tenant configuration: Separate shared settings from per-tenant content to limit cache fragmentation.
- Conversation structure: Keep volatile user text after stable context when the provider requires prefix matching.
Invalidate deliberately. A prompt-version change should create a new cache key or prefix version. Reusing stale instructions can turn a cost optimization into a correctness incident. Semantic caching is different. It reuses answers for sufficiently similar queries and requires stronger freshness, authorization, and privacy controls than provider-level prefix caching.
Calculate the break-even point
Caching has an entry cost. A write costs more than a normal input pass under the Anthropic pricing example, so the prefix needs enough subsequent reads to recover that premium. The break-even hit rate varies with cache duration, read volume, provider rules, and whether the prefix would otherwise be sent in full.
Independent benchmarking across four models found statistically significant caching savings across all tested providers, with total reductions ranging from 45% to 80%. The reported ranges were 79% to 81% for GPT-5.2, 78% to 79% for Claude Sonnet 4.5, 46% to 48% for GPT-4o, and 28% to 41% for Gemini 2.5 Pro, depending on cache mode. Use the benchmark's full results only as evidence for those tested conditions, not as a promise for every application.
Measure cache-write tokens, cache-read tokens, misses, prefix versions, time to first token, and total cost by provider. Tie those fields to workload ownership so a FinOps review can show whether OpenAI, Anthropic, and Google deliver comparable reuse. AI FinOps practices for attribution and review can help turn cache behavior into an operating metric rather than a provider-specific curiosity.
Trimming Output, Batching Requests, and Tuning Concurrency
Input reduction gets attention because prompts are visible in code. Output control often has greater impact because providers commonly price output tokens above input tokens. Current guidance notes that flagship models often charge about 4 to 5 times more for output than input, so a verbose answer can cost more than the context that produced it. The analysis of output-token optimization covers practical controls such as maximum token limits, structured outputs, extraction, and tighter instructions.
A support summarizer that emits a 900-token response can often produce a useful 280-token result when the contract asks for fixed fields, concise bullets, and no repeated transcript. In the supplied GPT-4o scenario, that changes the illustrative per-request cost from $0.0045 to $0.0014, saving $0.0031 per request. At scale, multiply that difference by successful requests, then subtract any rework caused by missing detail.
Match the lever to the workload
| Tactic | Avg output tokens (before) | Avg output tokens (after) | Cost per request (before) | Cost per request (after) | p95 latency impact |
|---|---|---|---|---|---|
| Output ceiling and field schema | 900 | 280 | $0.0045 | $0.0014 | Measure in production |
| Concise extraction format | Long narrative | Required fields only | Provider-dependent | Provider-dependent | Measure in production |
| Asynchronous batching | Small separate calls | Aggregated request | Provider-dependent | Provider-dependent | Higher latency is expected |
| Queue-based concurrency | Retry-heavy burst | Controlled dispatch | Provider-dependent | Provider-dependent | Stabilizes tail latency |
Set a max_tokens ceiling, but don't confuse a ceiling with a target. Add explicit length requirements, use structured output where appropriate, and prefer extraction over generation when the application needs fields rather than prose. Strip unused markdown and trailing material only after confirming that the model isn't relying on it for a required format.
Batching fits asynchronous enrichment, document extraction, and report generation better than interactive chat. Aggregate requests with clear delimiters and a machine-readable response contract. The batch stops helping when the combined prompt becomes large, latency requirements become strict, or one slow item holds up the group. Test batch size against throughput, error handling, and p95 latency rather than assuming aggregation always wins.
Concurrency needs similar discipline. Aggressive parallelism can trigger rate-limit retries, which resend billable tokens and can create a second cost spike on top of the original burst. A queue, bounded worker pool, exponential backoff, and deliberate jitter let the service maintain throughput without turning provider errors into duplicate inference.
Routing Workloads Without Breaking Quality
Sending every request to a flagship model is simple, but simplicity can become expensive when the workload contains many routine tasks. A routing layer classifies requests by complexity, then sends easy work to a smaller model while reserving frontier capacity for difficult reasoning, ambiguous cases, and high-risk decisions.
The classification signal can come from rules, embeddings, historical outcomes, or a lightweight router model. A practical policy might route short, well-formed extraction requests to a small model and escalate requests involving conflicting evidence, policy interpretation, or multi-step reasoning. The decision should be based on evaluation results, not model size alone.

A 2026 guide reports that smaller models can be 5 to 10 times cheaper per token, while another documented recommendation frames routing as a major architecture lever. The guide to model routing and workload economics supports the direction, but the correct blend still depends on quality, latency, and rework.
Calculate savings after escalation
For a workload split 70% easy and 30% hard, the supplied scenario uses a blended cost of $0.0008 per request instead of $0.006 when every request goes to the flagship model. The difference is $0.0052 per request before router cost, escalation, monitoring, and rework. At high volume, that can become a meaningful monthly reduction. At low volume, it may not justify another model call and another operational path.
The risk is cheap-but-wrong output. If the smaller model fails and the system retries against the flagship, the request may consume the original input tokens again, add latency, and erase the expected saving. Set confidence thresholds, log escalation rates, compare quality by route, and sample routed responses with human reviewers. Track the cost of rework as part of the routing calculation.
A route is successful only when the cheaper first answer remains correct often enough that escalation and repair don't consume the savings.
Don't route regulated or high-consequence work merely because the unit price looks attractive. Consistency, traceability, and reviewability may matter more than cents per call. Also avoid routing when the workload is too small to amortize classifier overhead or when the task distribution changes faster than the evaluation process can detect.
Routing deserves a controlled rollout. Start with a shadow classifier, compare its recommendation with the current flagship result, then enable it for a narrow task class. Expand only when quality, escalation, latency, and cost all remain inside their limits.
The following walkthrough shows how routing can be implemented as a production decision rather than a pricing trick.
Instrumentation, ROI Tracking, and Your 30-Day Plan
Optimization becomes durable when every inference produces an auditable cost record. Wrap the provider call with a decorator or middleware layer that records prompt tokens, completion tokens, cache reads, cache writes, model, provider, latency, retry count, workflow, and estimated cost. Then aggregate by feature and release so a prompt change can be compared with the previous version before the invoice arrives.
A lightweight wrapper doesn't need to alter the OpenAI or Anthropic client. The pattern is simple: start a timer, call the existing function, read usage metadata, calculate cost from the active price table, and emit one ledger row. Tags such as support_summary, tenant_segment, experiment, or endpoint turn an opaque provider bill into an ownership map.
Prioritize work by return
| Week | Focus Area | Tactic | Target Metric | Expected Savings |
|---|---|---|---|---|
| Week 1 | Prompt audit | Compress templates, remove duplication, trim retrieval | Input tokens per request and quality score | Quantify the largest input reduction |
| Week 2 | Cache rollout | Reuse stable prefixes and version invalidation | Cache-hit rate, cache-read tokens, first-token latency | Convert repeated context into discounted reads |
| Week 3 | Output and throughput | Add output limits, structured extraction, batching | Output tokens, retry rate, throughput | Reduce generation and avoid duplicate work |
| Week 4 | Model mix | Route simple traffic and test cheaper alternatives | Escalation rate, quality, blended cost | Lower cost on eligible requests |
| Throughout | Instrumentation | Record usage, model, tags, latency, and cost | Attribution coverage and anomaly detection | Prevent savings from regressing |
In the first week, audit the largest templates and compare old and new prompts on a held-out evaluation set. In the second, identify prefixes that remain stable across calls and measure actual reuse. During the third, constrain output and move eligible asynchronous work into queues. In the fourth, test routing with confidence thresholds and human sampling.
A platform such as SpendLens AI's AI observability approach can provide a developer-focused way to attribute calls by project, provider, model, and workload, while surfacing token usage, cache efficiency, prompt waste, and model-switch opportunities. Use it alongside your existing provider telemetry, not as a substitute for task-quality evaluation.
Questions that surface after the first audit
How should shared prompt overhead be attributed? Assign stable shared context to the workflow that owns the template, then report it separately from request-specific tokens. For cross-service prompts, use a shared cost category and allocate it by measured request volume or token contribution.
When do cache discounts outweigh write costs? Calculate the expected cost of the write plus reads and compare it with resending the prefix at the normal input rate. The answer changes with cache lifetime, hit rate, prefix size, and provider pricing, so use observed cache events rather than a generic threshold.
What is a healthy input-to-output ratio? There isn't one universal ratio. Extraction, classification, chat, and summarization have different legitimate shapes. Compare the ratio with task quality, cost per business outcome, and the amount of output users consume.
How do engineers defend optimization work to finance? Report avoided spend and time together. Show the baseline monthly cost, the exact change, quality guardrails, latency movement, and expected savings at current volume. Finance can approve a prompt refactor more confidently when the ledger links it to a feature, release, and business unit.
SpendLens AI offers lightweight instrumentation for OpenAI and Anthropic workloads, including spend attribution, cache-efficiency visibility, prompt-waste signals, and model-switch recommendations. Visit SpendLens AI to connect token usage with concrete optimization opportunities before the next unexpected invoice.