AI Gateways & Model Routing: Optimise for Cost per Successful Task
The cheapest model on the price list is often the most expensive one in production. What you actually pay for is a successful task — and that includes retries, escalations, human clean-up and the failures nobody caught. An AI gateway is where you control that number.
Most teams start by calling one model directly from application code. It works — until there are five features, three providers, a finance team asking where the money went and an outage at a provider on a Friday night. At that point the model call needs the same treatment every other critical dependency gets: a single, governed entry point. That's an AI gateway.
The number that matters: cost per successful task
Per-token price tells you what one attempt costs. Users care about one result. The honest formula is:
cost per successful task = (model spend + retries + escalations + review effort) ÷ tasks that actually succeeded
A worked example with illustrative numbers: a small model costs $0.002 per attempt and solves 70% of a task on its own; a large model costs $0.02 and solves 95%. Sending everything to the large model costs $0.02 per attempt and leaves 5% of tasks failing. A cascade — try the small model, verify, escalate the 30% that fail — costs about $0.002 + 0.3 × $0.02 ≈ $0.008, with roughly the large model's success rate. Send everything to the small model without verification and the "cheap" option quietly ships 30% wrong answers, which someone pays for later.
What an AI gateway does
- One API for every model. Applications call the gateway; the gateway speaks to hosted APIs and self-hosted models, so swapping providers is a config change.
- Identity, quotas and budgets. Per-team and per-tenant keys, rate limits and spend caps, so one runaway feature can't drain the month's budget.
- Routing. Pick the model per request by task, complexity, latency target, price or measured quality.
- Caching. Exact-match caching for repeated requests, semantic caching for near-duplicates, and provider prefix caching for long shared system prompts.
- Guardrails. PII redaction on the way in, output checks on the way out, prompt-injection screening for untrusted content.
- Resilience. Timeouts, retries with backoff, circuit breakers and fallback to a second provider when the first one degrades.
- Observability. Tokens, latency, cache hits, errors and cost per feature, per tenant and per route — the data every other decision depends on.
Routing strategies, from simple to smart
- Static by task. Classification and extraction go to a small model; long-form reasoning goes to a large one. Easy, and often half the savings.
- Complexity-based. A cheap classifier (or simple heuristics: input length, number of documents, user tier) predicts how hard a request is and picks a tier.
- Cascades. Try the small model first, run a cheap check, escalate only when the check fails. This is where the biggest wins usually are.
- Quality-aware. Route using eval scores per model and task type, refreshed as models change — not on vibes or last quarter's benchmark.
- SLA- and budget-aware. Prefer the fastest healthy option for interactive paths; prefer the cheapest acceptable option for batch jobs; tighten when a budget is nearly spent.
# routing policy (illustrative)
routes:
- match: { task: classify } # cheap, high-volume
model: small-fast
- match: { task: answer, tier: free }
cascade:
- model: small-fast
verify: schema_valid && grounded_score >= 0.8
- model: large-reasoning # only the ~20–30% that fail verification
- match: { task: answer, tier: enterprise }
model: large-reasoning
fallback: [large-reasoning@provider-b]
limits:
per_tenant_monthly_usd: 2000
timeout_ms: 20000
cache:
exact: true
semantic: { threshold: 0.95, ttl: 1h }
Cascades only work if you can verify cheaply
Escalation needs a signal that the first answer is not good enough. Good signals are cheap and specific: the output parses against the expected schema, cited passages actually contain the claim, two samples agree, a small judge model scores it above a threshold, or a tool call succeeds. Without a check, a cascade is just "use the small model and hope".
Measure before and after every change
- Keep an eval set per route and re-run it whenever you change a model, prompt or threshold.
- Track cost per successful task, not just cost per request, broken down by feature and tenant.
- Roll routing changes out like code: shadow traffic first, then a canary, then everyone — and keep the old route one switch away.
Pitfalls to avoid
- Hidden retries. SDK-level retries plus gateway retries plus application retries can triple spend during an incident. Retry in one place, with a budget.
- Stale or unsafe caches. Semantic caching across tenants can leak data; scope cache keys per tenant and expire them.
- Prompt drift across models. A prompt tuned for one model can underperform on another; version prompts per model and evaluate each pairing.
- Routing on old data. Models are updated constantly. Re-evaluate on a schedule, not once.
Key takeaways
- Optimise cost per successful task, not price per token.
- An AI gateway centralises routing, caching, guardrails, resilience and cost visibility.
- Cascades (small model → verify → escalate) usually deliver the biggest savings — but need a cheap verification signal.
- Treat routing changes like releases: evals, shadow traffic, canaries and an easy rollback.