Sub topic 16
Tracing and Reviewing Multi-Agent Runs
This chapter assumes you have read the earlier chapters on the orchestrator-worker pattern, delegation briefs, and result aggregation. You should be comfortable reading basic Python and Git output.
Why multi-agent runs need a trace
When one agent works alone, you can read its conversation from top to bottom. With subagents, the work is spread across several contexts. The parent sees only each subagent's summary. If a change looks wrong, the summary alone will not tell you which brief produced it, what the subagent actually did, or when it happened.
A trace is a record of each step in a run, kept outside any single agent's context. It lets you answer three questions after the fact:
- Which subagent changed this file?
- What brief did that subagent receive?
- Did it check its work, and what was the result?
What to record
Record a small, consistent set of fields for every subagent run.
| Field | Why it helps |
|---|---|
| Run ID and task name | Links every record from one orchestration together |
| Brief text | Shows exactly what the subagent was told |
| Tools and workspace | Shows what it could touch, such as its branch or worktree |
| Start time, end time, status | Shows ordering, duration, and timeouts |
| Verification command and result | Shows what evidence supports the summary |
| Returned summary | Shows what the parent actually saw |
Store the full brief and the full returned summary. Do not rely on a shortened version, because a missing constraint is often the cause of a bad result.
A minimal trace in Python
The following wrapper writes one JSON line per subagent run. It builds on the orchestrator from the earlier chapter. The run_subagent function is a placeholder for your own agent runtime.
import asyncio
import json
import time
import uuid
from dataclasses import dataclass
@dataclass
class Task:
name: str
brief: str
async def run_subagent(task: Task) -> dict:
# Placeholder: replace with a call to your agent runtime.
await asyncio.sleep(0.1)
return {"name": task.name, "status": "ok", "summary": f"finished {task.name}"}
async def traced(run_id: str, task: Task, log_path: str) -> dict:
started = time.time()
try:
result = await run_subagent(task)
except Exception as exc:
result = {"name": task.name, "status": "failed", "summary": str(exc)}
record = {
"run_id": run_id,
"task": task.name,
"brief": task.brief,
"status": result["status"],
"summary": result["summary"],
"started": started,
"seconds": round(time.time() - started, 3),
}
with open(log_path, "a", encoding="utf-8") as log:
log.write(json.dumps(record) + "\n")
return result
async def main() -> None:
run_id = uuid.uuid4().hex[:8]
tasks = [
Task("update-docs", "Update README usage section for the new --verbose flag."),
Task("add-tests", "Add unit tests for parse_args in tests/test_cli.py."),
]
await asyncio.gather(*(traced(run_id, t, "trace.jsonl") for t in tasks))
if __name__ == "__main__":
asyncio.run(main())Each line in trace.jsonl is one complete record, so you can search it with ordinary tools.
Connecting the trace to Git
If each subagent works on its own branch or worktree, the branch name is a second trace. Name branches after the task, such as agent/docs, and list the commits each one made:
git log --oneline main..agent/tests
git diff --stat main..agent/testsCompare the diff against the brief's scope. A subagent told to edit only tests/test_cli.py should not have touched src/.
Reviewing a run
Work through the same steps each time:
- Read the status column first and list any failed or timed-out tasks.
- For each task, compare the brief with the diff. Look for changes outside the stated scope.
- Confirm that the recorded verification command was run and that it passed.
- Check durations. A task far slower than its peers often hit retries or got stuck.
- When a result is wrong, fix the brief and rerun that task. Patching the output by hand hides the cause.
Exercise
A subagent's summary says "tests added, all passing," but the branch diff shows it also edited src/cli.py, which the brief forbade. Which trace evidence do you check next, and what do you do with the task?
Show answer
Check the recorded brief to confirm the scope constraint was stated, and the recorded verification command to see what it actually ran. Then reject the branch's out-of-scope changes. If the brief was missing or unclear about scope, fix the brief. Rerun the task in a fresh worktree rather than merging the current branch.