The Core Idea: A long-horizon agent works for many steps, minutes or hours, mostly on its own. The challenges grow with length: errors compound, context overflows, processes crash and costs balloon. The answer is checkpoints, memory compaction, plans and budgets.
- Beginner: Autonomous Task Execution
An autonomous agent receives a goal and then works toward it without a person guiding each step: planning, using tools, checking its work and deciding when it is done. Examples include a coding agent that fixes a bug across many files, or a research agent that writes a long report.
| Short task | Long-horizon task |
|---|---|
| A few steps | Dozens to hundreds of steps |
| Fits in one context window | Exceeds the context window |
| Finishes in seconds | Runs for minutes or hours |
| One mistake is easy to fix | Mistakes compound over many steps |
| Little can go wrong | Crashes, rate limits and drift are likely |
Remember: Autonomy is earned. The longer an agent runs alone, the stronger its limits, checks and recovery must be.
- Beginner: Long-Running Tasks
A task that runs for a long time cannot live inside a single request. It needs the production design from the earlier lesson:
Tip: Design for interruption: the machine may restart at any time, so progress must never exist only in memory.
- Medium: Planning Over Many Steps
Over many steps, an agent loses the thread. Good long-horizon agents keep an explicit plan and a progress record:
| Technique | What it does |
|---|---|
| Written plan with checkboxes | Shows what is done and what remains |
| Replan at milestones | Adjust the plan when reality changes |
| Subgoals with checks | Verify each milestone before moving on |
| Periodic review | Compare progress with the original goal to avoid drifting |
| Verification steps | Run tests or checks, do not trust a claim of completion |
Drift: Agents can slowly wander away from the goal over many steps. Re-reading the goal and the plan at milestones is a simple cure.
- Medium: Persistent Memory and Compaction
The context window cannot hold hundreds of steps. The agent keeps recent details and compressed summaries of older work, and stores the full history outside the prompt for lookup.
In the code: 30 notes (450 tokens) were compacted into a summary plus the 5 most recent notes: 6 items and 91 tokens. The agent keeps the gist and loses the bulk.
| Memory layer | Holds |
|---|---|
| Prompt | The plan, recent steps and a summary |
| Notes file or database | Full details, findable by search |
| Checkpoint | The exact state for resuming |
- Advanced: Recovery From Failure, Budgets and Stop Conditions
Over long runs, failure is certain, so recovery is a core feature. Checkpoints save progress after each step, so a crash costs only the step that was running.
In the code: A 40-step task with a random crash chance of 4% per step. Without checkpoints, each crash restarts from step 1, and the run executed 66 steps (26 wasted). With checkpoints it executed 42 steps (only 2 wasted). The gap grows rapidly with task length.
| Stop condition | Example from the code |
|---|---|
| Step budget | stop: step budget used |
| Money budget | stop: money budget used |
| No progress | stop: no progress, ask a human |
| Time limit | Stop after a maximum duration |
Always keep a human exit: Long autonomous runs need a visible status, a pause button and a way to take over.
- Visual Explanation: Recovery, Compaction and Budgets
The figure compares a long run with and without checkpoints using the numbers from the code, then lists the four tools that keep long runs under control.
Click to see why waste grows with task lengthCompare ▼
Part 1 shows the wasted work in both cases. Part 2 lists plans, compaction, checkpoints and budgets. Open the bar for the math behind the waste.
- Python Implementation: Checkpoints, Memory Compaction and Stop Conditions
This script simulates a 40-step task that crashes randomly, with and without checkpoints, compacts 30 old notes into a short summary, and applies budget and no-progress stop conditions. The random numbers use a fixed seed so the output repeats.
import random def run_task(total_steps=40, crash_chance=0.04, checkpoints=True, seed=3, max_work=5000): rng = random.Random(seed) done = executed = crashes = 0 while done < total_steps and executed < max_work: executed += 1 if rng.random() < crash_chance: crashes += 1 if not checkpoints: done = 0 # no saved progress: start over from step 1 continue # with a checkpoint we only repeat the failed step done += 1 return executed, crashes for label, use in [("without checkpoints", False), ("with checkpoints", True)]: executed, crashes = run_task(checkpoints=use) print(label.ljust(20), "| steps executed:", executed, "| crashes:", crashes, "| wasted work:", executed - 40) # Persistent memory for long tasks: compact old notes into a short summarydef count_tokens(text): return round(len(text.split()) / 0.75) def compact(notes, keep_recent=5): if len(notes) <= keep_recent: return notes old, recent = notes[:-keep_recent], notes[-keep_recent:] summary = "Summary of " + str(len(old)) + " earlier steps: " + "; ".join(n.split(":")[0] for n in old[:3]) + ", ..." return [summary] + recent notes = ["step " + str(i) + ": collected facts about topic " + str(i) + " and saved the results" for i in range(1, 31)]before = sum(count_tokens(n) for n in notes)compacted = compact(notes)after = sum(count_tokens(n) for n in compacted)print("Notes before compaction:", len(notes), "items,", before, "tokens | after:", len(compacted), "items,", after, "tokens") # Budgets and stop conditions keep a long-running agent safedef should_stop(steps, max_steps, spent, max_spend, no_progress_steps, max_idle): if steps >= max_steps: return "stop: step budget used" if spent >= max_spend: return "stop: money budget used" if no_progress_steps >= max_idle: return "stop: no progress, ask a human" return "continue" print(should_stop(10, 100, 2.0, 5.0, 0, 8))print(should_stop(100, 100, 2.0, 5.0, 0, 8))print(should_stop(40, 100, 5.5, 5.0, 0, 8))print(should_stop(40, 100, 2.0, 5.0, 9, 8))Checkpointing removes almost all wasted work in this run, compaction shrinks the notes to a fifth of their size, and the stop function ends the run for each of its three reasons. The exact crash counts depend on the seed, but the pattern does not.
Knowledge Check
Key Points
The Big Picture
Plan → checkpoint → compact memory → verify milestones → stop on budget → keep a human in reach
Duration multiplies every weakness. Strong recovery and limits are what make autonomy usable.
Up Next: You now have the tools for reliable autonomy. In Advanced Agentic Systems you will see agentic RAG, computer-use and browser agents, multimodal agents, code agents and tool-rich agents.