Skip to main content
19

How do you reduce LLM cost and latency?

MediumCostLatencyCaching

How do you reduce the cost and latency of an LLM application?

TL;DR

First measure where tokens and time go (per feature, per request: input tokens, output tokens, time to first token, total time). Then use the biggest levers: pick the smallest model that passes your evals and route only hard requests to bigger models; cut tokens (shorter prompts, trimmed context, fewer retrieved chunks, shorter outputs); use prompt caching for long repeated prefixes; stream responses so users see output immediately; cache whole responses for repeated questions; and use batch APIs for work that doesn't need an instant answer.

Key points

  • Output tokens are both the slowest (generated one by one) and usually the most expensive, so ask for concise answers and set max_tokens.
  • Time to first token (TTFT) is what users feel; streaming plus a fast first step improve perceived latency.
  • Prompt caching: put stable content first (system prompt, tool definitions, documents) so the provider can reuse it; cached input tokens are much cheaper and faster.
  • Semantic caching: reuse answers for questions that are similar enough, carefully, since a wrong cache hit gives a wrong answer.
  • Parallelize independent LLM calls instead of chaining them.
  • Batch APIs often cost about half for jobs that can wait hours (evals, enrichment, classification backfills).
  • Self-hosting open models (vLLM, quantization, continuous batching) can pay off at high, steady volume, but adds operational cost.
  • Re-run evals after each optimization so quality doesn't silently drop.

Example

text
Before: large model, 6,000-token prompt, 800-token answer → 9 s, $$$
After:
  - router sends 80% of requests to a small model
  - system prompt + docs cached (prefix reuse)
  - retrieval trimmed from 10 chunks to 4
  - answers capped at ~250 tokens, streamed
→ TTFT 0.6 s, total 2.5 s, ~70% cheaper, eval score unchanged

Common follow-ups

  • How does model routing decide? A small classifier, simple rules (length, intent), or a cheap model's own confidence; send uncertain or complex cases to the big model.
  • What is speculative decoding? A small draft model proposes several tokens and the large model verifies them in one pass, speeding up generation without changing outputs.

What interviewers look for

Tip

Measure first, then concrete levers (model choice and routing, token cuts, caching, streaming, batching), with evals guarding quality.

  • Red flag: using the largest model for every request "to be safe".

Was this helpful?