How do I attribute AI costs to specific agents, tools, and errors?
Summary
- Attributing AI cost to agents, tools and errors takes three layers: an identity or tag on every request for the agent, step-level traces for the tools, and status and retry data for the errors.
- On Databricks, Unity Gateway adds the model service, destination model and requesting principal to model serving records in
system.billing.usage, and service tags flow into itscustom_tagsfield. - Service tags work best when each agent has its own model service. For a shared service, per-request tags in the
Databricks-Ai-Gateway-Request-Tagsheader attribute tokens insystem.ai_gateway.usageand external spend insystem.ai_gateway.external_model_spend. - Tool and error costs come from traces: MLflow Tracing records token usage and cost for every step, and Unity Gateway logs status codes, fallback attempts and retries for each request.
How do I attribute AI costs to specific agents, tools, and errors?
Give every agent an identity the billing data can see, tag every request with what it's for, and trace each run so token usage lands on the step that spent it. Agent-level attribution comes from separate model services, service principals or request tags. Tool-level attribution comes from traces that record tokens for each step. Error-level attribution comes from status codes, fallback attempts and failed spans that you join back to usage. Skip any layer and the bill stops at "model serving" with no owner.
Why AI cost attribution is hard
One agent run can call several models, retry after an error, fall back to another provider and invoke tools that trigger more model calls. A provider invoice or billing export shows consumption by model or SKU. It doesn't know which agent, which tool call or which failed retry caused it. Attribution has to be designed in at the point where requests are made.
Attribution levels and where the data lives
| Level | Attribution key | Databricks source |
|---|---|---|
| Team or project | Service tags, request tags | custom_tags in billing, endpoint_tags and request_tags in usage |
| Agent | Its own model service or service principal, or a request tag | endpoint_name, identity_metadata.run_by, request_tags |
| Coding agent and session | Agent name, session ID | session_metadata (Public Preview) |
| Tool or step | Span-level token usage | MLflow traces, unified trace table (Beta) |
| MCP tool | Calls per tool | mcp_metadata.tool_name |
| Errors and retries | Status, error code, attempt type | status_code, routing_information, databricks.action |
How Databricks approaches AI cost attribution
Dollars by service, model and principal. For requests served through a Unity Gateway model service, Databricks populates usage_metadata.ai_gateway.endpoint_name, destination_model and destination_id, plus identity_metadata.run_by, on MODEL_SERVING records in system.billing.usage. Service tags such as team or cost_center appear in custom_tags (Analyze Unity Gateway cost). Join system.billing.list_prices to convert DBUs to list-price dollars, which exclude negotiated discounts (cost tutorial). For external providers, system.ai_gateway.external_model_spend records estimated USD, aggregated hourly, with endpoint_tags, request_tags and the requester's identity.
Agents. The docs draw a line that shapes your design. For Databricks-provided models, only service tags propagate to system.billing.usage. For external provider models, both service and request tags reach the external spend table. So for dollar-level attribution on Databricks-hosted models, give each agent its own model service with a service tag, or run each agent under its own service principal so run_by separates them. Request tags in the Databricks-Ai-Gateway-Request-Tags header still attribute tokens on a shared service in system.ai_gateway.usage (request tagging). For coding agents, session_metadata (Public Preview) records the agent name, version and session ID on each request.
Tools and steps. MLflow Tracing records the inputs, outputs, latency, token usage and cost of every intermediate step, so you can see which part of a run consumed the budget: a retrieval loop, a planning step or a subagent. MCP tool calls are counted per tool with mcp_metadata.tool_name. In the unified trace table (Beta), model spans carry gen_ai.usage.input_tokens and gen_ai.usage.output_tokens, and MCP spans carry gen_ai.tool.name, all in one OpenTelemetry table (schema reference).
Errors and retries. Each row in system.ai_gateway.usage has a status_code, and routing_information lists every fallback attempt with its error_code and latency (Track model usage). Trace spans mark databricks.action as initial_attempt or retry and status.code as STATUS_CODE_ERROR on failures. Group token usage by those fields to see how much consumption is tied to retries and failing runs. The docs suggest analyzing common error patterns in calls to models and MCPs as a way to reduce token usage.
Control what you've attributed. Budgets for Unity Gateway set shared and per-user monthly thresholds that alert or block usage. Their tag filters apply to model service tags, not request tags. Including external model usage in a budget is in Beta.
FAQs
Should I use service tags or request tags?
Use service tags when each agent or team has its own model service, and for any dollar attribution on Databricks-provided models. Use request tags when several applications share one service, to attribute tokens and external spend.
Can I see cost by user without tags?
Yes. identity_metadata.run_by in system.billing.usage identifies the user or service principal behind each request, and the usage dashboard's Cost Analysis view ranks top users by cost.
How current is the cost data?
The budgets docs say system.billing.usage updates every few hours and system.ai_gateway.external_model_spend updates hourly, while budget enforcement runs on near real-time estimates.
Are external model costs exact?
No. Databricks estimates them from token usage and each provider's published prices, adjusted by any configured price multiplier. Your provider invoice is the source of truth.
The information provided herein is for general informational purposes only and may not reflect the most current product capabilities or configurations.