Sub topic 10
Handing Work to Another Agent or Person
This chapter assumes you can read Python, JSON, and Markdown. It also assumes you know how an agent calls tools in a loop, and that you have seen checkpoints and progress files from earlier chapters. Here the question is what changes when the next reader is not the process that wrote the state.
Why a handoff is different from a resume
A resume happens when the same program, with the same code and the same assumptions, restarts after a crash. A handoff happens when a different reader takes over. That reader may be:
- A new agent session with an empty context window.
- A different agent with other tools or permissions.
- A person who was never watching the run.
- You, three weeks later.
A resuming process can rely on shared assumptions. It knows what the checkpoint keys mean, and it knows which functions wrote them. A new reader knows none of this. Anything not written down is lost.
The checkpoint file gives machine state: which steps finished. The progress file gives intent: the goal, the decisions, and the plan. A handoff adds a third thing, a snapshot written for a specific reader at a specific moment. It says what is true now, what is uncertain, and what to do next.
When to write a handoff
Write one at every point where the reader may change, not only at the end. Common triggers:
- The context window is nearly full and the session will be replaced.
- A scheduled job ends and another will pick up later.
- Work moves between agents with different roles, such as a planner and an implementer.
- A person reviews the work and takes over a manual step.
- You stop for the day.
If you can predict the trigger, write the note before it. A context window that is almost full is a poor place to compose careful prose, and a crash gives you no chance to write at all. For that reason, keep the progress file current during the run and treat the handoff as a short, final layer on top of it.
What a good handoff contains
A reader with no context needs six things.
- Where things stand. State facts about the world, such as "months 2023-01 through 2023-08 are exported". Avoid vague summaries like "most of the export is done".
- What is not verified. Separate what you confirmed from what you assume. This section matters most, because the reader will otherwise trust everything equally.
- What not to repeat. Name steps with side effects that must not run twice, and say why.
- The next action. Give one concrete step, not a list of options.
- Where to look. Point to the files that hold full state, so the note stays short.
- Who and when. Record the author and the time, so the reader can judge how stale the note is.
Here is an example note:
# Handoff: payment export job
**From:** agent-A **To:** agent-B **Time:** end of session 3
## Where things stand
Export of months 2023-01 through 2023-08 is done. Output is in out/ and listed in checkpoint.json.
## Not yet verified
The row count for 2023-08 was not compared with the source database.
## Do not repeat
The upload step already ran for 2023-01 to 2023-06. Re-running it creates duplicate files.
## Next action
Verify the 2023-08 row count, then export 2023-09.
## Where to look
progress.md for decisions, checkpoint.json for machine state.Notice what the note leaves out. It does not retell the session or explain every attempt. History that does not change the next action belongs in the progress file, if anywhere.
Facts, claims, and guesses
Label the confidence of each statement. A useful habit is to use three levels:
| Level | Meaning | Example |
|---|---|---|
| Verified | You checked it against the real system in this session | "Upload of 2023-06 returned 200 and the file is listed in the bucket" |
| Reported | A tool or a person said so, and you did not check | "The job log says export finished" |
| Assumed | You believe it, with no direct evidence | "The source schema has not changed" |
The reader can then spend checking effort on the second and third levels and skip the first.
Handing off the state files
The note is only useful if the state files it points to are consistent. Before you hand off:
- Write a final checkpoint with the atomic save from the previous chapter, so the reader never sees a half-written file.
- Update the progress file so its status list matches the checkpoint.
- Write the handoff note last, and refer to the other two by name.
You can automate the last step. The function below builds a note from the checkpoint and adds the parts only the author knows.
import os
import tempfile
from datetime import datetime, timezone
def write_text_atomic(path, text):
directory = os.path.dirname(os.path.abspath(path))
fd, tmp_path = tempfile.mkstemp(dir=directory, suffix=".tmp")
try:
with os.fdopen(fd, "w") as f:
f.write(text)
f.flush()
os.fsync(f.fileno())
os.replace(tmp_path, path)
except BaseException:
if os.path.exists(tmp_path):
os.unlink(tmp_path)
raise
def write_handoff(path, author, recipient, state, unverified, do_not_repeat, next_action):
now = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M UTC")
done = ", ".join(state["completed"]) or "nothing yet"
def bullets(items):
return "\n".join(f"- {item}" for item in items) or "- None"
text = (
"# Handoff\n\n"
f"**From:** {author} **To:** {recipient} **Time:** {now}\n\n"
"## Where things stand\n"
f"Completed steps: {done}.\n\n"
"## Not yet verified\n"
f"{bullets(unverified)}\n\n"
"## Do not repeat\n"
f"{bullets(do_not_repeat)}\n\n"
"## Next action\n"
f"{next_action}\n\n"
"## Where to look\n"
"progress.md for decisions, checkpoint.json for machine state.\n"
)
write_text_atomic(path, text)
if __name__ == "__main__":
state = {"completed": ["fetch", "transform"], "notes": []}
write_handoff(
"handoff.md",
author="agent-A",
recipient="agent-B",
state=state,
unverified=["Row count after transform was not compared with the source."],
do_not_repeat=["Do not re-run fetch; it drains a rate-limited API quota."],
next_action="Run validate, then publish.",
)
print(open("handoff.md").read())The code generates the facts that can be derived, such as the completed steps and the timestamp. The author must supply judgment: what is unverified, what is dangerous, and what comes next. If you generate those too, the note becomes a restatement of the checkpoint and adds nothing.
Receiving a handoff
The receiver has a job too. Treat the note as a claim, not a command.
- Read the note, then the progress file, then the checkpoint.
- Check that the three agree. If the note says "done through 2023-08" and the checkpoint lists 2023-07, stop and find out why.
- Verify the items under "Not yet verified" before relying on them.
- Check the real system for the side effects the note mentions, as covered in the chapter on verifying saved state.
- Record in the progress file that you took over, with the time.
- Only then run the next action.
Step 5 matters because it leaves a trail. If the next handoff goes wrong, the log shows who held the work and when.
Handoffs between people and agents
People and agents read differently, so adjust the note.
- Agent readers work best with consistent headings and exact file paths. They follow the instructions literally, so an ambiguous "finish the export" can lead to a repeated upload. Name the command or function.
- Human readers need the reason behind a decision, because they may disagree with it. Add a short "why" to each decision and say who can change it.
- Mixed teams should keep one format. A person can read a note meant for an agent, but not always the reverse.
When a person takes over a step that needs approval, such as a deploy, say clearly which actions need that approval. Do not let an agent in the next session assume the approval carried over.
Common mistakes
- Writing the note from memory late. Details are lost or invented. Write from the files.
- Hiding the risky step. A handoff that says "everything is fine" and omits the duplicate-upload hazard causes the damage it should prevent.
- Listing many next actions. The reader must guess the order. Give one, and put alternatives in the progress file.
- Leaving stale notes. An old handoff file that is never replaced gets read as current. Overwrite it each time, or date and archive the old ones.
- Embedding secrets. Notes get copied and committed. Name where a credential lives, never the value.
Exercises
Exercise
A handoff note says "Upload is complete" but the checkpoint's completed list does not include "upload". What should the receiving agent do first, and why?
Show answer
Stop and find out which is true by checking the real system, for example by listing the uploaded files. Do not run the next action yet. The note and the checkpoint disagree, so at least one is wrong. Running upload again could create duplicates, and skipping it could lose data. After finding the truth, fix both files and log the correction.
Exercise
Why does the writehandoff function take "unverified" and "donot_repeat" as arguments instead of computing them from the checkpoint?
Show answer
The checkpoint records what ran, not what the author doubts or which steps are unsafe to repeat. Those come from the author's judgment about the work. If the code guessed them, the note would look complete while missing the information the reader needs most.