TUTORIALS 9 min read

LLM Cost Attribution for Multi-Tenant Apps: Track Every Token to a Customer

Aggregate AI spend is useless for unit economics. Attribute model, tool, retrieval, and retry costs to each tenant without turning telemetry into a privacy leak.

By EgoistAI ·
LLM Cost Attribution for Multi-Tenant Apps: Track Every Token to a Customer

Your cloud bill says the AI feature cost $42,000 last month. That number cannot tell you whether one enterprise customer created half the spend, whether free users are profitable, or whether retries quietly doubled inference cost.

Cost attribution turns model usage into product economics. Every billable event needs a tenant, feature, workflow, model, price version, and outcome before it reaches the warehouse.

Create One Billable Event Schema

Do not calculate spend from request logs written by five different services. Emit a normalized event at the provider adapter—the place that knows the actual model response and token counts.

type AiCostEvent = {
  eventId: string;
  tenantId: string;
  userIdHash?: string;
  feature: string;
  workflowId: string;
  attempt: number;
  provider: string;
  model: string;
  inputTokens: number;
  outputTokens: number;
  cachedInputTokens?: number;
  occurredAt: string;
  pricingVersion: string;
};

Keep a stable event ID so queue retries do not duplicate cost. Link attempts to one workflow so the dashboard can separate useful completion cost from wasted retry cost.

Version Prices Instead of Rewriting History

Provider prices change. Discounts differ by account, batch mode, cache usage, region, and contract. Store usage facts separately from price tables, then calculate estimated cost using the price version effective at event time.

Never overwrite old rates. A reproducible invoice needs to answer, “Which rate converted these tokens into this amount?” Maintain provider-list cost, contracted cost, and customer-billed amount as separate measures.

Some products do not expose token counts for every modality. Track image generations, audio seconds, storage, tool calls, search units, and vector operations as typed quantities. Do not force everything into fake tokens.

Propagate Tenant Context Safely

Tenant identity should come from authenticated application context, not a field suggested by the model. Sign or bind the context as it passes through queues, agent runtimes, retrieval services, and provider gateways.

For shared batch requests, either avoid mixing tenants or maintain per-item attribution. If the provider returns only aggregate usage, define and document an allocation rule. Hiding shared overhead makes customer margins look cleaner than they are.

Hash user identifiers before analytics ingestion and avoid storing prompts by default. Cost analysis usually needs token counts, latency, feature, and outcome—not customer content.

Attribute the Whole Workflow

The first completion is rarely the full cost. Include embeddings, reranking, vector retrieval, web search, code execution, safety checks, image generation, tool-provider fees, and every retry.

Track a workflow outcome: succeeded, abandoned, blocked, user-corrected, or escalated. Cost per request is less useful than cost per successful task. A cheaper model that triggers more retries can be the expensive choice.

select tenant_id, feature,
       sum(estimated_cost_usd) / nullif(count_if(outcome='succeeded'), 0)
         as cost_per_success
from ai_workflows
where occurred_at >= current_date - interval '30 days'
group by 1, 2;

Build Budgets Into the Runtime

Dashboards report yesterday’s mistake. Runtime budgets stop today’s. Set limits per request, workflow, tenant, and billing period. Estimate the next step before sending it, then block, downgrade, summarize context, or request approval when the remaining budget is insufficient.

Budgets should distinguish customer entitlements from safety ceilings. A premium plan may receive more capacity, but no tenant should be able to trigger an unbounded agent loop.

Alert on cost per success, cache hit rate, retry multiplier, output-to-input ratio, and spend concentration by tenant. A 20% bill increase can be healthy growth or one broken workflow; attribution tells you which.

Reconcile With Provider Bills

Telemetry will drift. Providers may round, discount, delay, or classify tokens differently. Run daily reconciliation by provider, model, and account. Preserve both raw usage and adjusted cost so finance can trace the gap.

The goal is not false penny-level precision. It is decision-grade visibility: which customer, feature, and workflow consumed capacity; what value it produced; and what changed. Without that, AI pricing is guesswork wearing a dashboard.

Share this article

> Want more like this?

Get the best AI insights delivered weekly.

By subscribing, you agree to our Privacy Policy. You can unsubscribe at any time.

> Related Articles

Tags

LLM costsFinOpsmulti-tenant SaaSobservabilityunit economics

> Stay in the loop

Weekly AI tools & insights.