Long-Horizon Agents
An agent that keeps going for hours without losing the plot. It writes down where it is, trims its notes, and rechecks the plan after every milestone.
Why Does This Exist?
You have felt the short loop work. You ask for a summary, the agent calls one search tool, reads the result, and answers. That fits in a few steps and one prompt.
Stretch that job to an afternoon and the same loop starts to fail in three concrete ways. Say you ask for a sweep across 30 papers with notes and a comparison table, or a migration that touches 40 files. First, the prompt swells. Every step appends reasoning plus tool output, so step 24 carries steps 1 through 23. Attention thins and early instructions slip, which is context degradation. Second, memory lives in RAM. One timeout or one expired key kills the process and its history, so you restart from zero. Third, an early misread compounds. If step 2 picks the wrong file set, steps 3 through 24 polish the wrong set.
This page is about the outer loop that keeps a long run alive: a milestone plan, a checkpoint after each milestone, a trimmed context, and a replan check. The parts have their own pages. Context compaction covers the trimming, episodic and semantic memory covers what lasts for weeks, planning and reasoning covers how plans get made, and human-in-the-loop covers where you approve.
You already know the short loop underneath. An agent reads state, picks an action, runs a tool, reads the observation, and repeats. The full setup is in agent architecture and the reason plus act rhythm is in ReAct pattern. Long horizon work keeps that rhythm and adds a safety net around it.
Think of It Like This
Renovating a kitchen over two weeks
You and a friend agree to renovate a small kitchen over two weeks while you still live in the house.
You do not carry the whole job in your head. You tape a work list to the fridge with six milestones: strip, wiring, plumbing, cabinets, tiles, paint. Each evening you write one page in a notebook: what got done, what is open, where the spare tiles sit. You take phone photos of the wiring before you close the wall. Each morning you read yesterday's page, not the whole two weeks, and you pick the next milestone. If power fails on day nine, you don't restart the kitchen. You open the notebook, look at the last photo, and resume from the last finished milestone.
That maps point for point. The work list is the plan. The notebook page plus photos is the checkpoint. Reading one page instead of the whole history is compaction. The morning check is replanning.
Where it stops: your notebook pages are exact and your photos don't invent outlets. An agent summary is lossy and can drop an ID or a count. And you would not reinstall the cabinets twice just to resume. An agent will repeat an outside action unless you make actions safe to retry or gate them.
How It Actually Works
At each step the agent sees a context, picks an action, and reads an observation. Write the step index as . Write the saved state after step as , meaning the plan, the summaries so far, pointers to artifacts like files or drafts, and receipts from tools such as IDs. Write the context tokens fed to the model at step as , meaning the prompt the model actually reads. Write the action as , meaning the tool call plus its arguments, and the tool reply as , meaning the observation. A milestone is a chunk of steps with its own check, like finishing notes for five papers. Write the count of milestones as and steps per milestone as , so total steps satisfies .
Checkpoints bound how much you lose
A checkpoint is a write of to durable storage after a milestone finishes. Don't trust RAM alone. If the process dies, you load the last and continue from the next milestone.
Reliability is why this matters. Write per step success as , meaning the chance one step finishes without a timeout or bad tool reply. Assume steps fail independently with the same to keep the arithmetic clear. Then the chance a run of steps finishes in one go is
where means the chance of zero failures across all steps. With and , . You finish cleanly about 62 times in 100 tries. Without checkpoints, the other 38 tries cost you the whole run. With a checkpoint every steps, a crash costs at most the current milestone, which is 4 steps, not 24. That bound is the whole point.
The assumption underneath: you can resume from without redoing outside effects. If sent an email or charged a card, reloading and retrying repeats it. Persist receipts in , make writes idempotent when you can, and gate risky calls. That gating is human-in-the-loop.
Compaction bounds how much you feed the model
Raw history grows with . Write tokens per raw step as , meaning reasoning plus tool output kept verbatim. Then raw tokens after steps are . That linear growth is what breaks long runs.
Compaction replaces old steps with short summaries. Write tokens per milestone summary as , meaning the distilled note you keep. After milestones you keep summary tokens plus the recent raw steps you still need in full. If you keep the last milestone raw, that is tokens. So kept tokens are
where means the context you actually feed after compaction. You pay one summary call per milestone to buy a prompt that stays flat while grows. The trade is lossy memory. A summary can drop an ID, a number, or a caveat, so keep receipts and artifact pointers verbatim and summarize prose only. The full trimming pattern is context compaction, and what deserves to last for weeks is episodic and semantic memory.
Worked example: 6 milestones, 24 steps
Take milestones with steps each. Then
Say each raw step costs tokens and each milestone summary costs tokens. Raw history would be
tokens. Kept context is
tokens. You feed 3120 tokens instead of 14400, a saving of tokens, about 78 percent smaller. Pair that with reliability: at , . One crash without checkpoints wastes up to 24 steps. With checkpoints it wastes at most 4.
Replan after each milestone. Compare the finished milestone to its check, such as five notes with quotes and page numbers, then revise the remaining milestones before you continue. If the check fails, fix the plan now. Polishing a wrong plan for three more milestones is how long runs burn budgets.
Skip all of this when the job is short. If it fits in about ten steps in one session, a plain loop is cheaper and simpler. Checkpoint writes, summary calls, and plan checks add latency and new ways to fail. Use the heavy loop when hours, many tools, or restarts are part of the job.
Code
The point of this snippet is the budget check. It recomputes the token math and the one go success chance from the worked example, so you can change , , , or and see what breaks first.
def raw_tokens(num_steps: int, per_step: int) -> int: return num_steps * per_step
def kept_tokens(num_milestones: int, per_summary: int, recent_steps: int, per_step: int) -> int: return num_milestones * per_summary + recent_steps * per_step
def success_prob(per_step_success: float, num_steps: int) -> float: return per_step_success**num_steps
M = 6K = 4N = M * KR = 600S = 120
raw = raw_tokens(N, R)kept = kept_tokens(M, S, K, R)print(raw) # -> 14400print(kept) # -> 3120print(round(success_prob(0.98, N), 3)) # -> 0.616Change to 10 and watch stay near 3600 while raw passes 24000. That gap is why long runs need the extra writes.
Watch Out For
Polishing the wrong plan for hours
Symptom: every tool call succeeds and the token meter climbs, but the final artifact answers the wrong question. Say milestone 1 picks the wrong paper set or the wrong table schema. Milestones 2 through 6 then do careful work on that wrong base. You only notice at the end, after hours.
Fix: approve the plan early and check each milestone against a written check. Pause after milestone 1 or 2 for a human yes, or keep going with a strict auto check, such as five notes with quotes and page numbers. If a milestone fails its check, replan the rest before you spend more. That pause is human-in-the-loop, and scoring the run is agent evaluation.
Resume repeats an outside action
Symptom: after a crash and resume, the agent sends the same message twice, files the same ticket twice, or overwrites a good file with a stale retry. Or it fails with file not found because the checkpoint kept prose but not the file pointer.
Fix: store receipts, not just words. Save tool IDs, file paths, and artifact versions in , keep those fields verbatim through compaction, and make writes idempotent when you can, such as upsert by ID instead of create. Gate risky tools so a retry needs approval. Test resume on purpose: kill the run after milestone 2 and confirm milestone 3 continues without duplicates.
The Quick Version
- A long horizon agent survives long jobs with a milestone plan, a checkpoint write after each milestone, a trimmed context, and a replan check.
- A 24 step run at 98 percent per step success finishes in one go about 62 times in 100 tries, and one crash without checkpoints costs all 24 steps.
- Six milestones of four steps shrink context from 14400 raw tokens to 3120 kept tokens: six 120 token summaries plus the last four raw steps at 600 tokens each.
- Checkpoints assume retry safe actions. Persist tool IDs and file pointers verbatim and gate risky calls, or a resume repeats outside effects.
- Short jobs don't need this machinery. A plain loop is cheaper and simpler when the work fits in about ten steps in one session.