Skip to main content

How to Get the History of a Metric in MLflow

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

When a run logs a metric many times, such as a loss after every round of training, MLflow keeps every value. The run's summary shows only the latest one, though. MlflowClient.get_metric_history returns them all, each with its step and the moment it was logged. This guide reads a short curve back and finds its best step, with real output from MLflow 3.18.0.

Syntax​

Syntax
from mlflow import MlflowClient

history = MlflowClient().get_metric_history("<run ID>", "<metric name>")
for point in history:
print(point.step, point.value, point.timestamp)
PartWhat it means
MlflowClient()MLflow's lower-level client. It talks to the tracking server you set with mlflow.set_tracking_uri() or MLFLOW_TRACKING_URI
get_metric_history(run_id, key)"Return a list of metric objects corresponding to all values logged for a given metric", in MLflow's words
"<run ID>"The run's ID, not its name. The run page shows it, and run.info.run_id holds it in code
point.stepThe step the value was logged at: the "x-coordinate" of the curve
point.valueThe number that was logged
point.timestampWhen it was logged, "as an integer (milliseconds since the Unix epoch)"

mlflow.get_run(run_id).data.metrics, by contrast, holds one value per metric: the latest.

Prerequisites​

Step 1: Log a Short Curve and Read It Back​

The example logs a falling "loss" at five steps, as a training loop would, then reads it back. Create metric_history.py:

The metric_history.py script in a macOS-style code window: it logs a loss of 0.92, 0.61, 0.47, 0.41 and 0.38 at steps 1 to 5 in an MLflow run, reads them back with MlflowClient().get_metric_history, prints each step, value and timestamp, finds the lowest loss with min, and prints the run's own metrics.

metric_history.py
import mlflow
from mlflow import MlflowClient

mlflow.set_tracking_uri("http://127.0.0.1:8080")
mlflow.set_experiment("history-demo")

with mlflow.start_run(run_name="history-demo") as run:
for step, loss in enumerate([0.92, 0.61, 0.47, 0.41, 0.38], start=1):
mlflow.log_metric("loss", loss, step=step)

client = MlflowClient()
history = client.get_metric_history(run.info.run_id, "loss")
for point in history:
print(f"step {point.step}: {point.value} (logged at {point.timestamp})")

best = min(history, key=lambda point: point.value)
print("Lowest loss:", best.value, "at step", best.step)
print("The run's own metrics keep only the latest:", client.get_run(run.info.run_id).data.metrics)
LineWhat it does
The with blockLogs loss five times, at steps 1 to 5. The values are made up to keep the example short; in real training they come from your model
client.get_metric_history(run.info.run_id, "loss")Reads back all five points of the run's loss
min(history, key=lambda point: point.value)Picks the point with the lowest value. For a score where higher is better, use max
client.get_run(...).data.metricsThe run's summary, for comparison

Step 2: Run It​

Terminal
uv run python metric_history.py
Output
2026/11/18 11:04:18 INFO mlflow.tracking.fluent: Experiment with name 'history-demo' does not exist. Creating a new experiment.
🏃 View run history-demo at: http://127.0.0.1:8080/#/experiments/2/runs/5001cfd7fe3040ca8c03faab17152385
🧪 View experiment at: http://127.0.0.1:8080/#/experiments/2
step 1: 0.92 (logged at 1794980058692)
step 2: 0.61 (logged at 1794980058707)
step 3: 0.47 (logged at 1794980058715)
step 4: 0.41 (logged at 1794980058722)
step 5: 0.38 (logged at 1794980058728)
Lowest loss: 0.38 at step 5
The run's own metrics keep only the latest: {'loss': 0.38}

All five points came back, each with its step and a timestamp in milliseconds; the five were logged within 36 milliseconds of each other. The run's summary kept only 0.38, the value from the last step.

Step 3: See the Same Curve in the UI​

Open the run with the View run link and click Model metrics. The chart draws the same five points, with Step along the bottom:

The Model metrics tab of the history-demo run in MLflow 3.18.0, with one line chart named loss falling from 0.92 at step 1 to 0.38 at step 5.

get_metric_history gives you the numbers behind that line, ready to compare, plot or test in code.

Common Mistakes​

MistakeWhat happensDo this instead
Reading run.data.metrics for a curveYou get one value per metric, the latestUse get_metric_history
Passing the run's name instead of its IDAn empty list, with no errorPass run.info.run_id, or the Run ID from the run page
Misspelling the metric nameAn empty list, with no errorCopy the name from the run's Model metrics tab
Assuming the lowest value is always bestFor scores such as R², higher is betterUse max for scores where higher is better

Troubleshooting​

SymptomLikely causeFix
[]The run ID or the metric name is wrong. MLflow returns an "empty list" for both rather than an errorCheck both against the run page
One point, at step 0The metric was logged once, without stepLog it with step= at each round of training
Timestamps like 1794980058692They're milliseconds since 1 January 1970 (UTC)Divide by 1,000 and use Python's datetime.fromtimestamp to read them as a date

FAQ​

Can I get the history of several metrics at once?​

get_metric_history reads one metric of one run. Call it once per metric, or once per run in a loop.

Is there a limit on how many points it returns?​

MLflow's reference for the method doesn't mention one. In every run I read for this series, the longest with 15 points per metric, it returned all of them.

Where else can I see the history?​

On the run's Model metrics tab, and on the experiment's chart view, which draws the same metric for several runs side by side: How to Compare Runs in the MLflow UI.

Conclusion​

MlflowClient().get_metric_history(run_id, "loss") returned all five values of a metric with their steps and timestamps, while the run's own summary kept only the latest. min picked the best step, and the run's Model metrics tab drew the same curve.

Additional Resources​