Module 8 · Lesson 4 · 15 min read

Long-running tasks

Long-Running Tasks

Introduction

A task that takes hours or days will inevitably be interrupted or have to wait. In this lesson we learn the four pillars of long tasks: checkpoints, avoiding harmful repetition, budgets and progress reports, and we apply them to an agent that sends reminders and resumes after an interruption without repeating any message.

Learning objectives

  • Identify the problems specific to long-running tasks
  • Design checkpoints that allow resuming from the last state
  • Apply idempotency keys to actions with external effects
  • Set budgets for steps, time and cost with clear progress reports

When the task does not finish in a minute

Some tasks finish in a single reply, but autonomous agents are often given long-running tasks: reviewing hundreds of files, following up requests over several days, or gathering sources for research and then summarizing them. These tasks face problems that do not appear in short conversations:

  • Interruption: a server crash, a connection timeout, or a redeployment.
  • Waiting: a human approval that may take two days, or a slow external service.
  • Dangerous repetition: if we restart from the beginning, the agent may send the same message twice.
  • Drift: the longer the task, the greater the chance that the agent strays from the goal or consumes a large budget.

Four pillars of long tasks

  1. Checkpoints: after every important step the state (what has been done, what remains, and the intermediate results) is saved in a persistent store. This is what LangGraph calls durable execution and persistence, where a run can be resumed from the last saved state (according to the official documentation, reviewed 18 November 2026).
  2. Avoiding harmful repetition (Idempotency): every action with an external effect carries a unique key, so if it is executed a second time after resuming, it is ignored instead of repeating its effect.
  3. Budgets: a limit on steps, a limit on time and a limit on cost. When any limit is reached, the agent stops with a clear report instead of silently continuing.
  4. Progress reporting: the user sees the completion percentage, the last step and the reason for any stop.
Tool?Sensitive?DoneSTARTAgentnodeToolsnodeHuman approvalinterruptENDStatecheckpoint
A workflow with a state graph: nodes that read and modify the shared state, conditional edges that choose the path, and checkpoints that allow resuming.

Practical example: reminding students about missing documents

The scenario: the admissions unit has a list of students who are missing a document, and the agent prepares and sends a reminder to each student. The run may be interrupted halfway, and no student must receive two reminders. The following standard Python code saves the checkpoint in a JSON file and simulates an interruption followed by a resume:

python
# A long task with a checkpoint, resume and idempotency - standard Python
import json, os

CHECKPOINT = "reminders_checkpoint.json"
students = ["s101", "s102", "s103", "s104", "s105"]

def load_state():
    if os.path.exists(CHECKPOINT):
        with open(CHECKPOINT, encoding="utf-8") as f:
            return json.load(f)
    return {"sent": [], "steps_used": 0}

def save_state(state):
    with open(CHECKPOINT, "w", encoding="utf-8") as f:
        json.dump(state, f, ensure_ascii=False)

def send_reminder(student_id, state):
    key = "reminder-" + student_id                # idempotency key
    if key in state["sent"]:
        return "Skipped (already sent)"
    state["sent"].append(key)
    return "Reminder sent"

def run(max_steps=10, crash_after=None):
    state = load_state()
    for i, sid in enumerate(students):
        if state["steps_used"] >= max_steps:
            print("Stopped: the step budget ran out")
            break
        if crash_after is not None and i == crash_after:
            print("!! Sudden interruption")
            return
        print(sid, "->", send_reminder(sid, state))
        state["steps_used"] += 1
        save_state(state)                         # checkpoint after every step
    print("Progress:", len(state["sent"]), "of", len(students))

run(crash_after=3)     # the first run is interrupted after three students
run()                  # the resume: no reminder is repeated
os.remove(CHECKPOINT)

On resuming, the agent passes over the first three students, finds their keys in the saved state and skips them, then completes the rest. If we removed save_state, the resume would start with an empty state and send the reminders to the first three students a second time. Notice too that skipped steps also count against the budget; this is intentional, because every pass consumes time.

Long waits and resuming

When the agent needs human approval, it should not stay as a process suspended in memory for two days. The better design: it saves the state, marks it "awaiting approval" and stops, and when the approval arrives it resumes from the same point. This is exactly what frameworks that support persistence make easier, and we will go into detail in the human-in-the-loop lesson.

Summary

Long tasks face interruption, waiting, dangerous repetition and drifting from the goal. They rest on four pillars: checkpoints in a persistent store, idempotency keys for actions with effects, budgets for steps, time and cost, and understandable progress reports. When waiting for approval, the run is saved and stopped and then resumed later instead of staying suspended. Frameworks that support durable execution, such as LangGraph, make this pattern easier.

Terms

TermEnglishDefinition
CheckpointCheckpointA snapshot of the run state saved in a persistent store to resume from
IdempotencyIdempotencyA property that makes repeating the same action not change the effect after the first time
Idempotency keyIdempotency KeyA unique identifier that prevents the same action from being executed twice
BudgetBudgetA limit on steps, time or cost that stops the run when reached
ResumeResumeContinuing the run from the last checkpoint instead of starting again

Review questions

  1. What are the four problems that long-running tasks face?
  2. How does an idempotency key prevent a message from being sent twice after resuming?
  3. Why does the order between performing the effect and saving the state matter?
  4. How should the agent behave while waiting for an approval that may take two days?

References