How the Modal Image build cache decides what to rebuild
By Christophe Paka · Facts checked on 7 November 2026
You change one line in the Image of your Modal function. An Image is the list of software the cloud machine needs: the operating system, Python and your packages. Modal builds it once, then starts containers from it; a container is one running copy of that machine. You run again, and the build starts over from the big package install. Why? The answer is the Modal Image build cache and the order of your chain.
A rebuild of a large package such as torch is time you wait before your code runs. Get the order wrong, and you wait on every tweak.
The figure below shows the chain as a layer cake, with one edit in the middle.

Use this diagram on your site
Free to share in your blog post, docs, course or slides, as long as the credit link stays. Paste the code below where the figure should go.
How to read this figure
Read the stack from the bottom. Layer 1,
modal.Image.debian_slimwith a pinned Python, is built first. Layer 2 adds OS packages withapt_install, here git. Each chained method call adds one layer: one cached step of the Image.Find the line you edit. The arrow points at layer 3,
uv_pip_install("torch<3"), which installs Python packages. This is the edit. Everything else in the chain stays as it was.See what rebuilds. The bracket marks the edited layer and every layer above it,
envandrun_commands, as rebuilt. A layer depends on everything under it, so the cache can trust only layers that sit on an unchanged base.See what the cache serves. Layers 1 and 2 did not change, and nothing under them changed. They come from the cache, reused at once. The bottom of the cake is the cheap part to keep.
Look at the floating box. Your code and files, added with
add_local_diroradd_local_file, are not baked layers. Modal copies them in fresh each time a container starts, so a code edit rebuilds nothing. In the drawing, that note overlaps the word rebuilt; it says edits to your code never rebuild. Imports are a separate rule: see the local and remote split.
Key takeaways
- Order the chain from stable to busy: base and OS packages first, big stable packages next, shell tweaks last.
- Keep a step you edit daily, such as a run_commands line, above torch, never below it.
- Bring your own code in with add_local_dir, and pass copy=True only when a later build step must see it.
Try this: build a data science Image and watch the cache
You need a terminal with the Modal client signed in. No GPU. Each run shows its build logs, so you can see which layers build.
- Create
ds_image.pywith a chain ofdebian_slim(python_version="3.13")anduv_pip_install("pandas==2.2.0", "numpy"), then runmodal run ds_image.py. The first run builds both layers. - Run it again, unchanged. There is no build output: every layer comes from the cache.
- Add
.env({"REPORT_MODE": "draft"})at the end of the chain and run. Only the new layer builds. - Edit
"pandas==2.2.0"to"pandas"and run once more. The package layer rebuilds, and so does theenvlayer you added on top of it in step 3.
Expected result: The top edit cost one cheap layer; the lower edit cost the packages too. You can now predict which layers an edit rebuilds before you press Enter.
Questions readers ask
Do I have to write a Dockerfile for Modal?
No. You build an Image by chaining methods in Python, each one a build step: a base, OS packages, Python packages, settings and shell commands. Attach the Image to a function with image=, and only that function gets it.
Why does editing my own code not trigger a rebuild?
Files added with add_local_dir or add_local_file are attached when a container starts, not baked into a layer. Pass copy=True only when a later build step must see them. Those files then become a layer, and every edit rebuilds it and everything above.
Where do my model weights go so starts are fast?
One choice is the Image. run_function runs one of your Python functions as a build step and saves what it wrote to disk into the Image. The build cache covers this step too, so the download runs once. The other home is a Volume that many containers read.
Does a Modal Image rebuild cost money?
Modal's Images guide does not say whether build time bills, so check the Usage page after your first build. What you can control is how often it happens: order the chain from stable to busy, and pin package versions so a rebuild gives you the same result each time.
Sources: Images guide (Modal docs).
This figure is from Modal Made Crystal Clear, chapter 4.
The chapter builds the idea step by step, with recipes you can follow, a Try it to check yourself, and every source.
Get the book


