AI API Spend Anomaly Runbooks: Detect Retry Storms, Agent Loops, and Model Drift Before the Invoice
A practical runbook for AI API cost control: detect abnormal burn rate early, attribute spikes to tenants, keys, users, models, and workflows, then apply reversible circuit breakers before provider invoices catch up.
Monthly budgets are too slow for many AI API incidents. A retry storm can multiply traffic in minutes. An agent loop can call tools until a queue is empty or a wallet is not. A model routing typo can quietly move routine traffic from a low-cost model profile to a premium one. By the time a provider dashboard, billing export, or invoice makes the spike obvious, the incident may already be expensive.
The practical answer is to treat AI spend spikes like production incidents. That means real-time gateway estimates, attribution joins, alert thresholds, scoped circuit breakers, human approval paths, and later reconciliation against provider-settled cost. This article lays out a runbook for teams that route AI traffic through multiple providers and need faster AI API cost control than monthly spend limits alone can provide.
The incident model: spend velocity, not just spend total
A monthly budget answers, “Have we crossed a line?” A burn-rate detector answers, “Are we spending abnormally fast right now?” For AI workloads, the second question is often more useful during an incident.
Fact: major cloud and AI providers expose usage, cost, billing, or anomaly reporting mechanisms, but the available dimensions, latency, and account requirements differ. For example, OpenAI documents usage and cost endpoints with grouping fields such as project, user, API key, model, batch, and service tier. Anthropic documents a Usage and Cost Admin API with dimensions such as model, workspace, service tier, API key, context window, and speed, with account limitations. Google Cloud documents billing anomaly management, budgets, alerts, and BigQuery billing export for analysis.
Recommendation: use provider reports for reconciliation and finance workflows, but use gateway-side estimates for early incident detection. The gateway sees requests as they happen, before provider cost exports are fully settled.
Prediction: as agentic systems and multi-provider routing become more common, cost incidents will increasingly resemble reliability incidents: sudden amplification, cascading retries, route misconfiguration, and tenant-specific abuse rather than simple organic growth.
Five common AI spend incidents
1. Retry storm after 429 or 5xx responses
A provider starts returning rate-limit or server errors. Clients, workers, SDKs, and gateway fallback logic all retry. Without a single retry budget, one user request can become many provider calls. If fallback routes use more expensive models, the cost spike can be larger than the traffic spike.
High-signal indicators include retry count per accepted request, provider error rate, fallback count, duplicate idempotency keys, and a rising ratio of upstream calls to end-user requests.
2. Infinite agent or tool loop
An agent keeps asking for tool calls because the tool result is ambiguous, invalid, or never reaches a terminal condition. The model may alternate between planning, tool invocation, and self-correction. Even if each call is valid, the workflow is not.
Watch tool-call count per workflow, repeated tool names with similar arguments, repeated response schemas failing validation, and a growing number of model calls under one trace or conversation ID.
3. Accidental premium-model routing
A model alias changes. A default route profile is edited. A model ID is mistyped and resolves to a premium fallback. A migration temporarily sends all traffic to the evaluation model instead of the production model. This can look like normal traffic volume with abnormal unit cost.
Detect it with model mix shift, cost per request, cost per successful workflow, and premium-model share by tenant, project, or prompt template.
4. Prompt-cache hit-rate collapse
Prompt caching depends on stable prefixes and compatible request construction. A release that adds timestamps, random request IDs, tenant-specific text, or dynamic instructions to the cached region can turn discounted cached-token traffic into full-price input-token traffic.
Indicators include cached-token share, cache hit rate by prompt template, input-token cost per request, and sudden divergence between prompt length and effective billed cost.
5. Tenant, user, or API-key compromise
A leaked key, compromised tenant account, or abusive end user can create a spend spike that is isolated to one identity. The right response is usually not to disable every AI feature for every customer. You need scoped attribution and scoped containment.
Useful signals include new geography or network origin, unusual model selection, sudden volume from one key, tenant share-of-wallet spike, repeated safety failures, and requests outside normal product workflows.
Build the gateway event needed for attribution
Cost anomaly response fails when telemetry is too shallow. “The bill went up” is not enough. The gateway should emit one normalized event per model call and join it to workflow context.
A practical event schema includes:
timestamptenant_idproject_idor workspaceend_user_id_hash, not a raw personal identifierapi_key_idrequest_idandidempotency_keytrace_id,conversation_id, or workflow run IDproviderandmodel_idroute_profile, such as standard, premium, fallback, batch, or evaluationprompt_template_idand prompt versioninput_tokens,output_tokens,cached_tokens, and reasoning-token fields where availableestimated_costat request timesettled_costwhen reconciled laterlatency_ms,status, and provider error classretry_countandfallback_counttool_call_countand tool names or tool categories
Recommendation: store enough metadata to debug cost without storing raw prompts by default. Prompt template IDs, token counts, route profiles, and pseudonymous user identifiers often provide strong operational visibility without retaining sensitive content.
Define detectors that catch abnormal burn
Start with a small set of high-signal detectors. Too many dimensions create alert fatigue, especially for teams with frequent launches, migrations, or customer onboarding events.
Cost burn rate
Compare current estimated spend per minute or per hour against a trailing baseline for the same tenant, project, model, or route profile.
current_15m_cost > max(absolute_floor, trailing_7d_same_window_avg * multiplier)
Use an absolute floor to avoid noisy alerts for tiny tenants. Use a multiplier to adapt to each tenant’s normal size. For example, a small tenant jumping from almost nothing to a few dollars may need notification only, while a large tenant doubling hourly burn may deserve immediate investigation.
Retry amplification ratio
Measure upstream provider calls per accepted end-user request.
retry_amplification = provider_attempts / accepted_user_requests
If this rises while success rate falls, suspect retries or fallback cascades. Pair this detector with provider status, rate-limit headers, and client idempotency keys.
Output-token expansion ratio
Measure output tokens relative to input tokens or expected workflow output size.
output_expansion = output_tokens / max(input_tokens, 1)
A spike can indicate missing max-token caps, a prompt regression, a loop producing verbose intermediate reasoning, or a structured-output failure that causes repeated regeneration.
Premium-model share shift
Track what percentage of traffic or cost is routed to premium models by tenant, application, or prompt template.
premium_cost_share = premium_model_estimated_cost / total_estimated_cost
This detector catches model alias changes, route profile mistakes, and unexpected fallback behavior even when request volume is normal.
Cache-miss delta
Track cached tokens as a share of eligible input tokens. Alert when the hit rate falls sharply for a template or route profile that normally benefits from caching.
cache_hit_delta = trailing_hit_rate - current_hit_rate
Do not alert on cache misses for templates that were never cacheable. Tag cache-eligible workflows explicitly.
Tool-loop count
Cap and alert on model calls, tool calls, or validation retries inside one workflow run.
if tool_call_count > policy.max_tool_calls_per_run: trigger_loop_guard
This is one of the most effective controls for agent workloads because the unit of failure is the workflow, not a single model call.
Use a response ladder instead of one big kill switch
The goal is to stop abnormal spend while preserving as much legitimate functionality as possible. A response ladder gives operators and automation several reversible options.
Level 1: Notify with context
Send an alert to the responsible team with the tenant, project, key, model, route profile, prompt template, current burn rate, baseline, top workflows, and recommended action. Chat or Telegram-style alerts are useful when they include buttons or commands for acknowledgement, temporary policy changes, and escalation.
Level 2: Require approval for expensive routes
If the anomaly is tied to premium models or high-output workflows, require human approval before dispatching new requests on that route. Keep low-cost or cached features available.
Level 3: Downgrade route profile
Move affected traffic from premium to standard models where quality requirements allow it. Make this a named policy change with an expiration time, not an undocumented configuration edit.
Level 4: Cap output tokens or disable tools
For loops and verbose generations, reduce max output tokens, cap tool calls, disable high-risk tools, or block recursive tool invocation. This often preserves read-only assistant features while stopping runaway workflows.
Level 5: Throttle tenant, key, user, or workflow
Apply rate limits to the narrowest reliable identity. If one API key is compromised, throttle or suspend that key. If one pseudonymous end user is looping an agent, contain that user. If a tenant integration is malfunctioning, throttle the tenant but keep other tenants unaffected.
Level 6: Defer non-urgent work to batch
For backfills, summarization jobs, migrations, and offline enrichment, push work into a batch queue with explicit budget checks. This prevents urgent interactive traffic from competing with runaway background jobs.
Level 7: Quarantine key or tenant
Use quarantine when there is likely compromise, abuse, or severe runaway automation. Quarantine should be auditable, reversible, and paired with notification to the owner or support team.
Separate benign growth from incidents
Not every spike is bad. A customer launch, product migration, marketing campaign, or planned batch backfill can look anomalous. The runbook needs ways to reduce false positives without ignoring real failures.
- Maintenance windows: allow teams to register planned migrations or load tests.
- Tenant-specific baselines: compare tenants to their own history, not only global averages.
- Workflow tags: distinguish interactive production traffic from batch jobs, evaluations, and experiments.
- Policy allowlists: allow approved temporary increases with expiration times.
- Multi-signal alerts: page humans when cost burn rises with another failure signal, such as retries, cache misses, or model mix shift.
Trade-off: aggressive automation reduces financial exposure but can block legitimate growth. Conservative automation avoids false positives but may allow larger incidents. Most teams should automate low-risk actions first, such as notifications, max-token caps, batch deferral, and approval gates, then reserve quarantine for high-confidence signals.
Reconcile after the incident
Gateway estimates are designed for speed. Provider-settled costs are designed for billing. They can differ because of discounts, cached-token pricing, batch pricing, service tiers, credits, minimums, currency handling, invoice line-item rules, or delayed reporting.
After containment, reconcile the incident window:
- Export gateway events for the affected time range.
- Group by tenant, project, API key, model, provider, and workflow.
- Pull provider usage or cost reports where available.
- Compare estimated cost with settled or invoice-aligned cost.
- Document known differences, such as cache discounts or batch treatment.
- Adjust tenant invoices, internal chargebacks, or credits if needed.
- Update detectors and policies based on what actually happened.
Recommendation: do not wait for perfect reconciliation before containment. Use estimates to stop the bleeding, then use provider reports to close the books.
Implementation checklist
- Define normal: create baselines by tenant, project, model, route profile, and workflow type.
- Tag every request: require tenant ID, key ID, route profile, prompt template ID, and workflow or trace ID.
- Estimate cost before and after dispatch: quote before sending, then update with actual token usage when the response completes.
- Track amplification: record retries, fallbacks, tool calls, validation retries, and provider attempts.
- Create a small detector set: start with burn rate, retry amplification, premium-model share, cache-hit collapse, and tool-loop count.
- Map detectors to actions: each alert should recommend notify, approve, downgrade, cap, throttle, batch, or quarantine.
- Scope controls narrowly: prefer user, key, tenant, workflow, or route-specific controls over global shutdowns.
- Add human overrides: support temporary approvals with owner, reason, expiration, and audit trail.
- Test synthetic incidents: simulate retry storms, cache regressions, model alias mistakes, and agent loops before they happen in production.
- Run postmortems: document timeline, detection gap, containment action, cost impact, reconciliation result, and policy changes.
Actionable conclusion
The fastest way to improve AI API cost control is not another monthly budget email. It is an incident runbook that watches spend velocity, attributes abnormal usage to the right tenant, key, user, model, and workflow, and applies reversible controls before the invoice arrives.
Start with five detectors: cost burn rate, retry amplification, premium-model share, cache-hit collapse, and tool-loop count. Add a response ladder that begins with contextual alerts and ends with scoped quarantine. Keep provider cost APIs and billing exports in the loop for reconciliation, but do not depend on them for minute-by-minute containment. The operational standard is simple: every expensive spike should be detected early, explainable by dimensions you already log, and controllable without taking down all AI features.