Skip to content
AI360XpertBeta

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.

A 24-step job checkpointed at six milestones resumes from the last save with 3120 tokens instead of 14400 after a crash
A 24-step job checkpointed at six milestones resumes from the last save with 3120 tokens instead of 14400 after a crash

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 tt. Write the saved state after step tt as StS_t, 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 tt as CtC_t, meaning the prompt the model actually reads. Write the action as ata_t, meaning the tool call plus its arguments, and the tool reply as oto_t, 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 MM and steps per milestone as kk, so total steps NN satisfies N=M×kN = M \times k.

Checkpoints bound how much you lose

A checkpoint is a write of StS_t to durable storage after a milestone finishes. Don't trust RAM alone. If the process dies, you load the last StS_t and continue from the next milestone.

Reliability is why this matters. Write per step success as qq, meaning the chance one step finishes without a timeout or bad tool reply. Assume steps fail independently with the same qq to keep the arithmetic clear. Then the chance a run of NN steps finishes in one go is

Psuccess=qNP_{success} = q^N

where PsuccessP_{success} means the chance of zero failures across all NN steps. With q=0.98q = 0.98 and N=24N = 24, Psuccess=0.9824≈0.616P_{success} = 0.98^{24} \approx 0.616. You finish cleanly about 62 times in 100 tries. Without checkpoints, the other 38 tries cost you the whole run. With a checkpoint every k=4k = 4 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 StS_t without redoing outside effects. If ata_t sent an email or charged a card, reloading and retrying repeats it. Persist receipts in StS_t, 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 NN. Write tokens per raw step as rr, meaning reasoning plus tool output kept verbatim. Then raw tokens after NN steps are N×rN \times r. That linear growth is what breaks long runs.

Compaction replaces old steps with short summaries. Write tokens per milestone summary as ss, meaning the distilled note you keep. After MM milestones you keep M×sM \times s summary tokens plus the recent raw steps you still need in full. If you keep the last milestone raw, that is k×rk \times r tokens. So kept tokens are

Ckept=M×s+k×rC_{kept} = M \times s + k \times r

where CkeptC_{kept} means the context you actually feed after compaction. You pay one summary call per milestone to buy a prompt that stays flat while NN 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 M=6M = 6 milestones with k=4k = 4 steps each. Then

N=M×k=6×4=24N = M \times k = 6 \times 4 = 24

Say each raw step costs r=600r = 600 tokens and each milestone summary costs s=120s = 120 tokens. Raw history would be

N×r=24×600=14400N \times r = 24 \times 600 = 14400

tokens. Kept context is

Ckept=M×s+k×r=6×120+4×600=720+2400=3120C_{kept} = M \times s + k \times r = 6 \times 120 + 4 \times 600 = 720 + 2400 = 3120

tokens. You feed 3120 tokens instead of 14400, a saving of 14400−3120=1128014400 - 3120 = 11280 tokens, about 78 percent smaller. Pair that with reliability: at q=0.98q = 0.98, Psuccess=0.9824≈0.616P_{success} = 0.98^{24} \approx 0.616. 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 MM, kk, rr, or ss 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.616

Change MM to 10 and watch CkeptC_{kept} 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 StS_t, 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.