Cost, Latency and Caching: Making LLM Apps Faster and Cheaper Without Losing Quality

LLM Development 55 min min read Updated: Nov 21, 2026 Advanced
Cost, Latency and Caching: Making LLM Apps Faster and Cheaper Without Losing Quality
Advanced Topic 2 of 3

Where Cost and Time Come From

  • Input tokens: long prompts and documents
  • Output tokens: usually priced higher and also slower to produce one by one
  • Model size: larger models cost more and respond slower
  • Number of calls: agents and chains make many calls

A Cost Calculator

The prices are example numbers only. Replace them with your provider's current prices.

python def monthly_cost(requests, in_tokens, out_tokens, price_in, price_out): per_req = in_tokens * price_in / 1e6 + out_tokens * price_out / 1e6 return per_req, per_req * requests per, month = monthly_cost(100000, 1500, 200, price_in=1.0, price_out=4.0) print("per request:", round(per, 5), " per month:", round(month, 2)) per2, month2 = monthly_cost(100000, 700, 200, price_in=1.0, price_out=4.0) print("after cutting the prompt by half: per month", round(month2, 2))
Result

per request: 0.0023 per month: 230.0 | after cutting the prompt by half: per month 150.0

Savings Techniques

TechniqueHow it helps
Shorter promptsFewer input tokens. Remove repeated rules and extra context
Smaller model for easy tasksRoute simple requests to a cheaper model
Response cachingReuse answers to identical requests
Prompt caching (provider feature)Some providers discount repeated prompt prefixes. Check availability and rules
Limit outputSet max tokens and ask for concise answers
StreamingDoes not lower cost but lowers waiting time that users feel
Parallel callsRun independent calls at the same time

A Simple Cache

python import hashlib cache, calls = {}, {"model": 0} def key(model, prompt, temperature): return hashlib.sha256(f"{model}|{temperature}|{prompt}".encode()).hexdigest() def fake_llm(prompt): calls["model"] += 1 return "Answer to: " + prompt def cached_call(prompt, model="m1", temperature=0): k = key(model, prompt, temperature) if k not in cache: cache[k] = fake_llm(prompt) return cache[k] for q in ["What is RAG?", "What is RAG?", "What is MCP?", "What is RAG?"]: cached_call(q) print("requests: 4, model calls:", calls["model"])
Result

requests: 4, model calls: 2

Four requests needed only two model calls. Cache only where repeated identical answers are fine. Never share cached answers between users if they contain private data.

A Simple Router

python def route(question): hard_words = ["analyze", "compare", "prove", "plan", "why"] if len(question) > 300 or any(w in question.lower() for w in hard_words): return "large-model" return "small-model" for q in ["What are your opening hours?", "Compare these two contracts and explain the risks"]: print(route(q), "<-", q)
Result

small-model <- What are your opening hours? | large-model <- Compare these two contracts and explain the risks

Measure first Record tokens, cost and latency per request. Optimize the biggest cost first, and re-run your evaluation set after each change.
Do not cache blindly Caching answers that depend on time, user or private data can show wrong or leaked information.

Practice

  1. Change the numbers in the cost calculator to your expected traffic
  2. Add a time limit to the cache
  3. Write which of your requests are safe to cache

Conclusion

Measure, shorten, route and cache. These simple habits often cut cost and wait time a lot.

Quick Summary: Cost sources; calculator; techniques; cache; router.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators