Skip to content
Blog

Cost Attribution Across Agents: Chargeback by Team and Tenant

Stop shipping LLM spend through one shared key. Tag every request, run a LiteLLM gateway with per-key budgets, compute per-request cost, and build a real chargeback model.

Published on • November 16, 2026

AI Assistant

The finance channel lights up on the first of the month: “Why did we spend $14k on the LLM this month?” Nobody knows. Every service shares one API key, the provider bill arrives as a single line item, and the debate over who caused what devolves into guesswork. Attribution is not a finance feature bolted on later — it is an engineering property you design in before the first request ships.

In this post, you will learn how to attribute every LLM dollar to a team, tenant, agent, user, and feature: tagging requests at the edge, standing up a LiteLLM gateway with virtual keys and hard budget limits, computing per-request cost from token counts × model pricing, exporting spend into a warehouse for dashboards, and choosing between showback and chargeback — including how to split shared, pooled sessions fairly.

Prerequisites

  • Python 3.10+ and comfort with OpenAI-compatible HTTP APIs.
  • Docker (or uv tool install 'litellm[proxy]') and a Postgres database — LiteLLM needs DATABASE_URL for virtual keys and spend tracking.
  • An API key for at least one model provider.
  • A place to land exported spend data (Postgres, BigQuery, or Snowflake all work; examples use Postgres-flavored SQL).

Tag Every Request at the Source

A gateway can only report on what you tell it. The moment a request enters your infrastructure, it should carry five attribution dimensions: team (who owns the code), tenant (whose data is being processed), agent (which pipeline made the call), user (the end user inside the tenant), and feature (which product surface triggered it). If you wait until the gateway to infer these, you will be scraping user agents and guessing.

The cheapest place to stamp them is a thin client wrapper. LiteLLM accepts tags in the request body under metadata.tags, plus a free-form spend_logs_metadata object for key/value pairs you want in the transaction log:

# attribution.py — one client, every request stamped.
import os
from openai import OpenAI


class AttributedClient:
    """OpenAI-compatible client that attaches attribution metadata to every call."""

    def __init__(self, team: str, tenant: str, agent: str, feature: str):
        self._client = OpenAI(
            api_key=os.getenv("LLM_GATEWAY_KEY", "sk-..."),
            base_url=os.getenv("LLM_GATEWAY_URL", "http://localhost:4000") + "/v1",
        )
        self._tags = [f"team:{team}", f"tenant:{tenant}", f"agent:{agent}", f"feature:{feature}"]

    def chat(self, messages, *, user_id: str, model: str = "fast-chat", **kwargs):
        # `user` feeds per-end-user spend; tags feed per-team/feature dashboards.
        return self._client.chat.completions.create(
            model=model,
            messages=messages,
            user=user_id,
            extra_body={
                "metadata": {
                    "tags": self._tags,
                    "spend_logs_metadata": {"user": user_id},
                }
            },
            **kwargs,
        )

Two details matter here. First, the user field is what LiteLLM uses for end-user spend tracking — but its own docs warn that end users who control their own requests can pass an arbitrary user value and dodge attribution. If you track spend per end user, always set user_id when minting keys in backend services rather than trusting the request body. Second, tags are convention-driven: prefix them (team:, feature:) so your warehouse queries can filter by namespace. LiteLLM also supports an x-litellm-tags header and config-based tags, and litellm_settings.extra_spend_tag_headers lets you promote any custom header — say x-product-feature — into spend tags without touching application code.

Gateway-Level Spend Tracking and Budget Limits

With LiteLLM as a centralized AI gateway, every provider call funnels through one service that issues virtual keys per team, tracks spend per key/user/team, and enforces max_budget before a runaway loop becomes a purchase order. As the LiteLLM README puts it, the proxy gives you “virtual keys, spend tracking, guardrails, load balancing, and an admin dashboard out of the box.”

Start the proxy with a config that enables tagging and a database:

# litellm-config.yaml
model_list:
  - model_name: fast-chat
    litellm_params:
      model: openai/gpt-6.1-sol
      api_key: os.environ/OPENAI_API_KEY

litellm_settings:
  drop_params: true
  extra_spend_tag_headers:
    - "x-product-feature"

general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY   # admin key, must start with sk-
  database_url: os.environ/DATABASE_URL       # required for keys + spend

Then mint a budgeted key per team:

curl http://localhost:4000/key/generate \
  -H "Authorization: Bearer $LITELLM_MASTER_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "key_alias": "search-team-prod",
    "team_id": "team-search",
    "max_budget": 500,
    "budget_duration": "30d",
    "models": ["fast-chat"],
    "metadata": {"tags": ["team:search", "env:prod"]}
  }'

The proxy now has a hard stop: when the key’s spend crosses $500 within its 30-day budget window, further requests are rejected. Spend accumulates in LiteLLM_VerificationTokenTable (per key), LiteLLM_UserTable, and LiteLLM_TeamTable, so GET /key/info, GET /user/info, and GET /team/info answer “who spent what” in real time. Because keys inherit their owner’s limits, a team-level budget caps every key minted under that team — useful as a backstop, dangerous if you expected keys to be independent.

Per-Request Cost: Token Counts × Model Pricing

Dashboards are only as honest as their cost function. LiteLLM keeps a public model cost map (model_prices_and_context_window.json) with per-token input/output prices, and its completion_cost() function multiplies the usage numbers in each response by those rates. You can reproduce the arithmetic yourself when you need it outside the gateway:

# USD cost of one request from raw token counts.
PRICES = {  # per 1M tokens, standard tier
    "fast-chat": {"input": 2.00, "output": 10.00},
    "cheap-chat": {"input": 0.30, "output": 2.50},
}


def request_cost(model: str, prompt_tokens: int, completion_tokens: int) -> float:
    p = PRICES[model]
    return (prompt_tokens * p["input"] + completion_tokens * p["output"]) / 1_000_000


print(request_cost("fast-chat", 1200, 350))  # 0.0059

Three caveats keep this honest. Cached input tokens are billed at a lower rate — include them as a third column or your totals will drift from the invoice. Thinking tokens count as output. And LiteLLM explicitly flags requests that carry usage but price to $0 because their model lacks a pricing entry: you get a warning log and a Prometheus counter (litellm_zero_cost_requests_total) so missing pricing shows up as an alert instead of silent under-billing. Inside the gateway you don’t need the math — the response header x-litellm-response-cost carries the computed cost on every call.

Export to a Warehouse and Build Dashboards

The proxy’s built-in UI answers “what happened yesterday.” Chargeback needs history. Two export paths cover it: /user/daily/activity gives you daily rollups by model, provider, and API key, while /spend/logs/v2?summarize=false&page=1&page_size=1000 returns individual transaction logs with pagination — the recommended endpoint for ETL, since the legacy /spend/logs truncates at 10,000 rows. Land them in one table:

CREATE TABLE llm_spend (
    request_id        TEXT PRIMARY KEY,          -- gateway request id
    created_at        TIMESTAMPTZ NOT NULL,
    team              TEXT NOT NULL,             -- from tags: team:*
    tenant            TEXT NOT NULL,             -- from tags: tenant:*
    agent             TEXT,                      -- from tags: agent:*
    feature           TEXT,                      -- from tags: feature:*
    user_id           TEXT,
    model             TEXT NOT NULL,
    prompt_tokens     BIGINT NOT NULL,
    completion_tokens BIGINT NOT NULL,
    cost_usd          NUMERIC(12, 6) NOT NULL,
    latency_ms        INTEGER,
    status            TEXT NOT NULL
);

CREATE INDEX llm_spend_team_month ON llm_spend (team, created_at);
CREATE INDEX llm_spend_tenant_month ON llm_spend (tenant, created_at);

From there, dashboards are ordinary SQL. The monthly chargeback report is one query:

SELECT team,
       date_trunc('month', created_at) AS month,
       COUNT(*)                        AS requests,
       SUM(cost_usd)                   AS cost_usd
FROM llm_spend
GROUP BY 1, 2
ORDER BY cost_usd DESC;

Wire that into Metabase or Looker, and the “why did we spend $14k” question becomes a table with someone’s name on every row. LiteLLM’s own /global/spend/report?group_by=team endpoint produces the same shape directly from the gateway if you want a zero-ETL starting point.

Showback vs Chargeback (and Fair Shared Sessions)

Showback means publishing the numbers: each team sees their monthly cost, nobody’s budget actually moves. Chargeback means the cost is debited — the team’s budget shrinks by what they consumed, and overspend is a decision they own. The difference is political as much as technical.

Start with showback for a quarter or two. Tagging coverage will be incomplete at first (ungrouped spend lands in an unattributed bucket — LiteLLM uses exactly that fallback label for missing keys), unit costs shift while you tune models, and teams need time to react to visibility before they react to invoices. Move to chargeback once three things hold: >95% of spend carries a team: tag, costs per task tier are stable, and every team has a budget that reflects reality. LiteLLM makes the transition cheap — the same max_budget you used as a guardrail becomes the enforcement mechanism.

Shared, pooled sessions are the hard case: one support agent conversation touches a tenant-scoped retriever, a shared summarizer, and a pooled guardrail — whose dollar is it? Three workable policies:

  1. Per-request attribution when tags exist (preferred; what the middleware above does).
  2. Usage-weighted splitting — split the session cost by each participant’s token share.
  3. Amortized pooling for genuinely shared work (system prompts, caching infrastructure): split evenly or by seat count, and record it as a platform line item rather than pretending it is per-tenant.
def allocate(session_cost: float, participants: list[str],
             weights: dict[str, float] | None = None) -> dict[str, float]:
    """Split one session's cost across tenants; equal shares unless weights given."""
    weights = weights or {p: 1.0 for p in participants}
    total = sum(weights.values())
    return {p: round(session_cost * weights[p] / total, 6) for p in participants}


print(allocate(0.42, ["acme", "globex"], {"acme": 3, "globex": 1}))
# {'acme': 0.315, 'globex': 0.105}

Attribute cache-read savings the same way you attribute cost — proportionally — so the tenant that caused the cache to exist isn’t the only one paying for it, and the one that reuses it isn’t the only one profiting.

Putting It All Together

The pieces form one pipeline:

  1. An agent calls AttributedClient.chat(...), stamping team/tenant/agent/feature tags and the end-user id.
  2. The LiteLLM proxy authenticates the team’s virtual key, checks max_budget, forwards to the provider, computes cost from the model cost map, writes the spend log, and returns x-litellm-response-cost.
  3. A nightly job pages /spend/logs/v2 and upserts into llm_spend.
  4. The chargeback query produces the monthly invoice; the dashboard shows trends and anomaly days.

A finished report looks like this:

team          month     requests   cost_usd
search        2026-11   412,884    4,102.55
support-bot   2026-11   198,201    3,411.09
docs-rag      2026-11   87,552       640.18
unattributed  2026-11      311        12.44   <- wire up this tag!

That last row is your backlog: every unattributed dollar is a request that entered without tags, and shrinking it is how you earn the right to switch from showback to chargeback.

Conclusion and Next Steps

You started with an unexplainable bill and ended with a system where every request carries its owner, its cost lands in a queryable table, and budgets stop overruns before they happen: edge tagging, a LiteLLM gateway with virtual keys, cost computed from tokens × pricing, warehouse export, and a deliberate showback → chargeback rollout with a fair policy for pooled sessions.

Homework, in rough order of leverage:

  • Add the x-product-feature header to your client and enable extra_spend_tag_headers so feature-level attribution works without redeploying every service.
  • Alert on litellm_zero_cost_requests_total — a missing pricing entry is a billing bug.
  • Add a weekly anomaly query (cost per team, 7-day moving average, z-score > 3) and page the owning team, not finance.
  • Revisit your chargeback criteria quarterly; the goal is a budget every team believes in, not a spreadsheet that wins arguments.