Controlling LLM costs: token budgets, caching and cheaper routing

How to keep an AI product's bill predictable: per-request context caps, embedding and response caches, routing simple requests to small models, fallback spend limits, and the dashboard that shows cost per feature.

Pitching at CSJMU
Pitching at CSJMU

The first month of a client AI feature is when you learn whether you designed for cost or for the demo. A user pastes a 300-page manual and asks forty questions; a bug retries a failed call in a loop overnight. These are the controls I put in every product now, and the ones I walk through in shipping workshops.

Cap the context per request

Retrieval returns the top five reranked chunks, not everything that matched. Long documents are summarised hierarchically rather than stuffed. A hard token ceiling per request means the worst case is known before the first user arrives.

Cache what repeats

  • Embeddings by content hash — re-uploaded documents cost nothing.
  • Retrieval results by normalised query for a short window.
  • Full responses for identical questions on identical context, where the product allows.

Route by difficulty

A small or fine-tuned model classifies and handles the frequent, simple requests; the expensive model handles the hard ones it flags. The pattern in small language models after fine-tuning is as much a cost control as a quality one.

Limit what fallback can spend

Multi-provider fallback keeps you up during an outage, and it can also route every request to your most expensive provider for six hours. Cap fallback spend per hour and alert when the cap is hit; see multi-provider LLM fallback.

Kill the loops

  • Retries with a maximum count and exponential backoff.
  • Idempotency keys so a client retry does not double a call.
  • A per-user and per-key rate limit, even for internal tools.

See it

Log tokens and model per request with a feature tag, and build the simplest dashboard: cost per feature per day. The feature that costs the most is rarely the one you expected, and you cannot fix what you cannot see. Students who add this to a portfolio project have a talking point most candidates lack.

CostLLM appsArchitectureOperations
Pranjul Rathour
Pranjul Rathour
GenAI Engineer · Kanpur, Uttar Pradesh, India

GenAI engineer and AI product builder with 2+ years shipping production-grade AI systems: RAG pipelines, fine-tuned LLMs, hybrid retrieval and multi-modal apps across vision, speech and OCR, architected end to end from ingestion to deployment. Leads engineering for SCULT INDIA's 14-member team, founded the 500+ member TechVerse Enclave community and has mentored 200+ students. Three hackathon first prizes: Changethon 2025 (IIT Roorkee), Product Genesis at Vividhotsava 2025 (CSJMU Kanpur) and BYTEBATTLE (MeetKats).

Open to GenAI roles, hackathon judging, mentorship sessions and guest talks at colleges. Email pranjulrathour41@gmail.com.

Keep reading

More on Shipping AI

All Shipping AI posts →
Campus talks · hackathon judging · mentoring

Want this as a live session at your college?

Open to GenAI roles, hackathon judging, mentorship sessions and guest talks at colleges.

Pranjul Rathour, GenAI engineer in Kanpur