Skip to main content

How to Trace Claude API Calls with MLflow

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
import mlflow

mlflow.set_tracking_uri("<tracking server URL>")
mlflow.set_experiment("<experiment name>")
mlflow.anthropic.autolog()
PartWhat 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 .env file as ANTHROPIC_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​

Terminal
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:

Terminal 2
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​

trace_claude.py
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)

The trace_claude.py script in a macOS-style code window: MLflow's tracking URI and experiment are set, mlflow.anthropic.autolog() is called, then an ordinary Anthropic client asks claude-opus-5-5 one question and prints the answer.

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​

Terminal
uv run --env-file .env trace_claude.py
Output
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...

The output in a terminal: MLflow creates the claude-tracing experiment, Claude's one-sentence answer about paper maps prints, and MLflow flushes its trace logging queue before the program exits.

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:

The Traces page of the claude-tracing experiment: one trace from 21 seconds ago with the question as its input, the start of the reply's JSON as its output, a duration of 1.7 seconds, the state OK and 97 tokens.

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 view: a Messages.create span with claude-opus-5-5, 97 tokens and about $0.0015, the user's question and the max_tokens and model inputs, and the assistant's answer as output.

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​

MistakeWhat happensDo 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 serverSet the tracking URI first
Calling autolog() after the calls you want tracedCalls made before it aren't recordedCall it once at startup
Running without the key in the environmentThe call fails before anything is tracedRun with uv run --env-file .env

Troubleshooting​

SymptomLikely causeFix
The Traces page is emptyThe traces went to another tracking URI or experimentCheck the two set_... lines
The program waits a moment before exitingMLflow is flushing traces to the serverNormal; 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​