How to Resume an Interrupted Long-Running AI Agent Task: Checkpoints, Context Rebuild, and Recovery Workflow

A practical guide for recovering interrupted AI Agent tasks: what to save before a long run, how to rebuild context after a crash or context reset, and how to turn recovery notes into a reusable workflow.

When an AI Agent runs a long task, the painful part is often not the failure itself. It is the moment after the interruption: the terminal is gone, the context window has been reset, the model only remembers half of the plan, and you are no longer sure which files were changed.

The practical answer is not to hope the Agent never breaks. It is to design the task so it can be resumed.

This article focuses on a very concrete question: when an AI Agent is interrupted during a long task, how do you recover it without starting from scratch?

Quick Answer

To make long AI Agent tasks recoverable, keep three things visible throughout the run: a task goal, a checkpoint list, and a current workspace state. After interruption, first inspect git status, changed files, logs, and generated artifacts; then rebuild the task context from the latest checkpoint; finally ask the Agent to continue only from the unfinished items.

In one sentence: do not let the Agent rely only on chat memory. Let the repository and checkpoint notes become the memory.

Why Long Agent Tasks Are Easy to Break

Long-running Agent tasks usually fail for a few common reasons:

  • the model context becomes too long;
  • a command times out or the terminal session disappears;
  • the user opens a new conversation without enough background;
  • the Agent forgets which files it already touched;
  • generated files and source files are mixed together;
  • tests fail midway and the next step is unclear.

The risk increases when the task includes writing content, editing code, running tests, generating translations, or touching multiple directories.

For example, after building a reusable workflow with Codex Skills, you may ask the Agent to process many posts in sequence. If it stops halfway, you need a clear way to know which posts are finished and which ones are still pending.

The Minimum Recovery Structure

A resumable Agent task needs a small but explicit structure.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
Goal:
- What exactly should be finished?

Scope:
- Which directories or files may be changed?

Progress:
- Done
- In progress
- Pending

Verification:
- Which commands or checks prove the work is usable?

Recovery note:
- If interrupted, where should the next Agent continue?

This does not need to be a large project document. A short checklist in the chat, a temporary Markdown file, or a task-specific SKILL.md is enough.

The key is that the state must be reconstructable outside the model’s memory.

Step 1: Inspect the Workspace First

After an interruption, resist the urge to immediately say “continue.” First inspect the repository.

Useful commands:

1
2
3
git status --short
git diff --stat
git diff -- path/to/file

For generated content, also list the target directories:

1
ls content/post/2026/07/38

For code tasks, check recent test output, build artifacts, and any files the Agent mentioned before the interruption.

The goal of this step is simple: separate completed work from half-written work.

Step 2: Rebuild a Compact Context

When starting a new Agent session, give it a compact recovery prompt instead of pasting the entire old conversation.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
Continue the interrupted task.

Goal:
- Translate posts 36, 37, and 38 into en, zh-tw, ja, and es.

Already completed:
- 36: all target files created.
- 37: all target files created.

Still pending:
- 38: create index.en.md, index.zh-tw.md, index.ja.md, index.es.md.

Rules:
- Preserve source date and slug.
- Skip existing target files.
- Do not commit.

Verification:
- Check target files exist.
- Check date/slug match source.
- Check front matter and code fences are valid.

This kind of prompt gives the Agent enough state to continue without re-litigating the whole task.

Step 3: Continue From the Next Small Unit

Do not restart the entire long task unless you have to. Resume from the next small unit.

Good units include:

  • one article;
  • one translation language;
  • one test file;
  • one module;
  • one batch of generated assets;
  • one command plus its verification.

For AI coding tasks, this is especially important. If you also use hooks to run tests automatically, as in Claude Code hooks automatic test runs, keep the recovery unit small enough that a failing test points to a clear file or behavior.

Step 4: Ask the Agent to Verify Before Moving On

The safest resume pattern is:

1
Inspect current state, finish only the pending part, then verify before doing anything else.

For content tasks, verification may include:

  • file existence;
  • front matter fields;
  • slug/date consistency;
  • internal link syntax;
  • Markdown code fences;
  • no obvious encoding damage.

For code tasks, verification may include:

  • targeted unit tests;
  • type check;
  • lint;
  • a narrow build;
  • git diff review.

The Agent should not treat “file exists” as the same as “task completed.”

What to Save Before Starting a Long Task

Before a long Agent task begins, it helps to save:

  • the exact goal;
  • the allowed paths;
  • the forbidden paths;
  • the batch order;
  • the verification command;
  • the rollback or recovery rule.

For example:

1
2
3
4
5
Process posts 36, 37, 38 in that order.
Only create missing target language files.
Do not edit existing translated files.
After each post, verify date and slug.
If interrupted, continue from the first post with missing targets.

This single paragraph can save a surprising amount of cleanup later.

How to Handle Context Window Pressure

Long tasks often fail because the prompt becomes too large. The solution is to compress context aggressively.

Keep:

  • current objective;
  • changed file list;
  • decisions already made;
  • pending items;
  • verification results.

Drop:

  • repeated discussion;
  • old failed attempts after the fix is known;
  • verbose command output;
  • unrelated file content;
  • speculative plans that were not used.

If token usage itself becomes a problem, the same idea applies to debugging. Keep only the facts needed for the next action. I discussed a related symptom in Claude Code token spike troubleshooting.

When to Use a Skill or Workflow File

If the same recovery pattern appears again and again, turn it into a workflow.

A reusable workflow can define:

  • how to discover source files;
  • how to decide what is already done;
  • what must be skipped;
  • how to preserve metadata;
  • how to verify the result;
  • what the final report should include.

This is where a local skill becomes useful. Instead of relying on a long prompt every time, you encode the recovery rules once and reuse them.

For cross-tool handoff, the idea is similar. If Codex starts the task and Claude Code finishes it, or the other way around, use an explicit checkpoint. I covered this direction in Codex and Claude Code task handoff.

A Practical Resume Prompt Template

You can reuse this template:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
This is a resumed AI Agent task.

Objective:
- ...

Source of truth:
- Repository files and git status are more reliable than prior chat memory.

Allowed changes:
- ...

Already done:
- ...

Pending:
- ...

Rules:
- ...

Verification:
- ...

Please inspect the current workspace first, continue only the pending items, and report exactly what changed.

The most important line is the source-of-truth rule. It prevents the Agent from confidently following stale memory when the workspace says something else.

Common Mistakes

The most common recovery mistakes are:

  • saying only “continue” without state;
  • asking the Agent to redo everything;
  • ignoring git status;
  • mixing generated files with source edits;
  • skipping verification because the task “looks done”;
  • letting the Agent edit unrelated files while recovering context.

Recovery is not just about continuing. It is about narrowing the task until continuing is safe.

Conclusion

An interrupted AI Agent task is not automatically a failed task. It becomes a failed task only when there is no external state to recover from.

For long tasks, keep a goal, a checkpoint list, and a verification rule. After interruption, inspect the workspace first, rebuild a compact context, resume from the next small unit, and verify before expanding the scope again.

Once this becomes a habit, long Agent work feels much less fragile. The Agent can forget, the terminal can close, and the conversation can restart, but the task still has a trail to follow.

记录并分享
Built with Hugo
Theme Stack designed by Jimmy