LLM cost and usage tracking

When the agent runtime calls a language model — to score a candidate, generate a screening message, summarize a CV — that call costs money. Real money, paid to OpenAI / Anthropic / Requesty / whichever model provider is in play. Crew tracks every one of those calls so you can answer three operational questions:

The Analytics dashboard.

  1. How much did this tenant cost us this month?
  2. Is this tenant approaching its usage quota?
  3. Why is this tenant suddenly costing 3× what it did last month?

The data lives in the tenant_llm_usage table, one row per LLM call.

What gets logged

Every row records:

Field What it is
tenant_id Which workspace this call was for.
model The provider model name (gpt-4o, claude-sonnet-4, etc.).
agent_id (when applicable) Which agent triggered the call.
input_tokens Token count of the prompt.
output_tokens Token count of the completion.
cached_tokens Token count served from the provider's prompt cache (cheaper).
cost_micro_usd Computed cost in millionths of a USD, using pricing at the time of the call.
latency_ms How long the call took end-to-end.
purpose Free-text bucket (screening, scoring, messaging, etc.) used for grouping.
created_at When the call happened.

cost_micro_usd is an integer in millionths of a dollar — 2,400 means $0.0024. This keeps the math precise (no floating-point drift across millions of rows) and lets you sum a year of usage without overflow.

How cost gets computed

At log time, the agent runtime applies the current pricing for that model to the token counts. The result is stored as a static number on the row. Subsequent pricing changes don't retroactively shift historical cost.

This matters because:

  • You can audit "what did we charge this tenant" against the rows, and the answer doesn't change when the model vendor adjusts pricing.
  • A future pricing decrease doesn't make last quarter's costs look artificially high in comparison.
  • A row from January 2026 reflects January 2026 pricing forever.

The price table itself is in code (lib/agents/pricing.ts), versioned with the deployment. When pricing changes, the team bumps the price table, and going forward all new rows use the new price.

What the data powers

Per-tenant cost dashboards. The admin UI surfaces "this tenant's spend this month" by summing cost_micro_usd for the tenant over the period. Drill down by model, agent_id, or purpose to see where the dollars went.

Quota enforcement. Tenants have an optional monthly cost cap. When the cap is approached (typically at 80%), the team gets an alert; when exceeded, the agent runtime can be configured to throttle or stop. Quota is computed live from tenant_llm_usage, not from a separate counter — there's only one source of truth.

Anomaly detection. A week-over-week comparison of spend per tenant catches the case where a playbook change suddenly tripled token usage on every application. The cost data is the single most reliable signal that something changed in agent behavior.

Billing audits. When a client questions an invoice, the row-level data is the answer. "Here are the 14,832 calls we made on your behalf last month, grouped by agent, with token counts and cost. Total: $X."

Constraints and gotchas

  • The table grows fast. A busy tenant generates 10,000+ rows per day. Reads need to use indexed columns (tenant_id + created_at, or model + created_at) — full scans are not workable past a few weeks of data.
  • Old rows are archived, not purged. After 90 days (configurable), rows are moved to cold storage by a background job. The live table only holds recent activity for fast querying. Audit queries against old data take longer but still work.
  • cached_tokens rows are cheap but logged at full token count. The cost field reflects the cached pricing, but the input_tokens field is the uncached count for clarity. To see actual money saved by caching, look at the difference between input_tokens * uncached_price and the recorded cost_micro_usd.
  • Failed calls are logged too. A model call that errored still incurred the input tokens (often). The row records what happened, with the error reflected in a status field. Cost is what was actually billed by the provider — typically zero for hard errors, full input cost for partial responses.
  • Cost is not the only resource. Latency matters for UX. Token volume matters for rate limits. The table captures all three; dashboards typically focus on cost but the underlying data supports any cut.

A concrete example

A row from Acme Co's tenant_llm_usage table:

tenant_id:        12 (Acme Co)
model:            claude-sonnet-4
agent_id:         screen-v3
input_tokens:     2,134
output_tokens:    487
cached_tokens:    1,800
cost_micro_usd:   2,400        ($0.0024)
latency_ms:       1,840
purpose:          screening
created_at:       2026-05-17 14:32:01

This row says: the Screen agent called claude-sonnet-4 to process a screening response, sent 2,134 prompt tokens (of which 1,800 were cached, so most of the prompt was free), got 487 output tokens, and the call cost a quarter of a cent and took just under two seconds.

Multiply by 50,000 calls per month for this tenant and you're at roughly $120 in LLM cost — easily summed by aggregating the cost_micro_usd column for that tenant over the month.

Controlling cost with the per-agent model picker

Each agent has a model picker that lets admins choose which language model it runs on. Selecting a lightweight Haiku-tier model for agents that handle high-volume, repetitive tasks — initial screening messages, simple answer validation, templated follow-ups — can significantly reduce cost without meaningful quality loss. Reserve larger, more capable models for agents whose proposals involve judgment-heavy decisions (offer scoring, CV summarization, nuanced rejection reasons). The combination of per-agent model selection and the autonomy dial gives admins two independent levers for balancing AI quality against spend. Cost of each call is logged immediately in tenant_llm_usage against the chosen model.

See also