How to Trace a Python Function with @mlflow.trace
MLflow can record any Python function you write, not only calls to a model: what went in, what came out and how long it took. You add one line, @mlflow.trace, above the function. This guide traces two small functions on your own MLflow server, one with its own name and one with a custom name, with real output from MLflow 3.18.0.
Syntax
import mlflow
@mlflow.trace
def <function>(<parameters>):
...
@mlflow.trace(name="<span name>")
def <function>(<parameters>):
...
| Part | What it means |
|---|---|
@mlflow.trace | A decorator: from now on, each call of the function below it is recorded |
<function>, <parameters> | Your own function, unchanged |
name="<span name>" | Optional. The name the recording gets in MLflow; without it, MLflow uses the function's name |
MLflow calls each recorded step a span, and a whole recorded call a trace. The mlflow.trace reference: "A decorator that creates a new span for the decorated function. [...] The span will automatically capture the input and output of the function." MLflow's manual tracing guide lists the name parameter "to override the span name from the default (the name of decorated function)".
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.
Step 1: Create a Project with MLflow
uv init --bare --python 3.15 tracing-examples
cd tracing-examples
uv add mlflow
uv init --bare creates a folder with only a pyproject.toml, and uv add mlflow installs MLflow 3.18.0 into the project. Don't call the project mlflow or mlflow-tracing: those are names of packages MLflow needs, and uv then installs an old MLflow or fails to build pyarrow.
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
The example works out prices: one function adds sales tax, the other totals a basket.
import mlflow
mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("trace-a-function")
@mlflow.trace
def add_tax(price: float, rate: float) -> float:
return round(price * (1 + rate), 2)
@mlflow.trace(name="basket_total")
def total(prices: list[float]) -> float:
return round(sum(prices), 2)
print(add_tax(19.99, 0.08))
print(total([4.5, 12.25, 3.0]))

mlflow.set_tracking_uri(...)sends the traces to the server from Step 2, andmlflow.set_experiment(...)files them under an experiment namedtrace-a-function, which MLflow creates if it's missing.@mlflow.traceonadd_taxrecords each call under the function's own name.@mlflow.trace(name="basket_total")ontotalrecords each call under the namebasket_totalinstead.
The functions themselves don't change: they take the same arguments and return the same values as without the decorator.
Step 4: Run It
uv run add_tax.py
2026/12/03 08:15:28 INFO mlflow.tracking.fluent: Experiment with name 'trace-a-function' does not exist. Creating a new experiment.
21.59
19.75
2026/12/03 08:15:29 INFO mlflow.tracing.export.async_export_queue: Flushing the async trace logging queue before program exit. This may take a while...

The two results print as usual: 19.99 with 8% tax is 21.59, and the basket adds up to 19.75. MLflow sends traces in the background, so its last line is it finishing that work before the program exits.
Step 5: Open the Traces
Open http://127.0.0.1:8080 in a browser, click trace-a-function, then Traces in the left sidebar:

Each call became its own trace. The Input column holds the function's arguments, by parameter name, and Output holds what it returned. Click the row with "price": 19.99 to open that trace:

The trace holds one span, named add_tax after the function, with its inputs, its output and its duration. Now open the other trace:

The function is called total, but its span is named basket_total, the name passed to the decorator. The Pretty view shows 3.0 as 3; the trace's JSON keeps the number as it was sent.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
No mlflow.set_tracking_uri(...) and no tracking server running | The traces aren't on the server you open in the browser | Set the URI to your server, such as http://127.0.0.1:8080 |
Writing @mlflow.trace("basket_total") | Python stops at the decorator line with AttributeError: 'str' object has no attribute '__name__' | Pass the name as a keyword: @mlflow.trace(name="basket_total") |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The Traces page is empty | The page shows another experiment | Click the experiment named in set_experiment |
| The traces appear a moment late | MLflow sends them in the background | Refresh the page; the "Flushing" line means they were on their way |
FAQ
Does @mlflow.trace change what my function returns?
No. The function gets the same arguments and returns the same value; MLflow only records the call.
What if one traced function calls another?
The inner call becomes a child span inside the outer call's trace, so you get one trace with a tree of spans instead of separate traces.
Do I need an LLM to use tracing?
No. Any Python function can be traced, as this guide does with two price calculations. Tracing is most useful in AI apps, where it shows each step an agent takes.
Conclusion
@mlflow.trace turned each call of add_tax and total into a trace, with the arguments as inputs, the return value as output and the time taken. The span was named after the function unless name= gave it another name, like basket_total.
Additional Resources
- mlflow.trace — the decorator and its parameters.
- Manual Tracing — MLflow's guide to tracing your own code.
- Span concepts — what a span records.
