LLM Cost Tracking For AI Agents
How to track model spend, customer IDs, steps, retries, and token usage for production AI agents.
Track AI agent LLM cost by capturing every provider call with customer_id, step_name, model, token counts, latency, and status. The important difference from a provider dashboard is attribution: the same OpenAI or Anthropic bill must be split by the customer and agent step that caused it.
- - How do I track LLM cost for each customer?
- - How do I attribute OpenAI and Anthropic spend to agent steps?
- - What should an AI agent cost event include?
What To Capture
A useful AI agent cost event needs enough context to explain who caused the spend, what step caused it, and whether the call succeeded.
AgentMeter captures model, provider, tokens in, tokens out, latency, status, customer_id, and optional step_name. Cost is calculated on the server from pricing tables so application code reports usage rather than dollars.
- - Use customer_id for the account, workspace, or tenant that caused the run.
- - Use stable step names such as evaluate, retrieve_context, summarize, or draft_reply.
- - Keep prompts, completions, emails, and raw user messages out of metadata.
Minimal SDK Setup
Install the SDK, initialize it with a telemetry key, and wrap logical agent work in a tracking context. The provider call remains in your runtime, while AgentMeter emits cost-shaped telemetry in the background.
import { init, track } from "@agentmeter/sdk";
init({ apiKey: process.env.AGENTMETER_API_KEY! });
await track("acme-corp", { step: "evaluate" }, async () => {
return openai.chat.completions.create({
model: "gpt-4o-mini",
messages,
});
});Why Step Names Matter
Total spend tells you the bill. Step-level spend tells you what to change. If evaluate is expensive but summarize is cheap, the optimization path is different from a broad model downgrade.
Flat step labels are enough for most early teams because they reveal repeat calls, retry loops, and expensive decision points without requiring a full tracing taxonomy.
Can I track cost without sending prompts?
Yes. AgentMeter expects cost-shaped telemetry, not prompt or completion text.
Does cost_usd come from the SDK?
No. The SDK sends usage signals and the backend calculates cost from pricing tables.
Which provider SDKs are covered first?
The current TypeScript SDK covers OpenAI, Anthropic, and Vercel AI. The Python SDK covers OpenAI and Anthropic.
Per-Customer AI Cost Attribution
A practical guide to attributing AI agent cost by customer, step, model, and source before margins drift.
Report Usage, Not Cost
Why AI agent instrumentation should emit raw usage metrics while the backend calculates dollars.
AgentMeter Vs Langfuse
A factual comparison between open-source LLM observability and AI agent cost infrastructure.