Guide

AI agent cost spikes: finding the runs behind the bill

A cost spike is rarely spread evenly. It is usually a small number of runs, or one customer, or one feature, doing something expensive many times. The job is to find which.

RootlinePublished

Short answer

To explain an AI agent cost spike, narrow it down in order: when (which hours or days), which runs (the most expensive ones in that window), which step inside those runs (usually one generation with a huge input, or many repeated calls), and who (customer, feature or API key). The usual causes are agent loops, context that grows every step, retries after errors, a switch to a pricier model, and one heavy customer.

Usual causes

CauseWhat it looks like in a traceTypical fix
Agent loopThe same tool called again and again with the same arguments; cost grows per iterationRepeat guard and step limit; clearer tool results
Growing contextInput tokens rise on every generation in the run; full documents or history re-sent each stepTrim, summarize or cache earlier context; cap retrieved documents
RetriesSeveral near-identical generations after a failed tool call or a parse errorBackoff with a retry limit; fix the underlying error
Model changeSame token counts, higher cost per run after a deployCheck the model name per generation; route simple steps to a smaller model
One heavy customer or featureSpend concentrated on one user ID, tag or featurePer-customer limits or pricing; look at what that customer is doing
Double countingThe same model call recorded twice (framework span plus SDK span)Fix instrumentation, or count only one of them
Synthetic example · not customer data

A research assistant's daily model spend goes from about $40 to $170 overnight. Volume is flat. The spike starts a few hours after a deploy that added "include the full source documents for accuracy" to the summarization step.

Find the runs behind a spike

  1. Find the window. Plot cost over time. In Langfuse, the dashboard shows cost per model and the Pulse strip above the observations table can switch to cost and narrow the table to the spike when you click a bar.
  2. List the most expensive runs in that window. Sort by total cost. If the top runs are ten times the median, it is a few runs; if every run got a bit pricier, it is a change that affects all of them (model, prompt, context).
  3. Open the top runs and find the expensive step. Look at input and output tokens per generation. Rising input tokens on every step means context growth; many identical tool calls means a loop.
  4. Split by customer, feature or key. Use the user ID, tags or metadata you attach to traces. In the synthetic example every customer is affected, which points at the deploy.
  5. Check the cost numbers themselves. Missing model prices show as zero cost; duplicated generations show as double cost. Spot-check one run against your provider's price list.
  6. Fix, then add an alert. Here: send excerpts instead of full documents. Then alert on spend so the next spike is a notification, not an invoice.

Query expensive runs

Langfuse's Observations API v2 accepts a filter parameter with numeric conditions, so you can pull costly generations from one day directly:

curl -G "https://cloud.langfuse.com/api/public/v2/observations" \  -u "$LANGFUSE_PUBLIC_KEY:$LANGFUSE_SECRET_KEY" \  --data-urlencode "fromStartTime=2026-11-09T00:00:00Z" \  --data-urlencode "toStartTime=2026-11-10T00:00:00Z" \  --data-urlencode "type=GENERATION" \  --data-urlencode "fields=core,basic,usage,model" \  --data-urlencode 'filter=[{"type":"number","column":"totalCost","operator":">=","value":0.5}]'

For totals grouped by model, user or time, Langfuse recommends its Metrics API instead of summing rows yourself. A quick per-run check for context growth:

gens = [o for o in run_observations if o["type"] == "GENERATION"]for i, g in enumerate(sorted(gens, key=lambda o: o["startTime"]), 1):    print(i, g.get("inputUsage"), g.get("totalCost"))# 1 1,850   0.004# 2 9,400   0.019# 3 41,200  0.083   <- full documents appended on every step

Common mistakes

  • Looking at averages. A spike from a handful of runs disappears in a daily average; look at the most expensive runs.
  • Trusting cost fields blindly. A model missing from the price table shows $0; a call traced twice shows double.
  • No customer or feature on traces. Without a user ID or feature tag you can't tell one heavy customer from a general regression.
  • Cutting the model before checking the context. Most spikes come from sending more tokens, not from the price per token.
  • No alert. Spikes are cheap when caught in hours and expensive when found on the invoice.

When Rootline helps

Where it fits

Rootline shows cost per run and flags runs over $0.50 as High cost run. It calculates cost from its own model price table, falls back to Langfuse's cost when a model isn't in it, flags runs where the two differ by more than 5%, and counts a model call that was traced twice only once. Every plan shows overall totals and daily spend. The Team plan adds cost per customer and feature, a dashboard alert when one customer spends more than $2 in a day, and an email when the last 24 hours' spend is at least $1 and more than three times (configurable) the daily average of the previous 7 days (at most one such email a day).

Limitations

Costs are calculated from list prices; batch tiers, regional uplifts, negotiated discounts and tool fees are not modelled, so use your provider's invoice as the source of truth for billing. Per-customer views need a user ID on your traces. Rootline does not cap or block spend; it reports and alerts.

Official documentation

Find out why your last failed run broke

Rootline reads your Langfuse or OpenTelemetry traces and explains each failed run: the failing step, the likely cause and a suggested fix. Free to start, no credit card.

Start debugging free creates a free account, no card. Then connect Langfuse or OpenTelemetry.