Agent Wranglers

Latency, cost and fallbacks Required

Price every 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 per answer and per task are in limit cost, steps and loops. The monthly bill is watched as in watch what the service costs.

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;
}
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.

For agents: if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in Based on the Agent Wranglers ai features playbook: https://agentwranglers.se/playbook/ai-features/latency-cost-and-fallbacks/cost-per-call.