Build a request-level cost model
Start with traces that explain where spend comes from. For each request, capture:- workflow and intent;
- selected model and provider;
- input, cached, and output token usage;
- retrieval and reranking calls;
- tool calls and retries;
- latency by workflow step;
- outcome, fallback, and escalation;
- tenant or product boundary where appropriate.
Remove avoidable work before changing models
The safest savings often come from application design:- Stop duplicate calls. Give retries an explicit policy and make tool operations idempotent.
- Reduce irrelevant context. Filter by metadata, retrieve fewer but better candidates, and rerank before generation.
- Reuse stable computation. Cache repeated retrieval, deterministic tool results, and supported prompt prefixes where freshness rules allow it.
- Move deterministic logic out of the model. Validation, formatting, access checks, and simple routing often belong in code.
- Shorten outputs by contract. Ask for the information the product needs, not an unrestricted essay.
Route by task requirements
Model routing is useful when task families have different reasoning, context, latency, or policy needs. A deterministic classifier or explicit workflow state is often easier to test than asking a model to select another model. A routing policy can consider:- task complexity and required tools;
- context size and modality;
- evidence quality;
- response latency objective;
- data residency and provider policy;
- fallback history;
- quality measured on the same task segment.
Treat context as a budget
RAG systems can accumulate context silently. More chunks do not guarantee a better answer and often increase cost, latency, and distraction. I give each workflow an explicit context budget and preserve the evidence most likely to affect the answer. Useful controls include query rewriting, metadata filters, hybrid retrieval, deduplication, reranking, compact source representations, and separate context for policy instructions versus retrieved evidence. Long conversation histories should be summarized or converted into structured state instead of being replayed indefinitely. The budget must remain observable. Track retrieved tokens, tokens actually cited, cache behavior, and failure categories. This shows whether context reduction improves efficiency or simply removes necessary evidence.Protect quality while optimizing
Each optimization should be evaluated against the same representative task set as the current version. Compare:- task completion and groundedness;
- citation and tool-call correctness;
- fallback and escalation behavior;
- latency distribution;
- cost per successful task;
- performance on critical segments, not only the average.
Add budgets and ownership
Cost guardrails work best at three levels:- request: prevent runaway loops, excessive context, and unbounded output;
- tenant or product: detect unexpected consumption and protect shared capacity;
- portfolio: compare spend with adoption, completion, and business value.