You can reduce LLM API costs without making an AI product worse by sending fewer unnecessary tokens, reusing work that has already been computed, and reserving expensive models for requests that truly need them. The highest-leverage approach is usually not one setting; it is a disciplined request design that measures tokens, makes repeated context cacheable, trims irrelevant history, routes by task difficulty, and moves delay-tolerant jobs off the synchronous path.
This matters most for AI agents and chatbots. A single customer interaction can trigger a system prompt, conversation history, retrieved documents, tool definitions, tool results, and a long response. If every turn sends all of that material to the same premium model, spend rises faster than useful work. Start by treating tokens, model choice, and latency class as explicit product decisions.
Start with a cost map, not a prompt rewrite
Before optimizing, record usage by endpoint, workflow, model, customer segment, and request outcome. Separate input tokens, cached input tokens, output tokens, retries, tool calls, and failed requests. Averages can hide the real problem: a small share of long agent sessions or repeated requests often creates most of the bill.
Use a simple request taxonomy
- Repeated-context requests: share a long system prompt, policy, product catalog, schema, or tool definitions.
- Repeated-answer requests: ask the same or meaningfully similar question repeatedly.
- Long-context requests: include history, retrieval chunks, attachments, or tool results.
- High-volume offline requests: classification, extraction, enrichment, evaluation, or content processing that can wait.
- Complex requests: need stronger reasoning, careful synthesis, or a human-review path.
For each group, track a quality metric alongside cost: task success, resolution rate, groundedness, escalation rate, or reviewer acceptance. Cost per request is not the goal; cost per successful outcome is.
Make repeated context cheaper with prompt caching
Prompt caching reuses computation for a repeated prompt prefix. It is most useful when many requests begin with the same long instructions, knowledge, schemas, or tool definitions. Providers have different rules, but the operational principle is consistent: keep stable content at the beginning and place changing user-specific material later.
Structure prompts for cache hits
- Put stable system instructions first.
- Keep reusable tool definitions and output schemas in a consistent order.
- Place account-specific facts, timestamps, retrieved passages, and the latest user message after the stable prefix.
- Version prompt templates deliberately instead of making tiny uncontrolled edits.
- Log cache-hit fields from API responses and compare hit rate by endpoint.
Do not inflate a prompt just because it can be cached. Cached tokens still cost money, and an overly broad instruction block can weaken answers. The right target is a stable prefix that is both necessary and reused frequently.
Use semantic caching for repeatable answers, with guardrails
Prompt caching saves work on the input side. Semantic caching can avoid a model call altogether by returning a previously approved answer when a new request is sufficiently similar in meaning. It works well for stable FAQs, internal policy questions, product documentation, and routine support questions.
Semantic caches require stricter controls than exact-match caches. Define a similarity threshold, scope the cache by tenant and permissions, set a time-to-live, and attach source or content versions to each entry. Never serve a cached answer for a question where freshness, authorization, pricing, inventory, medical guidance, legal advice, or account state changes the correct result.
Good and poor semantic-cache candidates
Semantic cache decision guide
| Candidate | Why it fits or fails | Control to add |
|---|---|---|
| Stable help-center FAQ | Many users ask equivalent questions and the answer changes infrequently. | Content-version key and expiry. |
| Personal account balance | The answer depends on live, private data. | Bypass semantic cache; retrieve current data. |
| Internal policy explanation | Can be reusable when the policy version is stable. | Permission scope and policy-version key. |
Trim context before it reaches the model
Long context is not automatically useful context. Agents often resend every conversation turn, every retrieved document, and every tool result because it is convenient. Instead, give each type of context a budget and a reason to exist.
Apply a context budget
- Conversation: retain the current goal, unresolved constraints, and decisions; summarize completed portions.
- Retrieval: retrieve fewer, better chunks and deduplicate overlapping passages before prompt assembly.
- Tools: pass concise structured results rather than raw logs, HTML, or full API payloads.
- Instructions: remove duplicate rules and move enforcement into deterministic application code where possible.
- Attachments: extract only the sections needed for the task instead of attaching an entire document by default.
Context compression should be evaluated, not assumed. A summary that drops a customer's constraint can create a costly follow-up call or an incorrect action. Test compression against representative conversations and include a fallback to retrieve the original source when the agent signals uncertainty.
Route requests to the smallest model that meets the bar
Model routing assigns simple, repetitive, or well-bounded work to lower-cost models while reserving stronger models for ambiguity, complex reasoning, high-value actions, or low-confidence cases. The key is to route based on measured task quality, not a vague assumption that one model is always cheaper or better.
Begin with a small evaluation set for each workflow. Compare at least two model tiers on quality, latency, cost, and failure modes. Then use deterministic rules where they are reliable: for example, route extraction with a fixed schema to a smaller model, and escalate only when validation fails. For more open-ended work, a lightweight classifier or confidence check can decide whether a stronger model is justified.
A routing policy needs safeguards: sample lower-tier outputs for review, monitor escalation rate, retain an override path for high-risk actions, and update evaluations when prompts, tools, or models change. Routing that lowers the token bill but increases correction work is not an optimization.
Match processing mode and output length to the job
Use synchronous calls for interactions where a person is waiting or a downstream action must happen immediately. Use batch or other asynchronous processing for large, non-urgent jobs such as enrichment, tagging, nightly evaluations, document extraction, and bulk content review. Some providers discount batch processing in exchange for a longer completion window, so the savings are meaningful only when the workflow can tolerate delay.
Output is another controllable cost. Ask for the shortest format that completes the task: a classification label, a JSON object with required fields, a five-bullet summary, or an answer with a defined word limit. Set output-token limits, request concise reasoning only where appropriate, and validate structured responses in code. Do not use a low output limit for work that needs explanation, citations, or safety-sensitive nuance; measure quality after the change.
A practical API cost-reduction checklist
- Instrument input, cached input, output, retries, and tool-call usage per workflow.
- Rank workflows by total spend and cost per successful outcome.
- Stabilize long reusable prompt prefixes and monitor cache-hit rate.
- Add exact or semantic caching only for answers that are safe to reuse.
- Set context budgets for history, retrieval, tool results, and attachments.
- Test smaller models on bounded tasks and define escalation rules.
- Move delay-tolerant volume to batch or asynchronous processing.
- Set task-appropriate output limits and structured response formats.
- Re-run quality evaluations after every material optimization.
- Review savings, latency, and error patterns monthly so an old rule does not become a new source of waste.
The most durable savings come from designing the workflow so that each request contains only relevant information, uses the right level of intelligence, and runs at the right time. That approach reduces spend while making AI systems easier to observe, improve, and scale.