Finance dashboard splitting monthly AI spend by product feature and customer

Cost Attribution for LLM Apps: Tokens, Tools, and Tenants

LLM cost attribution is the practice of tying every token, tool call, and GPU-second to the tenant, feature, and release that caused it, so that a $42,000 monthly invoice stops being one frightening number and becomes a table someone can act on. Providers bill by model and API key. Your business decides by customer, plan, and feature. Attribution is the translation layer between those two views.

This post covers app-level attribution: what to record per call, where the tags come from, how to split costs nobody owns, and which unit-economics targets to enforce. If your product is built around autonomous loops, read it alongside agent cost attribution, which goes deeper on multi-step runs, budgets per agent, and runaway-loop protection. The two posts share a ledger idea but answer different questions.

Why provider dashboards are not enough

A vendor console tells you what you spent on a model. It cannot tell you that one enterprise tenant’s nightly summarization job consumed 31% of output tokens, or that last Thursday’s prompt change added 900 tokens to every support reply. Both are common, both are invisible without your own tags, and both are cheap to fix once seen.

There are also costs the API invoice never shows: embedding batches billed through another account, a vector database, reranker calls, and GPU nodes if you self-host. A complete picture needs one ledger that joins all of them.

The invoice has five shapes

Cost shapeWhere it appearsHow to attribute
Uncached input tokensProvider usage objectDirectly from per-call usage
Cached input (reads and writes)Usage object, separate fieldsDirectly, but track writes separately; see prompt caching
Output and reasoning tokensUsage objectDirectly; reasoning tokens often bill as output
Tool and infrastructure callsSearch APIs, code sandboxes, vector DB, rerankersEmit a cost event from each tool wrapper
Retries, failures, and abandoned streamsPartly invisibleRecord every attempt; tag with outcome

The attribution record

One row per model call, written by middleware or a gateway so developers cannot forget. Keep the schema small and stable; every column you add is a column someone will populate inconsistently.

{
  "ts": "2026-11-30T08:14:03Z",
  "request_id": "req_8f21",
  "trace_id": "4bf92f35",
  "tenant_id": "acme-eu",
  "feature": "support-copilot",
  "route": "draft-reply",
  "release": "prompt@2026-11-27.2",
  "model": "provider/model-snapshot",
  "tokens": {"in_uncached": 410, "in_cached": 5990, "cache_write": 0, "out": 280},
  "unit_prices_version": "2026-11-01",
  "cost_usd": 0.0072,
  "attempt": 1,
  "outcome": "success"
}

Store token counts and a price-table version, not just dollars. Prices change, and you will want to recompute history. The trace ID lets you join cost to behavior in the traces described in the LLM observability stack, so a spike can be clicked through to the exact conversation.

Where tags come from

  • tenant_id comes from the authenticated session, never from the client body. A tenant that can spoof another tenant’s ID can also skew your bill.
  • feature and route are static constants set at the call site or in the gateway route table; a lint rule that rejects calls without them is worth the ten minutes.
  • release is the hash or version of prompt, model alias, and retrieval index together. It is the field that answers “what changed on Thursday?”
  • trace_id comes from your tracing context, so tool calls inside one request share it.

If your app talks to tools through a gateway, tag at the gateway so you capture every client at once; MCP gateway patterns describes where that choke point sits.

Allocating costs nobody owns

Some spend is genuinely shared. Pick a rule once, write it down, and apply it mechanically, because arguing about allocation monthly is a worse tax than a slightly imperfect rule.

  • Shared cached prefix writes. Charge the write to the feature whose prompt owns the prefix, and spread reads by actual use.
  • Embedding backfills and index rebuilds. Charge to the platform line, not to whichever tenant’s documents happened to be re-embedded that week, unless you bill for storage.
  • Retries. Charge to the feature, not the tenant, when the failure is yours (timeouts, validation loops). Charge to the tenant when they triggered repeated runs.
  • Idle GPU capacity. Allocate reserved-instance cost by share of tokens served, and show the idle remainder as its own line so utilization is visible.
  • Evals and canaries. Tag them internal; they are the price of quality, and they should have their own budget.

A worked month: $42,000 split by feature

FeatureSpendShareSuccessful tasksCost per success
Support copilot$24,40058%310,000 replies accepted$0.079
Document Q&A (RAG)$9,20022%120,000 answers with citations$0.077
Nightly summarization$6,10015%41,000 summaries$0.149
Evals, canaries, internal$1,9005%n/an/a
Unattributed$4001%n/atarget < 2%

Look at what the table makes obvious. Nightly summarization is 15% of spend but costs almost double per success, and it runs with nobody watching. A cheaper model for that single route, or a batch tier that tolerates delay, might save $2,000 a month without touching the products customers see. A provider-level dashboard would never reveal that.

Targets worth enforcing

  • Cost per successful task per feature (for the table above: ≤ $0.08 for support replies), tracked weekly and compared across releases.
  • P95 cost per conversation ≤ 8× the median. A wide tail points to runaway tool loops or oversized context.
  • Unattributed spend under 2% of the invoice. Higher means calls are escaping the middleware.
  • Reconciliation drift under 2% between your ledger and the provider invoice each month.
  • Budget alerts at 50%, 80%, and 100% of the monthly cap per tenant and per feature, with a hard stop only for tiers where overage is not contractually allowed.

Tie these to reliability objectives, not only finance. A cost regression is a release regression, and it belongs in the same review as latency, as laid out in agent SLAs.

Tenant guardrails that use the ledger

  1. Per-tenant daily token quotas with a soft warning at 80% and a degrade path (smaller model, shorter context) before a hard block.
  2. Rate limits keyed on tenant plus feature, so one script cannot starve the shared pool described in LLM batching and latency.
  3. A kill switch per feature, flippable without a deploy, used in the first fifteen minutes of any cost incident; the full playbook is in incident response for LLM apps.
  4. A price-change checklist: when a provider updates rates or you move models, recompute last month with the new table before announcing margins.

Reconcile with the real invoice

Each month, sum your ledger by model and compare it with the provider invoice. Differences of 1–2% come from timing and rounding. Larger gaps usually mean untagged callers (a notebook with a production key), unrecorded retries, or cached-token pricing handled wrongly. Check the field semantics against the vendor docs; for example, OpenAI documents cached tokens within the usage object in its prompt caching guide, and Anthropic reports cache creation and read tokens separately in its prompt caching docs. Billing them as ordinary input is the most common reason ledgers drift. For the wider operating model, see the LLMOps production guide.

FAQ

What is LLM cost attribution?

It is recording, for every model call and tool call, which tenant, feature, and release caused the spend, so costs can be reported and controlled by business unit instead of only by provider account or API key.

What fields should I log for each LLM call?

At minimum: timestamp, request and trace IDs, tenant, feature, route, release version, model, token counts split by uncached input, cached input, cache writes and output, price-table version, computed cost, attempt number, and outcome.

How do I allocate the cost of retries and shared prompts?

Choose a written rule and apply it consistently. A common approach charges retries to the feature when the failure is yours, charges shared cache writes to the feature that owns the prefix, and tracks idle GPU time as its own line.

How accurate should my ledger be compared with the provider invoice?

Aim for within about 2% each month. Larger gaps usually mean untagged callers, missing retry records, or mishandled cached-token pricing, and each is worth fixing because it hides real spend.

What is a good unit-cost metric for an LLM feature?

Cost per successful task, such as an accepted support reply or a cited answer. It ties spend to value and exposes features that are expensive because they fail and retry, which total spend alone does not.

How is this different from agent cost attribution?

App-level attribution covers every model and tool call across a product, including tenants and features. Agent-focused attribution adds run-level budgets, step tracking, and loop protection. The ledger fields overlap, so most teams use both.

Related reading

About Mudasir Syed

Check Also

MCP Install Config Explained (stdio vs HTTP) — professional topic photo

MCP Install Config Explained (stdio vs HTTP)

MCP install config: practical guide with definitions, implementation checklist, security notes, and internal links across the agentic stack.

Leave a Reply