AI & automation

Cost Tracking for LLM Tokens

Calculate and attribute LLM token costs per task using clear log extraction, task metadata tagging, and simple spreadsheet calculations.

To attribute LLM costs to specific client tasks, track input and output token counts for every API call alongside task metadata. Multiply raw token totals by your internal rate model to assign costs per client matter. This process helps teams monitor usage metrics and optimize operating budgets.

Capture Token Usage Metrics at the Request Level

Accurate LLM token cost tracking requires structured logging at the moment of execution. Generic monthly API invoices obscure which projects or end users generate your costs.

Modify your application middleware to inspect the metadata returned in every API response payload. Extract prompt tokens, completion tokens, and total token usage. Attach custom tracking headers or contextual tags, such as task IDs, matter numbers, or user IDs, to the logged event before saving it to your analytics database. Storing raw token usage alongside task parameters provides the granular data required for precise cost analysis and resource planning across teams.

Calculate Task Costs with a Simple Model

To calculate individual task costs, map raw token metrics against your target pricing rates. Assume an arbitrary internal rate model of $0.002 per 1,000 input tokens and $0.006 per 1,000 output tokens.

Extract usage logs into a operational summary format.

Matter ID Task Name Input Tokens Output Tokens Calculation Total Cost
M-101 Summary 5,000 500 (5.0 * $0.002) + (0.5 * $0.006) $0.013
M-102 Extraction 12,000 1,500 (12.0 * $0.002) + (1.5 * $0.006) $0.033

Summing these calculations attributes operational expenses directly to individual client matters.

Diagnosing Missing Metadata and Unattributed Usage

A common breakdown in cost attribution occurs when background tasks or retry loops emit standard calls without client metadata. When an API call fails due to timeouts or temporary upstream errors, your application might retry three times, consuming thousands of tokens. MDN HTTP response status codes.

If your logger records those retries without the original client context, or if the API drops metadata on error responses, unassigned spend accumulates in your general budget. Inspect HTTP status codes on all failing requests to ensure retries maintain task labels. Distinguish unassigned system errors from successful task executions so your usage metrics remain accurate.

Verify Tracking Accuracy with Human Audits

To ensure your token attribution model remains accurate over time, reconcile your internal logs against provider usage dashboards weekly. Aggregate total input and output tokens across all application tasks, and compare those sums to your actual provider invoice totals.

Operational cost tracking establishes workload visibility for engineering and management teams, but it does not replace financial accounting systems. Establish a routine human review step where project managers review flagged high-cost tasks to confirm billing accuracy before client reports are finalized.