Cost engineering
Model Routing and Cost Controls for AI Workflows
Optimize cost per accepted outcome, not price per token. Route stable tasks to deterministic logic or the least expensive model that passes, escalate difficult cases from measured signals, cache only reusable context, batch delay-tolerant work, cap retries and agent steps, and include tools, infrastructure, review, correction, and failure.
How this guide was produced
Provider-neutral routing and cost-control model based on official current pricing and caching documentation. No provider prices are copied as durable facts and no hands-on model benchmark is claimed.
AI assisted with research organization and editing. It is not treated as a source. Product capabilities and prices can change; verify the linked primary sources before making a purchase or production decision.
Begin with accepted outcomes
A cheap call that fails and triggers two retries, a frontier fallback, and ten minutes of correction can cost more than a stronger first attempt. Define the accepted unit—correctly classified ticket, grounded answer, approved report, completed workflow—and validate it outside the model. Track initiated, completed, accepted, corrected, rejected, abandoned, and failed outcomes separately.
Create a representative evaluation set by task type and risk. For each candidate route, measure acceptance, critical errors, latency, input and output usage, tools, retries, fallbacks, and review. A routing policy without task-level evidence is only a cost hypothesis.
Use a routing ladder
| Tier | Typical job | Admission rule |
|---|---|---|
| Deterministic | Rules, lookup, parser, schema validation | No model when the answer is already encoded and testable |
| Small model | Classification, extraction, rewrite, simple structured generation | Use only after task-level acceptance meets threshold |
| General model | Ambiguous synthesis, longer reasoning, difficult formatting | Default bounded generative route |
| Specialist or frontier | Hard cases proven to benefit in evaluation | Escalate by task evidence, not self-reported confidence alone |
| Human review | Consequential, unresolved, policy-bound, or exceptional work | Measure labor and correction as part of outcome cost |
Remove unnecessary model calls first. Exact lookup, validation, policy, math, and stable routing rules often belong in ordinary code. Use models for ambiguity and language, then validate structure and constraints deterministically.
Promote a task to a lower-cost tier only when it passes the same acceptance bar on a protected test set. Route by known task features, policy, context length, language, modality, latency class, and evaluated difficulty. Model-reported confidence can be an input, but calibrate it; confident mistakes are common.
Calculate the full cost
| Layer | Components |
|---|---|
| Model | uncached input × rate + cached input × rate + output × rate |
| Tools | search + browser + code + retrieval + external API charges |
| Execution | workflow compute + storage + queues + observability + network |
| Recovery | retries + fallback calls + duplicated or compensated actions |
| Human | review + correction + escalation + incident handling |
| Outcome | total period cost ÷ accepted validated outcomes |
Read current provider pricing at calculation time and store a dated rate table separately from the article or routing logic. Prices vary by model, context, modality, region, service tier, batch, cache, tool, and contract. Record actual usage returned by APIs and reconcile it with billing exports.
Allocate fixed costs such as observability, evaluation, platform engineering, and reserved capacity. Run low, expected, and high scenarios for volume, acceptance, output length, fallback, review, and price. Publish assumptions beside any savings estimate.
Design escalation from measured failure
Simple-to-strong routing can save money when early attempts are inexpensive and failures are detected reliably. It wastes money when the first response cannot be judged until a human reviews it or when failure contaminates later context. Compare direct strong routing with cascades on the same cases.
Useful escalation triggers include deterministic validation failure, missing evidence, unsupported tool request, policy-required review, repeated disagreement, task category, or a calibrated score. Cap the number of tiers. Preserve the original request and structured failure reason without carrying every failed response into the next prompt.
Control retries and agent loops
Classify errors before retrying. Transient rate limits and network failures may retry with backoff. Invalid arguments need repair. Policy denial should not retry. External writes require idempotency. Model-quality failure may need a different route, but repeated self-reflection can multiply tokens without new evidence.
Set per-run limits for attempts, model calls, input, output, tool calls, elapsed time, and spend. Set period-level limits by customer, workflow, and environment. When a limit is reached, stop with resumable state and an explicit reason rather than returning an invented answer.
Use caching when reuse is real
Prompt or context caching can reduce repeated-input cost and latency, but storage and eligibility rules vary. Measure prefix reuse, cache hit rate, cached-token usage, storage duration, invalidation, and total savings. A frequently changing context or low request volume may not justify explicit caching.
Keep stable instructions and reference context in a consistent prefix when supported. Do not cache secrets longer than their purpose or allow one user’s data to cross authorization boundaries. Version cached content and invalidate it after policy, source, prompt, tool, or permission changes.
Batch delay-tolerant work
Evaluation, offline enrichment, indexing support, and scheduled analysis may use discounted batch or flexible service tiers. Check completion window, limits, error handling, data requirements, and model support. Batch changes latency and recovery behavior; it is not appropriate for interactive or urgent actions.
Compare end-to-end operational cost, including queueing, result ingestion, partial failure, and reruns. Group compatible work without creating oversized failure domains or mixing authorization contexts.
Reduce context before reducing quality
Instrument input by source: system instructions, history, retrieval, tool schemas, attachments, and prior outputs. Remove duplicated policy, stale conversation, irrelevant retrieval, verbose tool output, and fields the task does not use. Summaries must preserve decisions, identifiers, approvals, and unresolved facts with provenance.
Optimize retrieval for relevance and evidence coverage rather than inserting more chunks. Use typed state instead of replaying an entire transcript. Shorter context can improve both cost and focus, but validate on long-history and edge cases.
Protect quality and safety gates
Do not route sensitive or consequential work to a model that fails authority, privacy, or critical-error thresholds even if average quality is adequate. Policy determines eligible routes before cost optimization. Validate output and proposed actions after routing; a model tier does not grant permission.
Monitor acceptance, correction, fallback, critical errors, latency, cost, cache hit rate, and routing distribution by task and segment. Alert when a cheaper route grows without an evaluation change, a fallback spikes, or cost per accepted outcome moves beyond the declared range.
Run a controlled rollout
- Baseline current quality and total cost by task.
- Introduce routing in shadow mode and compare chosen routes with references.
- Enable one low-risk task class with strict limits and fallback.
- Review failures and actual billing before expanding.
- Change one dimension at a time and preserve rollback.
- Re-evaluate after model, prompt, price, tool, policy, or workload changes.
Routing decision log
Log task class, eligible routes, selected route, rule version, measurable selection inputs, policy result, model and tool usage, retries, fallback, final validator, acceptance, review, latency, and cost. Do not store sensitive prompt content when structured fields are sufficient. The log should explain cost changes without exposing customer data.
Review route confusion, not only spend. Investigate cases where the cheaper route failed but did not escalate, the strong route handled routine work, fallback repeated the same failure, or a policy-ineligible model was considered. Feed corrected cases into the protected evaluation set and re-estimate the expected cost distribution.
Retain a provider-outage and quota-exhaustion route. Decide which tasks wait, use an evaluated alternative, become read-only, or return to humans. Never relax data or quality policy automatically because the preferred endpoint is unavailable. Test the degraded path and include its standby or switching cost in planning.
Version route names by capability rather than vendor marketing labels. A route contract should state modalities, context, tools, data eligibility, quality evidence, latency class, and cost source. This allows provider or model changes to trigger the right tests without rewriting every downstream workflow.
Limitations
Provider prices, model behavior, caching, batch features, rate limits, and regional availability change. Verify linked official pages and account terms. Cost measurements from one workload do not transfer directly to another, and model evaluations cannot cover every future input.
Routing adds complexity, failure paths, and observability requirements. At low volume, engineering cost may exceed token savings. High-impact work may require a consistently strong route or human review even when a cascade appears cheaper.
Related guides
Build the base formula with the automation cost model, compare deployment through local vs cloud AI, test quality with the agent evaluation framework, and instrument usage using the observability guide.
Primary sources
Sources were checked on . Follow the links for current product details.
- OpenAI API pricing
Current model, token, cached-input, batch, built-in tool, and service pricing dimensions.
- OpenAI prompt caching
Caching mechanism, cached-token usage reporting, latency, and cost considerations.
- Gemini Developer API pricing
Current standard, batch, cache, tool, and agent billing dimensions.
- Gemini context caching
Cache token, storage duration, suitability, usage metadata, and tradeoffs.
- Amazon Bedrock pricing
Model, on-demand, batch, cache, provisioned, custom, regional, and tool pricing dimensions.
- NIST AI Risk Management Framework
Lifecycle measurement, risk ownership, monitoring, and change management.