Sub topic 3
Breaking Work into Resumable Steps
This chapter assumes you can read basic Python and JSON. It also assumes you know how an agent calls tools in a loop. A long task survives interruption only if it is split into steps with clear boundaries. Here you will learn how to choose those boundaries and how to make each step safe to run twice.
Why step boundaries matter
A resumable task is a list of steps. Each step has a name, an input, and a recorded outcome. After a crash, a runner reads the record, skips finished steps, and starts at the first unfinished one.
If the whole task is one large step, a crash at 90% means starting over. If it is split into small steps, a crash costs at most the step that was running.
Choosing good boundaries
A good step has these properties:
- One clear outcome. You can say in one sentence what is true after it finishes, such as "all pages have converted front matter".
- Checkable. You can test whether the outcome holds without trusting your memory.
- Small enough to redo. Losing one step's work is cheap.
- Ordered by dependency. A step runs only after the steps whose output it needs.
Split at points where state changes in the outside world, such as a file written, a record updated, or a message sent. Those are the places where a crash is most costly. Put each such change in its own step so the checkpoint can say whether it happened.
For work over many items, such as 42 pages, you can split again inside a step. Record progress per item, for example "18 of 42 done", so a restart continues at item 19 instead of item 1.
A runner loop
This loop uses the load_checkpoint and save_checkpoint functions from the checkpoint example in the next chapter. For now, only the control flow matters.
STEPS = ["fetch", "transform", "validate", "publish"]
def run(checkpoint_path, step_functions):
state = load_checkpoint(checkpoint_path) or {"completed": [], "notes": []}
for name in STEPS:
if name in state["completed"]:
continue
step_functions[name](state)
state["completed"].append(name)
save_checkpoint(checkpoint_path, state)
return stateThe checkpoint is saved after the step runs. This ordering has a consequence. If the process dies after step_functions[name](state) but before save_checkpoint, the step has run but is not recorded. On restart, the runner runs it again.
Steps must tolerate a second run
The gap described above cannot be closed completely, so each step must be safe to repeat. A step with this property is idempotent: running it twice leaves the same result as running it once.
| Step | Not safe to repeat | Safe to repeat |
|---|---|---|
| Save output | Append a line to a log file | Write the whole file to a fixed path |
| Create a record | Insert a new row | Insert or update by a unique key |
| Upload | Upload under a new random name | Upload under a name derived from the input |
| Send a notice | Send an email on each run | Check a "sent" marker first, then send |
When a step cannot be made idempotent, give it its own step and record the intent before acting. Then a restart can check whether the action already happened before it tries again.
Common mistakes
- Steps that are too large. A crash discards too much work.
- Steps that hide dependencies. Step 3 reads a temporary file that step 2 created and a restart deleted.
- Names that change. If you rename a step, old checkpoints no longer match. Keep step names stable.
- Progress kept only in memory. Anything the next run needs must be written to the checkpoint.
Exercise
A runner saves the checkpoint after each step. The "publish" step uploads a file and then the process crashes before the checkpoint is saved. What happens on restart, and what must be true of "publish" for this to be safe?
Show answer
The runner does not see "publish" as completed, so it runs it again. This is safe only if "publish" is idempotent, for example by uploading under a name derived from the input, or by checking whether the file already exists before uploading. Otherwise the restart creates a duplicate upload.