I'm Gopi Gugan, a software engineer building AI features with FastAPI, Postgres/pgvector, and React/TypeScript. You can find more of my work at gopigugan.ca. LLM bills have a way of surprising you. A feature that costs pennies per user in testing can quietly become your biggest infrastructure line item once real traffic arrives. I've learned to treat cost as a design constraint from day one, not a surprise to fix later. Here's how I keep it under control.

Know what you're actually paying for

Most LLM cost isn't the model call you think about. It's everything around it.

  • Input tokens usually dominate the bill. Long system prompts, big RAG contexts, and full conversation histories add up fast — and you pay for them on every single call.
  • I log tokens in and tokens out for every request, with the feature and user attached, so I can see which feature is actually expensive instead of guessing.
  • I set a per-request budget in code, not just in my head. If a prompt would exceed it, I trim context before calling the model, not after the invoice arrives.
  • I review the bill weekly, not monthly. A 2x jump caught in week one is a fix; caught in month two, it's a postmortem.

Shrink the prompt before you shrink the model

Downgrading the model is the obvious lever, but trimming the prompt is usually cheaper and safer.

  • In my RAG app, I cap retrieved chunks at the top few that actually score well, instead of stuffing the context window "just in case." Fewer tokens, and often better answers.
  • I keep system prompts tight. A 200-token instruction that does the job beats a 1,000-token one nobody reads — including the model.
  • I summarize conversation history instead of replaying it. After a few turns, I keep the last couple of messages verbatim and compress the rest into a short summary.
  • I strip what the model doesn't need: boilerplate, repeated headers, and metadata that came along for the ride from the database.

Route to the cheapest model that can do the job

Not every request needs your best model. Most don't.

  • I classify tasks by difficulty. Simple classification, extraction, and formatting go to a small, cheap model. Only the genuinely hard reasoning gets the flagship.
  • I start new features on the cheaper model and upgrade only when I can point to a real quality gap in my evals — not a vague feeling that bigger is better.
  • I cache aggressively where the answer is repeatable. Identical or near-identical queries — common in search and summarization — get a cached response from Postgres instead of a fresh call.
  • I set max_tokens on every call. An unbounded response is an unbounded bill, and most of my features only need a few hundred tokens anyway.
import hashlib, json def cached_llm_call(prompt: str, call_llm, ttl_seconds: int = 3600): key = hashlib.sha256(prompt.encode()).hexdigest() cached = db.get_cached_response(key, ttl_seconds) if cached is not None: return cached, {"source": "cache", "tokens": 0} result = call_llm(prompt, max_tokens=500) db.store_cached_response(key, result) return result, {"source": "llm", "tokens": result.usage.total_tokens} 

Guardrails that stop runaway spend

Good design helps, but a bug or an abusive user can still burn through budget in an afternoon. I put hard limits in place.

  • Per-user rate limits and daily spend caps, enforced in the API layer before the LLM is ever called.
  • A global kill switch: if spend crosses a threshold, the feature degrades to cached or template responses automatically. I would rather serve a simpler answer than an unexpected bill.
  • Alerts, not just dashboards. If spend jumps 50% over the previous day, I get notified — dashboards only help if someone looks at them.
  • I never let the model call itself in a loop. Agents and multi-step chains get a strict step budget, because an agent stuck in a retry loop is a meter running at full speed.

What I don't do

  • I don't optimize cost before I know the feature works. A cheap answer nobody wants is still a waste of money.
  • I don't cache answers where freshness matters, or where the answer depends on private user data that shouldn't be shared across users.
  • I don't pick a model on price alone. If the cheap model fails my evals on the hard cases, the savings are fake — I'll pay for it in support tickets and churn.
  • I don't hide cost cutting from users. If a feature falls back to a simpler answer to stay within budget, I say so. Honesty is cheaper than lost trust.

LLM cost control isn't about being cheap — it's about being deliberate. Measure what each request costs, trim the prompt, route to the right model, cache what's repeatable, and cap what can go wrong. The model is the exciting part of the system. The bill is the part that decides whether the feature survives.