How to Reduce LLM API Costs in a Production App
If your LLM bill is growing faster than your user base, the problem is almost never "the model is too expensive." It's usually that you're sending more tokens than you need to, calling the wrong model for the task, or re-computing the same answer over and over. Reducing LLM API costs in a production app comes down to four levers: fewer tokens per request, the right model for the job, fewer redundant requests, and better visibility into where the spend is actually going.
Below is a practical breakdown of each lever, with concrete things you can implement this week — not theoretical advice about "optimizing prompts" without specifics.
1. Cut tokens before you cut anything else
Token count drives both latency and cost, and it's the lever most teams ignore the longest.
- Trim system prompts. Long, repetitive system prompts get sent on every single call. Audit yours — most can be cut by 30-50% without losing behavior. Move examples and edge-case instructions into a shorter, more direct style.
- Don't paste entire documents when you only need a section. If you're building RAG, retrieve the 2-3 most relevant chunks, not the whole document. Oversized context windows are a convenience, not a requirement.
- Cap output length explicitly. Set
max_tokensto the smallest value that covers your actual use case. A chatbot reply doesn't need a 4,000-token budget if your UI only shows 200 words. - Strip conversation history aggressively. Summarize older turns instead of replaying the full transcript on every request. A rolling summary + last 2-3 turns is usually enough for continuity.
A quick way to see the impact: log token counts per request type for a week before you optimize, then again after. Teams are often surprised that 20% of requests account for 70% of token spend — usually the ones with long documents or unbounded chat history.
2. Route requests to the right model tier
Not every request needs your most capable (and most expensive) model. Classification, extraction, formatting, and short Q&A tasks often work fine on a smaller/cheaper tier, while multi-step reasoning or code generation justifies the premium model.
A simple routing pattern:
function pickModel(task) {
if (task.type === "classification" || task.type === "extraction") {
return "claude-haiku";
}
if (task.type === "reasoning" || task.type === "code") {
return "claude-opus";
}
return "claude-sonnet"; // default middle tier
}
This single change often cuts 30-50% of spend because most production traffic is simple, repetitive tasks — not the hard reasoning problems the flagship model is priced for.
3. Eliminate redundant calls
Redundant requests are pure waste — you're paying twice for the same answer.
- Cache deterministic outputs. If the same input (or a normalized version of it) produces the same output, cache it. A Redis cache keyed on a hash of the prompt + parameters is enough for most apps.
- Debounce user-triggered calls. Autocomplete, live search, and "suggest as you type" features should debounce input and only call the API after the user pauses, not on every keystroke.
- Batch where the workflow allows it. If you're processing a queue of similar items (summarizing 50 support tickets, extracting fields from 200 invoices), batch them into fewer, larger calls instead of one call per item, as long as you stay within context limits.
- Short-circuit with cheap checks first. Use regex, a lookup table, or a lightweight classifier to filter out requests that don't need the LLM at all before they ever hit the API.
4. Get visibility into where money is going
You can't optimize what you can't measure. Most teams only find out they're overspending when the invoice arrives — by then it's a month too late.
At minimum, track per request:
- Model used
- Input and output token counts
- Which feature/endpoint triggered the call
- Latency
This lets you answer questions like "which feature is actually driving 60% of our spend" instead of guessing. If you're calling Claude directly, this means wiring up your own logging around every API call, which is easy to skip under deadline pressure and easy to regret later.
This is one of the reasons we built SubToAPI — it sits between your app and Claude, turning your existing access into a standard HTTPS API with per-key usage metadata already attached. Every request you make through it includes token and cost breakdowns, so you can see which application key, team, or feature is burning budget without building your own metering layer. You get an API key (sub_live_...), swap it into your existing request code, and the dashboard gives you the per-request and per-team numbers you'd otherwise have to log and aggregate yourself. See the quickstart for the five-minute setup, or check pricing — plans start at €9/month for solo use and scale to team seats if you need shared visibility across a larger product.
5. Watch for architecture-level waste
Beyond prompts and models, a few structural patterns quietly inflate bills:
- Retrying on every error, including non-retryable ones. Make sure retry logic distinguishes rate limits (retry with backoff) from bad requests (don't retry, fix the input).
- Streaming where you don't need it, or not streaming where you do. Streaming doesn't reduce token cost, but it reduces perceived latency, which lets you use smaller models without users noticing a slowdown. See streaming for how this works in practice.
- Tool calls that re-send full context every turn. If you're using function calling or tools, check whether your framework is re-sending the entire conversation plus tool definitions on every step — this adds up fast in multi-step agent loops.
- No request-level timeouts or budgets. A single runaway loop (agent calling itself repeatedly) can burn through a day's budget in minutes if there's no hard cap on calls per session.
None of these fixes require switching providers or downgrading quality across the board — they require being deliberate about where tokens and model tiers go.
Questions
Does using a cheaper model always mean worse output quality? Not for every task. Smaller models handle classification, extraction, formatting, and short factual answers well. Reserve the most expensive tier for genuine multi-step reasoning or code generation, and test quality on your actual data before assuming you need the top model everywhere.
Is caching LLM responses safe for user-facing apps? Yes, as long as you cache based on normalized input and avoid caching anything with time-sensitive or personalized data baked into the prompt. Cache the deterministic parts (static Q&A, repeated lookups) and skip caching for anything that depends on live user context.
How do I know if my prompt is too long? Log token counts per request and look at the distribution. If your 90th-percentile request is 5x your median, you likely have unbounded context (full chat history, entire documents) that should be trimmed or summarized before the call.