Making My RAG App Cheaper Without Making It Wrong
Cost per question, a cache I turned off, and a model router that fixed one answer and broke another

My RAG assistant answers questions about cloud architecture with citations. The last post was about knowing when it's wrong. This one is about making it cheaper and faster, without making it wrong. Three of the five things I tried taught me more by failing than by working.
1. Cost per question, not the monthly bill
The number to manage is cost per question, split into two parts:
- Variable: tokens. On Amazon Nova Micro, my ~850 tokens in and ~100 out come to about $0.00004 a question. A thousand questions a day is roughly $1.20 a month.
- Fixed: the platform (load balancer, containers, database). In my dev setup that's $40–60 a month, about fifty times the tokens. At low volume, fixed costs dominate, so scaling the database to zero matters more than any token trick. Tokens dominate at high volume, with bigger models, or with agents that call the model several times per question.
I develop on local models, which cost $0, and that number says nothing about production. So my evaluation now prices the same tokens on the hosted model and reports projected cost per question. A prompt change that adds context shows up as a cost regression in review, before anyone deploys it.
2. The cache: one level I kept, one I turned off
Repeated questions shouldn't cost a model call. My cache has two levels, checked in order.
Exact: the question after normalising case, spacing and punctuation. A hit is one indexed database lookup: 27–56 ms instead of 6.7 seconds, zero tokens. It's always safe, as long as stale answers can't be served. Each entry is stored under a hash of everything that changes an answer: model, prompt version, retrieval settings, and a fingerprint of the indexed documents. Re-ingest a document or edit the prompt, and old entries simply stop matching. Namespacing is simpler than invalidation, and it can't miss a case.
Semantic: reuse the answer to a similar question, when the embeddings are close enough. This is where it got interesting. A semantic cache is a classifier: "is this the same question?" A wrong hit is a fluent, cited answer to a question the user didn't ask. So instead of picking a threshold by feel, I labelled pairs: rewordings that should share an answer, and near misses that must not.
Everyone worries about pairs like RTO vs RPO. On my embedding model those scored only 0.767. The real danger was polarity:
| Pair | Similarity | Same meaning? |
|---|---|---|
| "Is it safe to store secrets in environment variables?" / "…unsafe…" | 0.986 | No |
| "When should I use spot instances?" / "…not use…" | 0.967 | No |
| "Should I use reserved instances?" / "…avoid…" | 0.941 | No |
| "How can I reduce my AWS costs?" / "How do I lower my AWS bill?" | 0.897 | Yes |
Embeddings capture topic and sentence shape well, and polarity badly. A one-word change from "safe" to "unsafe" barely moves the vector, while a true rewording with different words moves it further.
With my first 20 pairs, a threshold of 0.97 looked safe: zero wrong hits. I added four more negation pairs, and the same threshold served the "unsafe" question the answer to "safe". Twenty test pairs said ship it; twenty-four said don't. I shipped the exact cache and turned semantic matching off, with the check kept for the next embedding model.
The lesson isn't "semantic caches are bad". It's that the threshold is a measurement, and the measurement is only as good as its near misses.
3. A reader found what my tests didn't
A reader commented on my last post: when a user closes the tab mid-answer, that shouldn't count against the error budget, and the disconnect must actually stop the model call, or you keep paying for tokens nobody reads.
Both were already handled. When I checked, both were weaker than I'd written:
- Disconnects were kept out of errors, but in a side counter. My outcomes didn't add up to all requests, and the SLO excluded cancellations by accident rather than by design. Now
cancelledis an outcome of its own, excluded explicitly, with a rule test. - My cancellation test used a fake generator. I deleted the cleanup line it was supposed to protect, and the test still passed. It proved my loop stopped, not that the model did. The new test runs the real model client against a small fake model server that streams like the real one and records what it sent. When the "browser" leaves after ten events, the server sees the disconnect and stops after under a quarter of the answer. With the cleanup line deleted, the test fails. What's still open is the provider's side: whether billing stops when the stream closes. That's a test per provider, not an assumption.
4. The router that fixed one answer and broke another
My small model answers 36 of 37 evaluation questions. The one it fails, it fails while holding the right section. A 7-billion-parameter model answers all 37, at about 1.9× the cost per question and 2.4× the time to first token.
Paying that on every question to fix one is the wrong trade, so I built a cascade: the small model first, and only if it refuses while retrieval found strong evidence, the same prompt goes to the larger model. A refusal alone isn't enough: an earlier experiment showed it also fires on questions that should be refused. I wrote the decision rule down before running anything, then measured three options:
| Pass | Fact recall | Cost / question | Total p95 | |
|---|---|---|---|---|
| Small model | 97% | 72% | $0.000041 | 8.1 s |
| Small model, section headings in the prompt | 97% | 72% | $0.000043 | 8.1 s |
| Cascade to the larger model | 97% | 73% | $0.000047 | 21.5 s |
| Larger model only | 100% | 80% | $0.000079 | 22.5 s |
The cascade escalated two questions. One was the case it was built for, and the larger model answered it correctly. The other was a question the documents only partly cover: they discuss the feature, but not the setup steps the user asked for. The small model's refusal was the right outcome. The larger model answered anyway, and named something no source contains.
One fix, one break, pass rate flat. And my metric, "escalations answered", read 100%, because it counted the break as a success. It's now "escalations passed".
The broken question actually had higher retrieval similarity (0.772) than the fixed one (0.742), so no threshold separates them. The trigger can't tell "the model failed to read" from "the documents are relevant but don't hold the answer". A bigger model fixes reading failures and creates willingness failures. Routing stays off for now; the real decision moves to the production models.
I also caught my own bug in that table: the large-model-only run was first priced as the small model, which made it look nearly as cheap. Every report now states which model it's priced as.
5. A spend cap that degrades instead of breaking
Cloud budgets report spend hours late and can only send an alert. An app needs its own cap, checked before every model call, in steps:
- Under 80% of the daily budget: normal.
- 80–100%: economy mode. Small model only, never cached, and a ticket for someone to look.
- At 100%: no model calls, a clear "limit reached" message, and a page. Cached answers still work, because they cost nothing. The spend lives in one database row per day with an atomic upsert, so every instance agrees (tested with 40 concurrent writes). Checking before and adding after can overshoot by the requests in flight, which at four-thousandths of a cent per answer is noise. A deliberate outage is still an outage, so "over budget" is its own outcome on the dashboard, separate from errors and refusals.
What I'd tell a team
- Ask for cost per question. Split fixed from variable; at low volume, fix the platform first.
- Treat thresholds as measurements. A semantic cache and a model router are both classifiers in disguise. Label the near misses, and expect more test cases to change the answer.
- Name metrics after outcomes, not actions. "Answered" rewarded the mistake; "passed" caught it.
- Read the cases when the total doesn't move. A flat pass rate hid one fix and one break.
- Delete the line a test protects. If the test still passes, it isn't protecting it.
- Publish your work. A reader found in one paragraph what my test suite didn't.
Comments
Join the discussionSplitting cost per question into variable token spend ($0.00004 on Nova Micro) versus fixed infrastructure ($40 to $60 a month) keeps low-volume teams from over-optimizing prompts while an always-on database dominates the bill. Pricing local evaluation runs against hosted production rates so context growth shows up as a cost regression in PR review is also a very practical habit.
Namespacing the exact cache under a composite hash of model, prompt version, retrieval settings, and corpus fingerprint eliminates stale-answer invalidation bugs cleanly. Treating a semantic cache as a binary classifier tested against labeled near-miss pairs—rather than guessing a cosine threshold—explains why turning it off is often safer than shipping a fluent wrong answer.
Thanks, glad the cost split was useful. It surprised me too: at low volume the always-on platform costs about fifty times the tokens.
Curious whether you've run a semantic cache in production. Did you find an embedding model, or a check like a reranker or exact-entity match, that handles negation well enough to keep it on?
AI Journey
Hands-on notes from building GenAI systems on AWS: RAG, agents, MCP, evaluation, observability and cost control, written as I go.





