Skip to content
howdoaiagentswork.com

AI Agent Cost Tracking

AI agent cost tracking

Track cost at the event level, roll it up to an agent run, and divide by successful outcomes. A monthly provider invoice can tell you what you spent; it cannot tell you which workflow, tool, retry, or customer created that spend.

See every step in the AI agent loop →

What to Track

Create one cost event for every billable or capacity-consuming action. Model calls are the obvious source, but production agents also pay for embeddings, vector storage, search APIs, browsers, code sandboxes, and retry loops.

Event typeUsage fieldsUseful dimensions
Model callInput, output, cached, and reasoning tokensProvider, model, step, prompt version
Tool callRequests, runtime, bytes, vendor costTool name, status, retry reason
Memory writeEmbedding tokens, vectors, bytes retainedMemory type, collection, retention policy
Agent runTotal events, duration, outcomeWorkflow, tenant, environment, version

A Cost Event Schema

Use immutable events rather than updating one running total. Events can be replayed when a provider changes pricing, and they preserve failed attempts that would otherwise disappear from success-only metrics.

{
  "trace_id": "tr_123",
  "run_id": "run_456",
  "step": 3,
  "event_type": "model_call",
  "provider": "example-provider",
  "model": "example-model",
  "input_tokens": 2840,
  "output_tokens": 412,
  "cached_input_tokens": 900,
  "cost_usd": 0.008976,
  "latency_ms": 1840,
  "success": true,
  "timestamp": "2026-08-07T10:30:00Z"
}

Store the raw usage fields and the calculated cost. Raw usage lets you recompute historical spend; the stored cost preserves what your system believed at execution time. Do not put prompts, secrets, or raw tool output in a cost event.

Cost Formulas That Matter

Estimate model budgets before launch with the token budget calculatorand memory infrastructure with the memory cost calculator.

Instrument Every Step

Put telemetry around the provider adapter, not inside individual prompts. The wrapper should record success and failure, use the provider's returned usage when available, and attach the current trace and run IDs.

async function callModel(input: ModelInput, trace: TraceContext) {
  const startedAt = performance.now();
  try {
    const response = await provider.generate(input);
    await costEvents.write({
      ...trace,
      event_type: "model_call",
      provider: response.provider,
      model: response.model,
      input_tokens: response.usage.inputTokens,
      output_tokens: response.usage.outputTokens,
      cost_usd: price(response.usage, response.model),
      latency_ms: performance.now() - startedAt,
      success: true,
    });
    return response;
  } catch (error) {
    await costEvents.write({
      ...trace,
      event_type: "model_call",
      cost_usd: 0,
      latency_ms: performance.now() - startedAt,
      success: false,
    });
    throw error;
  }
}

Apply the same wrapper to tools and memory operations. If a request times out after the vendor accepted it, record the event with an unknown or estimated cost and reconcile it later instead of silently assigning zero.

Dashboards and Budget Alerts

Start with distributions, not only totals. Track p50, p95, and p99 cost per run by workflow and outcome. A stable monthly total can hide a small group of looping runs that create poor user experiences and future budget risk.

Common Tracking Mistakes

Do not attribute all spend to the final successful run, ignore failed tool calls, or mix list price with discounted invoice price in one metric. Do not use tokens as a substitute for dollars when a workflow uses several models with different prices.

Cost is also not quality. A cheaper run that completes the wrong task is waste. Join cost events with task evaluations so optimization preserves completion rate and safety.

Frequently Asked Questions

Should AI agent cost tracking happen in real time?

Hard caps need a running estimate during execution. Detailed billing and reconciliation can be asynchronous as long as events are durable and carry stable trace and run identifiers.

How should shared infrastructure be allocated?

Keep direct event costs separate from allocated platform costs. Assign shared databases and reserved compute using a documented driver such as runs, runtime, or stored bytes so unit economics remain auditable.

What is the most important agent cost metric?

Cost per successful task is usually more useful than cost per call or per run because it includes failure and retry spend while staying tied to an outcome.

Next Steps

Map events onto the complete perceive-reason-plan-act-feedback loop, then set budgets using the two calculators above.

Calculate a token budget →Return to the AI agent architecture guide →

ai agent cost tracking — return to the complete AI agent architecture guide.

Was this helpful?

Your feedback stays on this page — no tracking.

Share this page