Sub topic 10
Handing work to another agent or person
This chapter assumes you can read the checkpoint and progress-file examples from earlier chapters and know how a resume routine skips finished steps. A handoff applies the same ideas to a new reader.
Why a handoff differs from a resume
When the same agent resumes, it still has habits, tooling, and a rough sense of the task. A new agent or person has none of that. They see only what you wrote down. Any fact that lives only in the first agent's context is lost at the handoff.
Handoffs happen for several reasons:
- The context window is full and a fresh session must take over.
- A different agent has the right tools or permissions for the next phase.
- A person needs to review or approve before work continues.
- A shift ends and someone else owns the task overnight.
Two layers of state
A handoff gives the receiver two kinds of information. They have different readers.
| Layer | File | Reader | Holds |
|---|---|---|---|
| Machine state | `checkpoint.json` | Code | Completed steps, IDs, offsets |
| Human state | `progress.md` and the handoff note | Agent or person | Intent, decisions, warnings, next action |
The checkpoint tells the receiver what the program did. It cannot tell the receiver why, or what to avoid. The handoff note covers that.
The handoff sequence
The sender stops cleanly, writes the note last, and only then releases the task. The receiver reads the note, then checks it against the real world before acting.
Generating the note from state
Write as much of the note as possible from the checkpoint. Generated parts cannot drift from the real state. Add the judgment parts by hand or by asking the sender for them.
import json
import os
import tempfile
def write_handoff(checkpoint_path, handoff_path, next_action, warnings):
with open(checkpoint_path) as f:
state = json.load(f)
done = ", ".join(state["completed"]) or "nothing yet"
lines = [
"# Handoff",
"",
"## Where things stand",
f"Completed steps: {done}",
"",
"## Do not repeat",
*[f"- {w}" for w in warnings],
"",
"## Next action",
next_action,
"",
]
directory = os.path.dirname(os.path.abspath(handoff_path))
fd, tmp_path = tempfile.mkstemp(dir=directory, suffix=".tmp")
with os.fdopen(fd, "w") as f:
f.write("\n".join(lines))
os.replace(tmp_path, handoff_path)
if __name__ == "__main__":
with open("checkpoint.json", "w") as f:
json.dump({"completed": ["fetch", "transform"], "notes": []}, f)
write_handoff(
"checkpoint.json",
"handoff.md",
"Run validate, then publish.",
["Do not re-run fetch; it rate-limits after one call per hour."],
)
print(open("handoff.md").read())The note uses the same temporary-file-then-replace pattern as the checkpoint. A receiver that starts while the sender is still writing then never reads a half-written note.
What the receiver does first
- Read the handoff note, then the progress file.
- Load the checkpoint.
- Check the claims in the note against the real system, for example by counting output files.
- Record in the progress file that the handoff was accepted, with the time and the new owner.
- Take the next action.
Step 4 matters. Without it, the sender cannot tell whether anyone picked the task up.
Common mistakes
- Writing the note before the final checkpoint. The note then describes a state that no longer exists.
- Assuming shared context. Phrases like "the usual config" or "as discussed" mean nothing to the receiver.
- No named owner. Two agents may both continue, or neither.
- Hiding unverified work. Marking work as done when it was never checked passes the sender's doubts to the receiver without telling them.
Exercise
Agent A finished a step, wrote the handoff note, and then ran one more step and saved a new checkpoint. Agent B now takes over. What goes wrong, and what is the correct order for Agent A?
Show answer
The note is stale. It does not list the last step, so Agent B may repeat it and duplicate its side effects. Agent A should finish its work, save the final checkpoint, update the progress file, and write the handoff note last. Agent B should still verify the note against the real system.