Sub topic 1
Why Long-Running Agent Tasks Break
Prerequisites
Assume you know basic Python, JSON, Markdown, and file-system concepts. You should also understand that an agent runs in a loop: it observes results, chooses an action, calls a tool, and repeats.
Why Long-Running Tasks Break
An agent's working memory is temporary. A context window can fill, a process can crash, a tool can time out, or a session can end. The work may also be interrupted after an external side effect, such as uploading a file or changing a record.
Without saved state, the next run cannot tell whether work finished. It may start over, skip unfinished work, or repeat an action that must happen only once. The original intent and decisions can also disappear with the session.
What State Has to Survive
A resumable task needs enough information for another process or agent to continue safely:
- The goal and input scope.
- The ordered steps and each step's status.
- Outputs created by completed steps.
- Decisions, assumptions, and reasons.
- External side effects, such as uploaded object names.
- Errors and unresolved questions.
- The next action to take.
A checkpoint usually stores machine-readable state, such as JSON. A progress file stores the same task's meaning in a form that a person can quickly inspect.
Breaking Work into Resumable Steps
Split a large task at boundaries where completion can be checked independently. A useful step has a clear input, one main result, and a visible success condition.
For example, a data task might use these steps:
- Fetch the source data.
- Transform the data.
- Validate the result.
- Publish the output.
Record completion after each step, rather than only at the end. This limits rework when an interruption occurs.
Making Steps Safe to Repeat
An idempotent step produces the same intended result when run more than once. Prefer operations that create or update a named result instead of blindly appending duplicates. Check whether the result already exists before creating it.
STEPS = ["fetch", "transform", "validate", "publish"]
def run(state):
for step in STEPS:
if step in state["completed"]:
continue
print(f"Running {step}")
state["completed"].append(step)
return state
state = run({"completed": ["fetch", "transform"]})
print(state)Here, restarting skips completed steps. Real steps must also inspect their outputs, because a recorded status can be stale or incomplete.
Exercise
Why should a task save state after each independently verifiable step?
Show answer
An interruption then loses at most the current step's work, and the next run can continue without repeating confirmed work.