Esta nota todavía no está traducida, así que se muestra la fuente en inglés.
Measure and cut inference cost
Mental model: inference cost is a property of successful workflows, not requests. Context, output, retries, loops, routing, and cache misses are the numerator; verified success is the denominator.
Mechanism: trace → attribution → constrained optimization
Use this playbook when an AI feature works but spend is too high, unpredictable, or hard to attribute to users, workflows, prompts, models, or agents.
Inputs
- Per-request traces with model, tokens, latency, retries, tool calls, cache hits, and outcome.
- Cost by provider, model, endpoint, customer, task, and environment.
- Eval suite that can catch quality regressions.
Procedure
- Break cost down by task type, model, prompt version, customer, and success/failure.
- Separate fixed prompt tokens, retrieved-context tokens, output tokens, retries, and agent loops.
- Identify the top cost contributors with p50, p95, and worst-case requests.
- Remove unnecessary context before changing models.
- Add caching for repeated semantic requests or stable intermediate results.
- Route easy tasks to smaller or cheaper models.
- Cap retries, agent steps, output length, and retrieval fanout.
- Re-run evals after every cost change and compare quality, latency, and safety.
Cost levers
| Lever | Watch for |
|---|---|
| Shorter context | lost evidence or worse grounding |
| Smaller model | format or reasoning regressions |
| Semantic cache | stale or cross-user responses |
| Fewer retries | lower recovery rate |
| Agent step cap | incomplete tasks |
Pitfall
Cutting cost without evals often just hides quality loss. Measure cost per successful task, not only cost per request.
Connects to: cost optimization · pricing vs compute cost · semantic caching
Executable cost model
requests, successes, cost = 120, 90, 18.0
print("cost/request", cost/requests, "cost/success", cost/successes)
Run with python3; expected output makes retries visible. Re-run holdout quality, safety, and latency gates after every optimization.
Sources
- vLLM — serving throughput and memory management.
- FlashAttention — IO-aware attention efficiency.
- Greenhouse Gas Protocol Scope 2 Guidance — electricity-accounting context.