Sub topic 1
Why Long-Running Tasks Break
Prerequisites
This chapter assumes you have:
- Basic Python or comparable scripting experience.
- Familiarity with how LLM-based agents call tools in a loop.
- Comfort reading JSON and Markdown files.
- A basic understanding of the file system and processes.
You do not need to know anything about checkpoints yet. This chapter explains the problem they solve.
The core problem
A short task fits inside one run. The agent starts, calls tools, finishes, and exits. A long-running task does not fit. It can outlast a single context window, a single process, or a single working session. When any of those ends, everything that lived only in memory is gone.
The rest of this tutorial is about moving the important parts of a task out of memory and into files. This chapter covers why that matters.
Four ways a long task is interrupted
| Interruption | What is lost | Example |
|---|---|---|
| Context window fills up | Earlier instructions, decisions, and tool results | An agent converting 200 pages forgets the naming rule it chose on page 12 |
| Process crashes or is killed | All in-memory variables and the loop position | A laptop sleeps, a container restarts, or a timeout fires |
| Session ends | The conversation and any unstated plan | A user closes the terminal at the end of the day |
| Work changes hands | The reasoning behind the work | A second agent or a person takes over with only the output files |
These look different but share one cause. The only record of progress sits inside something temporary.
What happens without saved state
Suppose a four-step job runs: fetch, transform, validate, publish. The process dies during validation.
Without saved state, a restart has three poor options:
- Start over. This wastes time and may repeat side effects, such as publishing twice.
- Guess where it stopped. The guess may skip a step that did not finish.
- Ask the user to explain. This only works if the user remembers.
The failures that follow
Losing state leads to a small set of recurring problems. You will meet each of them again in later chapters.
- Lost work. Finished steps are redone because nothing records that they finished.
- Duplicated side effects. A step that sends an email, uploads a file, or charges a card runs a second time.
- Drift. After a context reset, the agent follows slightly different rules than before, so early and late output do not match.
- Silent corruption. A crash in the middle of writing a file leaves it half-written, and the next run reads it as valid.
- Lost intent. Another agent sees what was done but not why, and undoes a deliberate decision.
A small demonstration
The following script processes five items and crashes on the third. It keeps no state, so rerunning it starts at item one.
import random
ITEMS = ["a", "b", "c", "d", "e"]
def process(item, fail_on):
if item == fail_on:
raise RuntimeError(f"crashed on {item}")
print(f"processed {item}")
def main():
fail_on = random.choice(ITEMS)
for item in ITEMS:
process(item, fail_on)
if __name__ == "__main__":
main()Run it a few times. Each run repeats work from the start, and nothing tells you which items were already handled. With five items this is a nuisance. With five thousand items, each costing minutes or money, it is a real loss.
The approach in this tutorial
The fix has the same shape each time: write down what matters, outside the process, in a form that survives. The later chapters build this up in layers.
- Identify the state that must survive.
- Split the work into small steps that are safe to repeat.
- Save checkpoints between steps, written so a crash cannot corrupt them.
- Keep a human-readable progress file.
- Resume by reading saved state and checking it against reality.
- Hand off with a note that carries intent, not only results.
Exercise
A job runs fetch, transform, validate, and publish. It crashes after publish has started but before the run finishes, and there is no saved state. Name two problems a naive restart can cause.
Show answer
The restart may repeat fetch, transform, and validate, which wastes time. It may also run publish a second time, creating duplicated side effects such as duplicate files or messages. A restart that guesses the position could instead skip a step that never completed.