← Back to blog

AI Agent Cost Breakdown | AgentMeter

A practical way to measure agent costs

Agent costs are easy to underestimate when a single task makes several model calls, retries, or paid tool requests. A monthly provider total tells you what you spent; it does not show which workflow used it or how much an individual run cost.

This guide shows how to instrument a workflow with AgentMeter, record model and tool costs, and use per-run measurements to make budget decisions. It is a tutorial, not a benchmark report: it contains no measured run dataset or claims about typical costs.

Start with a task boundary

Pick one workflow with a clear start and finish, such as resolving a support ticket or reviewing a code change. Give every run a stable workflow name and useful non-sensitive tags. Avoid customer identifiers or prompt contents unless your data policy explicitly permits them.

Install the SDK and initialize it with an API key stored in the environment:

npm install @grislabs/agentmeter @anthropic-ai/sdk
import Anthropic from "@anthropic-ai/sdk";
import { budget, extractors, init, trace } from "@grislabs/agentmeter";

const apiKey = process.env.AGENTMETER_API_KEY;
if (!apiKey) throw new Error("Set AGENTMETER_API_KEY before starting");

init({ apiKey });
const anthropic = new Anthropic();

// Choose a limit that fits your own workflow and economics.
budget("support-resolution", { perRun: 2, onExceeded: "kill" });

const answer = await trace(
  "support-resolution",
  { environment: process.env.NODE_ENV ?? "development" },
  async (ctx) => {
    return ctx.llm(
      anthropic.messages.create({
        model: "claude-sonnet-4-20250514",
        max_tokens: 800,
        messages: [{ role: "user", content: "Summarize the issue and suggest next steps." }],
      }),
      extractors.anthropic,
    );
  },
);

The model call is real if you run this example, so it may incur provider charges. The limit is an example setting, not a recommended price. AgentMeter uses the response's reported model and token usage with its pricing table to estimate the call cost; configure custom pricing when your model or rates are not represented accurately.

Include non-model costs deliberately

AgentMeter's LLM instrumentation records model usage. If a workflow also pays for a search API, MCP tool, or other service, wrap that call with ctx.tool() and pass the actual fee from your billing source:

const result = await ctx.tool("web-search", search(query), {
  cost: observedFeeUsd,
  costType: "api",
});

Use the fee in the currency expected by your reporting setup, and document whether it is an invoice amount, provider estimate, or allocation rule. Do not treat a missing fee as zero. The SDK supports mcp, api, compute, and other tool cost types. For a payment performed inside the workflow, use the payment tracing API with the amount and provider details.

Read the distribution, not just the total

Once representative runs have been collected, compare costs by workflow and model. The AgentMeter CLI provides overall status and workflow summaries:

npx agentmeter status
npx agentmeter workflows

Use the median to describe a typical observed run and a high percentile, such as p95, to understand the more expensive part of your own distribution. Also check run success, duration, input/output tokens, tool calls, and retries. Keep environments and materially different task types separate; otherwise the summary can hide meaningful differences.

These summaries describe only the runs you measured. A small or biased sample is not a forecast, and provider prices and prompts can change. Record the date range, model names, pricing source, sample size, exclusions, and failure handling whenever you publish results from your own data.

Set a limit and understand what it does

Choose a per-run budget based on your measured workload and the amount you are willing to spend. With onExceeded: "kill", AgentMeter raises a budget error when a recorded cost pushes the run over its per-run threshold. The call that crosses the threshold has already happened, so the limit is a guardrail—not a guarantee that total provider spend can never exceed the threshold. Handle the error at your workflow boundary and decide whether to stop, retry with a cheaper model, or escalate to a person.

A short measurement checklist

  • Name and tag each workflow consistently without collecting unnecessary personal data.
  • Instrument model calls and record actual fees for paid tools and services.
  • Separate successful, failed, and retried runs when comparing outcomes.
  • Compare per-workflow medians and upper percentiles over a defined period.
  • Review model pricing and custom rates before relying on dollar totals.
  • Set a per-run limit, handle budget errors, and verify the behavior in a controlled test.

AgentMeter is an open-source SDK for tracking per-task costs across model calls, tools, and agent payments. See the SDK quick start to adapt this workflow to your application.

Check what happens during recovery

Retries affect both cost and external actions. Our Agent Evals guide to turning a failure into a regression test includes a CRM timeout example and a downloadable test-case template. Use the service’s mutation ledger to check for duplicate writes, alongside your workflow cost measurements.

Track your AI agent costs

Join the waitlist for early access to the hosted dashboard.