New Tool Radar

AI Agents & Automation

How to Log Agent Token Usage Per Run for a Cost Report

Log agent token usage per run so the cost of each task is visible in one report.

Schematic with three steps: Record the usage with every run; Add the usage up by task and; Review the report and act on the.

A run’s cost is invisible until someone adds it up, and by then the budget is already spent. Logging token usage per run makes it visible: each run records its prompt tokens, completion tokens, and total, with the run id and task name attached, and the report adds them up by task and by week. CrewAI Flows expose usage metrics aggregated across every LLM call in a run, which is the shape to copy: totals per run, not per call, so the report reads at a glance. Keep the report where the owner looks, and review it on a schedule, because a report nobody reads is a report that changes nothing.

What you need

  • A run that reports its token usage, per call or per run
  • A log that stores the usage with the run id and task name
  • A report that adds the usage up by task and by week

Step 1: Record the usage with every run

After each run, record its prompt tokens, completion tokens, and total, with the run id and task name. A run with no recorded usage is a run with no cost.

Step 2: Add the usage up by task and by week

Build the report by summing the recorded usage per task and per week, so the most expensive tasks stand out. A single total hides which task is spending the budget.

Step 3: Review the report and act on the most expensive task

Read the report on a schedule and pick the most expensive task: check whether it can use a cheaper model, fewer steps, or a tighter budget. A report with no action is just a list.

Worked example: synthetic token usage report

The table below is a synthetic example. It shows the expected result of the usage report; no tokens were spent.

Task Runs Total tokens
Inbox triage 20 180,000
Price comparison 12 96,000
Meeting prep 8 40,000
Most expensive task named Inbox triage Pass

Expected result (UNTESTED). the report shows token totals per task, inbox triage is the most expensive, and it is picked for review. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • Usage reporting lags the actual spend, so a fast run can overshoot before the report sees it.
  • Token counts are not the whole cost: paid tool calls must be counted separately.
  • A report by task only helps if the task names are consistent across runs.
  • Logging usage costs storage, so keep the per-run record small.

Sources

See all