AIF-29Recommended
Measure cost and speed in every eval run
Record tokens, cost and latency for each case in every eval run, and fail the run when they pass the limits in the feature's spec.
How to do it
Read token counts from the provider's usage fields and price them from one price table in the gateway. Report the median and 95th percentile latency and the average cost per case. A prompt change that adds a long context can keep the score and double the bill. This check catches it before release. In production, watch what the service costs.
Examples
TypeScript example
const p95 = percentile(results.map(r => r.latencyMs), 95);
const avgCost = results.reduce((sum, r) => sum + r.costUsd, 0) / results.length;
if (p95 > spec.p95LatencyMs || avgCost > spec.maxCostPerRequestUsd) process.exitCode = 1;
Python example
p95 = statistics.quantiles([r.latency_ms for r in results], n=100)[94]
avg_cost = sum(r.cost_usd for r in results) / len(results)
if p95 > spec["p95_latency_ms"] or avg_cost > spec["max_cost_per_request_usd"]:
sys.exit(1)
How to check
The eval report shows cost and latency per case. On a branch, set the spec's cost limit to half the last measured average cost, and the eval job fails on cost.
Sources
Related rules
- INF-53 Give every account a budget with alerts in Infrastructure and environments
- AIF-84 Gate releases on tool choice scores in AI features
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/ai-features/evals-and-quality/eval-cost-and-latency">Agent Wranglers AI features playbook</a>. In code you write, add a comment next to it with the same link.