AIF-42Required
Price every model call and cap the spend
Compute the cost of every model call from its token counts and record it with the feature and user. Enforce a daily budget per user and per feature.
How to do it
Keep one price table per model version in the gateway, and multiply it by the uncached input, cache write, cache read and output token counts. Providers count these differently. Anthropic's input_tokens excludes cached tokens, while OpenAI's input_tokens includes them, so normalise usage in the gateway first. Store the cost on the call's log line. Before each call, check the user's and the feature's spend for the day, and refuse with a clear message at the cap. Alert the owner at 80 percent of a feature's daily budget. Caps on what outside agents spend through your product are in Cap what each agent grant may spend. Caps on coding agents are in Cap what your coding agents can spend. Caps per answer and per task are in limit cost, steps and loops. The monthly bill is watched as in watch what the service costs.
Examples
TypeScript example
const PRICE_PER_MILLION_USD = { 'example-model-2026-05-14': { input: 0.4, cacheWrite: 0.5, cachedInput: 0.04, output: 1.6 } } as const;
export function callCostUsd(model: keyof typeof PRICE_PER_MILLION_USD, usage: Usage): number {
const price = PRICE_PER_MILLION_USD[model];
return (usage.uncachedInputTokens * price.input + usage.cacheWriteTokens * price.cacheWrite
+ usage.cachedInputTokens * price.cachedInput + usage.outputTokens * price.output) / 1_000_000;
}
Python example
PRICE_PER_MILLION_USD = {"example-model-2026-05-14": {"input": 0.4, "cache_write": 0.5, "cached_input": 0.04, "output": 1.6}}
def call_cost_usd(model: str, usage: Usage) -> float:
price = PRICE_PER_MILLION_USD[model]
return (usage.uncached_input_tokens * price["input"] + usage.cache_write_tokens * price["cache_write"]
+ usage.cached_input_tokens * price["cached_input"]
+ usage.output_tokens * price["output"]) / 1_000_000
How to check
A test sets a test user's budget to one call and makes two. The second is refused without a provider call. The same test with the feature's budget set to one call gives the same refusal. The cost per feature per day appears on the dashboard.
Sources
Related rules
- AID-62 Cap what your coding agents can spend in AI-assisted development
- AIF-40 Put the stable part of the prompt first in AI features
- AGT-25 Cap what each agent grant may spend in Agents as users
- AIF-71 Stop every agent run at fixed limits set in code in AI features
- INF-53 Give every account a budget with alerts in Infrastructure and environments
- AIF-63 Route each kind of request to the model it needs in AI features
- AIF-80 Price each image or video job before it starts in AI features
- STK-110 Use LiteLLM as the gateway when you call several model providers in Software choices
- SCL-45 Queue model calls within the provider's rate limits in Scaling
- USG-11 Charge each model call's cost to the customer who caused it in Usage-based billing
- PRC-7 Let customers cap their spend in Pricing, plans and entitlements
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/latency-cost-and-fallbacks/cost-per-call">Agent Wranglers AI features playbook</a>. In code you write, add a comment next to it with the same link.