How to See the Token Usage and Cost of LLM Calls in MLflow
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
trace = mlflow.get_trace(<trace ID>, flush=True)
trace.info.token_usage
trace.info.cost
| Part | What it means |
|---|---|
mlflow.get_trace(<trace ID>, flush=True) | Loads a trace; flush=True first waits for traces still being sent |
trace.info.token_usage | A dictionary of input_tokens, output_tokens and total_tokens, plus cache counts for models that report them |
trace.info.cost | A 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:8080and the script below, which traces one Claude call withmlflow.anthropic.autolog().
Step 1: Trace a Call and Read Its Usage in Python
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)

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.
uv run --env-file .env token_cost.py
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 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 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:

Then rest it on the cost, ≈$0.0015:

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 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
| Mistake | What happens | Do this instead |
|---|---|---|
Calling get_trace right after the call without flush=True | It returns None and logs that the span data "is corrupted" | Pass flush=True |
| Treating the cost as your bill | It's an estimate from public prices | Check your provider's console for the real charges |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The cost shows $0.00 while tokens are counted | The model name on the span isn't in MLflow's price table | Set the cost on the span yourself, as MLflow's token usage page shows |
token_usage is None | The provider or API didn't report usage, for example a streamed call | Use 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
- Token Usage and Cost Tracking — accessing usage and cost, and setting them yourself.
- mlflow/mlflow#28299 — the page's notes on the
genaiextra and the Overview tab.
