How to Trace Claude API Calls with MLflow
When your code calls Claude, you'd like to see afterwards exactly what was sent, what came back, how long it took and what it cost. MLflow records all of it as a trace, and for the Anthropic SDK one line turns that on. This guide traces one call to Claude on your own MLflow server, with real output from MLflow 3.18.0.
Syntax
import mlflow
mlflow.set_tracking_uri("<tracking server URL>")
mlflow.set_experiment("<experiment name>")
mlflow.anthropic.autolog()
| Part | What it means |
|---|---|
mlflow.set_tracking_uri(...) | Where the traces go: your tracking server, such as http://127.0.0.1:8080 |
mlflow.set_experiment(...) | The experiment that groups the traces; MLflow creates it if it's missing |
mlflow.anthropic.autolog() | From now on, every client.messages.create(...) call is traced |
MLflow's Anthropic tracing page: "By enabling auto tracing for Anthropic by calling the mlflow.anthropic.autolog function, MLflow will capture nested traces and log them to the active MLflow Experiment upon invocation of Anthropic Python SDK." It records the prompts and responses, latencies, the model name, settings such as max_tokens, tool calls, token usage, and any exception raised.
What It Costs
MLflow is free and open source, and here it runs on your own computer. The Claude API is paid: Claude Opus 5.5 costs $4 per million input tokens and $20 per million output tokens. The call in this guide used 97 tokens, about $0.0015, as the trace itself shows.
Prerequisites
- A Claude API key, in a
.envfile asANTHROPIC_API_KEY=.... - uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.
Step 1: Create a Project with MLflow and the Anthropic SDK
uv init --bare --python 3.15 trace-claude
cd trace-claude
uv add "mlflow[genai]" anthropic
MLflow's documentation recommends starting the server with the genai extra, which enables features such as the AI Gateway; the same project runs the server and the script. Put your .env file in this folder.
Step 2: Start the Tracking Server
In a second terminal, in the same folder:
uv run mlflow server --port 8080
Leave it running. How to Start an MLflow Tracking Server on Your Computer explains each line it prints.
Step 3: Write the Script
import anthropic
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("claude-tracing")
mlflow.anthropic.autolog()
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "In one sentence, why do hikers carry a paper map?"}],
)
print(message.content[-1].text)

Apart from the three MLflow lines at the top, it's an ordinary Anthropic SDK call. message.content[-1].text is the last block of the reply, the answer.
Step 4: Run It
uv run --env-file .env trace_claude.py
2026/11/30 17:29:36 INFO mlflow.tracking.fluent: Experiment with name 'claude-tracing' does not exist. Creating a new experiment.
Hikers carry a paper map because, unlike a phone or GPS, it never runs out of battery or loses signal, so it remains a reliable backup for navigating if their electronics fail.
2026/11/30 17:29:38 INFO mlflow.tracing.export.async_export_queue: Flushing the async trace logging queue before program exit. This may take a while...

MLflow sends traces in the background, so the last line is it finishing that work before the program exits.
Step 5: Open the Trace
Open http://127.0.0.1:8080 in a browser, click claude-tracing, then Traces in the left sidebar:

One call, one trace: the question, the start of the reply, 1.7 seconds and 97 tokens. Click the question to open the trace:

The trace holds one span, Messages.create, named after the SDK method. It shows the model, the tokens, the estimated cost, every input sent to Claude and Claude's answer.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Forgetting set_tracking_uri() | MLflow 3.18 writes the traces to an mlflow.db file in the folder you run from, not to your server | Set the tracking URI first |
Calling autolog() after the calls you want traced | Calls made before it aren't recorded | Call it once at startup |
| Running without the key in the environment | The call fails before anything is traced | Run with uv run --env-file .env |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The Traces page is empty | The traces went to another tracking URI or experiment | Check the two set_... lines |
| The program waits a moment before exiting | MLflow is flushing traces to the server | Normal; let it finish |
FAQ
Do I have to change my Claude code?
No. autolog() patches the SDK, so existing messages.create calls are traced as they are.
Does it trace async calls?
Yes. The Anthropic page lists async calls made with anthropic.AsyncAnthropic as supported.
Conclusion
Three lines at the top of the script sent one Claude call to MLflow as a trace, with the question, the answer, the model, 97 tokens and an estimated $0.0015, all visible on the Traces page.
Additional Resources
- Tracing Anthropic — what autologging records and which APIs it supports.
- MLflow Tracing — traces, spans and everything built on them.
