Skip to main content

How to See the Token Usage and Cost of LLM Calls in MLflow

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Every traced LLM call in MLflow carries its token counts and an estimated cost in dollars. You can read them per call in the UI, as totals and charts on the experiment's dashboard, and in Python for your own reports. This guide shows all three for one Claude call, with real output from MLflow 3.18.0.

Syntax​

Syntax
trace = mlflow.get_trace(<trace ID>, flush=True)

trace.info.token_usage
trace.info.cost
PartWhat it means
mlflow.get_trace(<trace ID>, flush=True)Loads a trace; flush=True first waits for traces still being sent
trace.info.token_usageA dictionary of input_tokens, output_tokens and total_tokens, plus cache counts for models that report them
trace.info.costA dictionary of input_cost, output_cost and total_cost, in US dollars

MLflow's Token Usage and Cost page: "MLflow automatically tracks token usage and cost for LLM calls within your traces." The cost is "The estimated cost in USD for each LLM call, calculated based on model pricing", and the UI notes that "Cost is estimated from token usage and public model pricing."

What It Costs​

MLflow is free. The traced call is paid: this one to Claude Opus 5.5 cost $0.0015, the figure MLflow itself reports below.

Prerequisites​

  • A tracking server with a traced LLM call. This guide uses a server at http://127.0.0.1:8080 and the script below, which traces one Claude call with mlflow.anthropic.autolog().

Step 1: Trace a Call and Read Its Usage in Python​

token_cost.py
import anthropic
import mlflow

mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("token-cost")
mlflow.anthropic.autolog()

client = anthropic.Anthropic()
client.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Suggest a name for a camping stove, in three words or fewer."}],
)

trace = mlflow.get_trace(mlflow.get_last_active_trace_id(), flush=True)
print("Token usage:", trace.info.token_usage)
print("Cost (USD):", trace.info.cost)

The token_cost.py script in a macOS-style code window: one traced Claude call, then mlflow.get_trace with the last active trace ID and flush=True, printing the trace's token usage and cost.

mlflow.get_last_active_trace_id() returns the ID of the trace the call just made. flush=True matters: without it, get_trace ran before MLflow had finished sending the trace and returned nothing. Fixed: "Trace with ID … cannot be loaded because its span data is corrupted" When You Call mlflow.get_trace shows that failure.

Terminal
uv run --env-file .env token_cost.py
Output
2026/11/30 17:34:35 INFO mlflow.tracking.fluent: Experiment with name 'token-cost' does not exist. Creating a new experiment.
/Users/you/source/token-cost/.venv/lib/python3.15/site-packages/google/protobuf/json_format.py:1065: FutureWarning: invalid character '+' in URL-safe Base64 data will be discarded in future Python versions
return base64.urlsafe_b64decode(padded_value)
Token usage: {'input_tokens': 29, 'output_tokens': 69, 'total_tokens': 98, 'cache_read_input_tokens': 0, 'cache_creation_input_tokens': 0, 'cache_creation_input_tokens_above_1hr': 0}
Cost (USD): {'input_cost': 0.000116, 'output_cost': 0.0013800000000000002, 'total_cost': 0.0014960000000000002}
2026/11/30 17:34:44 INFO mlflow.tracing.export.async_export_queue: Flushing the async trace logging queue before program exit. This may take a while...

The output in a terminal: the token-cost experiment is created, a FutureWarning from protobuf about URL-safe Base64 appears, then token usage of 29 input, 69 output and 98 total tokens, and a cost of 0.000116 input, 0.00138 output and 0.001496 dollars in total.

The call used 29 input and 69 output tokens. MLflow priced them at Claude Opus 5.5's $4 and $20 per million tokens: $0.000116 + $0.00138 = $0.001496. The long digits are how Python prints these decimal fractions.

On Python 3.15, reading a trace back can print the FutureWarning from protobuf seen here. It doesn't affect the values: Fixed: "FutureWarning: invalid character '+' in URL-safe Base64 data will be discarded in future Python versions" When You Read MLflow Traces explains it.

Step 2: See the Tokens in the Trace Table​

Open the experiment's Traces page:

The Traces page of the token-cost experiment: one trace with the camping stove question, a duration of 1.8 seconds, state OK and 98 in the Tokens column.

The Tokens column shows each trace's total: 98.

Step 3: Open the Usage and Cost Breakdowns​

Open the trace and rest the mouse on 98 tokens in the top bar:

The trace view's top bar with a Usage breakdown popup: input tokens 29, cache read 0, cache write 0, output tokens 69, total 98.

Then rest it on the cost, ≈$0.0015:

The trace view's top bar with a Cost breakdown popup: input cost $0.000116, output cost $0.00138, total about $0.0015, and the note Cost is estimated from token usage and public model pricing.

The same numbers as in Python. Cache reads and writes get their own lines because models that cache prompts price them differently.

Step 4: See Totals on the Dashboard​

Click Dashboard in the experiment's sidebar, then scroll down on the Usage tab:

The Usage tab of the Dashboard: Token Usage 98 with 29 input and 69 output, Tokens per Trace 98 on average, Cost Breakdown about $0.001496 with claude-opus-5-5 at 100 percent, and Cost Over Time.

The dashboard adds up every trace in the time range: total tokens, tokens per trace, the cost by model or provider, and the cost per day. With more traces, this is where spending trends show.

MLflow's page says the cost charts are on the experiment's Overview tab and that cost tracking needs the server installed with the genai extra. In MLflow 3.18.0 the charts are under Dashboard, and a server installed without the extra still records costs, from a price table that ships with MLflow. I reported both as mlflow/mlflow#28299.

Common Mistakes​

MistakeWhat happensDo this instead
Calling get_trace right after the call without flush=TrueIt returns None and logs that the span data "is corrupted"Pass flush=True
Treating the cost as your billIt's an estimate from public pricesCheck your provider's console for the real charges

Troubleshooting​

SymptomLikely causeFix
The cost shows $0.00 while tokens are countedThe model name on the span isn't in MLflow's price tableSet the cost on the span yourself, as MLflow's token usage page shows
token_usage is NoneThe provider or API didn't report usage, for example a streamed callUse a non-streaming call, or set the usage on the span yourself

FAQ​

Where do the prices come from?​

From a price table bundled with MLflow, or LiteLLM's when that package is installed. The cost is computed when the trace is saved.

Can I see the usage of a single span?​

Yes. Each LLM span carries its own counts and cost, shown next to its name in the trace view.

Conclusion​

The traced Claude call used 98 tokens and cost about $0.0015, and MLflow showed it in four places: trace.info in Python, the Tokens column, the trace's usage and cost breakdowns, and the experiment's Dashboard.

Additional Resources​