Autonomous and Long-Horizon Agents

Learn how agents handle long and autonomous tasks: autonomous task execution, long-running work, planning over many steps, persistent memory with compaction, budgets and stop conditions, and recovery from failure with checkpoints.

30 minAdvancedCode Examples

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.

  1. 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 taskLong-horizon task
A few stepsDozens to hundreds of steps
Fits in one context windowExceeds the context window
Finishes in secondsRuns for minutes or hours
One mistake is easy to fixMistakes compound over many steps
Little can go wrongCrashes, 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.

  1. 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:

1
Run in the background: Workers and a queue, not an open browser tab.
2
Report progress: Show which step is running and what is done.
3
Allow pausing and cancelling: The user stays in control.
4
Notify at the end: Email, message or webhook when the result is ready.

Tip: Design for interruption: the machine may restart at any time, so progress must never exist only in memory.

  1. 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:

TechniqueWhat it does
Written plan with checkboxesShows what is done and what remains
Replan at milestonesAdjust the plan when reality changes
Subgoals with checksVerify each milestone before moving on
Periodic reviewCompare progress with the original goal to avoid drifting
Verification stepsRun 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.

  1. 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 layerHolds
PromptThe plan, recent steps and a summary
Notes file or databaseFull details, findable by search
CheckpointThe exact state for resuming
Risk
Summaries can drop important details. Keep the full log, and let the agent search it when something seems missing.

  1. 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 conditionExample from the code
Step budgetstop: step budget used
Money budgetstop: money budget used
No progressstop: no progress, ask a human
Time limitStop 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.

  1. 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.

Part 1 · Crash Recovery in the CodeSame task, same crashes
No checkpointsRestart from step 1
2 crashes in 40 steps
↓
Each crash throws away all progress
↓
66 steps executed, 26 wasted
With checkpointsResume from the last step
The same 2 crashes
↓
Only the failed step repeats
↓
42 steps executed, 2 wasted
Part 2 · Four Tools for Long RunsKeep the agent on track
Plan
Checklist
Know what is done.
Compaction
450 to 91 tokens
Keep the gist, drop the bulk.
Checkpoints
Resume
Never lose progress.
Budgets
Stop rules
Steps, money, no progress.
Click to see why waste grows with task lengthCompare ▼
If each step can crash with probability p, the chance of a run of n steps finishing without any crash is (1 - p) to the power n. For 40 steps at 4% that is only about 20%, so without checkpoints most runs restart several times.

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.

  1. 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.

long_horizon.pyPython 3.10+ · standard library only
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))
EXPECTED OUTPUT (your numbers may differ slightly)
without checkpoints | steps executed: 66 | crashes: 2 | wasted work: 26
with checkpoints | steps executed: 42 | crashes: 2 | wasted work: 2
Notes before compaction: 30 items, 450 tokens | after: 6 items, 91 tokens
continue
stop: step budget used
stop: money budget used
stop: no progress, ask a human

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

Question 1: Why are checkpoints more important for long-horizon agents?
Question 2: What is a main risk of summarizing old notes?

Key Points

✓Long-horizon agents run for many steps, mostly alone, so errors, context overflow and crashes become likely.
✓Run them in the background with progress reports, pause, cancel and notification.
✓Keep an explicit plan, verify milestones and re-read the goal to avoid drift.
✓Compact old steps into summaries and keep the full log searchable outside the prompt.
✓Checkpoint after every step so a crash costs only one step.
✓Set step, money, time and no-progress limits, and keep a human exit.

The Big Picture

Long-Horizon in One Line

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.