Agent Wranglers

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

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.