TensorFlow
Saver
Model Limit
Machine Learning
AI Libraries

TensorFlow Saver has 5 models limit

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

TensorFlow does not have a hard limit of five models. What people usually mean is that TensorFlow's checkpoint retention defaults to five saved checkpoints in a rolling window. In TensorFlow 1 this is controlled by Saver(max_to_keep=5), and in TensorFlow 2 the same idea is handled by CheckpointManager.

What the Default of Five Actually Means

In TensorFlow 1 style code, tf.compat.v1.train.Saver keeps the most recent checkpoints according to max_to_keep. The default is five, which is why older checkpoints disappear during training unless you change that value.

python
1import tensorflow as tf
2
3
4tf.compat.v1.disable_eager_execution()
5
6w = tf.compat.v1.get_variable("w", initializer=0.0)
7update = tf.compat.v1.assign_add(w, 1.0)
8saver = tf.compat.v1.train.Saver(max_to_keep=5)
9
10with tf.compat.v1.Session() as sess:
11    sess.run(tf.compat.v1.global_variables_initializer())
12    for step in range(10):
13        sess.run(update)
14        path = saver.save(sess, "./ckpt/model", global_step=step)
15        print(path)

After enough saves, only the newest checkpoint files remain in the active retained set. That is a retention policy, not a model-count ceiling.

Change the Retention Policy Intentionally

If you need more recovery points, raise max_to_keep.

python
saver = tf.compat.v1.train.Saver(max_to_keep=20)

If you need fewer, reduce it. The right number depends on how often you save, how expensive training is, and how much storage you can afford.

Some TensorFlow 1 documentation also notes that None or 0 changes deletion behavior, but that still does not create a five-model hard limit. It just changes how checkpoint bookkeeping works.

The TensorFlow 2 Approach

Modern TensorFlow code should use tf.train.Checkpoint together with tf.train.CheckpointManager.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([tf.keras.layers.Dense(1)])
4optimizer = tf.keras.optimizers.Adam(1e-3)
5
6ckpt = tf.train.Checkpoint(step=tf.Variable(1), optimizer=optimizer, model=model)
7manager = tf.train.CheckpointManager(ckpt, directory="./tf2_ckpt", max_to_keep=5)
8
9for _ in range(8):
10    ckpt.step.assign_add(1)
11    path = manager.save()
12    print("saved", path)
13
14print("latest", manager.latest_checkpoint)

CheckpointManager makes the retention policy explicit. According to the TensorFlow API docs, it keeps some checkpoints and deletes unneeded ones, oldest first, until only max_to_keep remain in the active set.

Keep Best and Rolling Checkpoints Separate

A rolling retention count is good for crash recovery, but it is not the same thing as preserving the best model by validation metric. If training quality fluctuates, the best-performing checkpoint may be older than the newest five.

A practical setup is:

  • rolling checkpoints for recent recovery
  • a separate "best model" checkpoint when validation improves
  • optional long-term milestone snapshots for audits or reproducibility
python
1best_loss = float("inf")
2current_val_loss = 0.12
3
4if current_val_loss < best_loss:
5    best_loss = current_val_loss
6    ckpt.write("./best/model")

This prevents a good model from being deleted simply because later checkpoints were saved afterward.

Storage and Restore Planning

Checkpoint retention is an operational decision, not just a code parameter. Large models can consume significant disk space. If you increase retention aggressively, also plan for storage monitoring and cleanup policies.

More importantly, test restoration regularly.

python
1latest = manager.latest_checkpoint
2if latest:
3    ckpt.restore(latest)
4    print("restored", latest)

A checkpoint strategy is only useful if restore actually works during recovery.

Common Pitfalls

The biggest misunderstanding is treating the default value of five as a hard TensorFlow limit. It is only the default retention count.

Another issue is increasing max_to_keep without considering storage growth. More checkpoints improve recovery history, but they also cost disk space.

Teams also sometimes rely only on rolling checkpoints and forget to preserve the best-performing model separately.

Finally, be careful when mixing TensorFlow 1 and TensorFlow 2 checkpoint styles in the same project. The retention idea is similar, but the APIs and object models are not the same.

Summary

  • TensorFlow does not have a hard limit of five models.
  • In TensorFlow 1, Saver defaults to max_to_keep=5.
  • In TensorFlow 2, CheckpointManager provides the same retention concept.
  • Retention count should match recovery needs and storage budget.
  • Keep best-model checkpoints separately from the rolling checkpoint window.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.