How to Replay the AI Agent Run That Did Something Odd Last Night
An agent ran overnight on a batch of tasks. In the morning one result is wrong. It filed a ticket under the wrong customer, or summarised a document it never fetched, or called a tool twice. You run the agent on the same task to see what happened, and it does the job perfectly.
That’s normal for agents. The model’s answers vary from run to run, so the agent picks different tools in a different order and ends up somewhere else. Running it again doesn’t show you last night’s run. It shows you a new one. With an ordinary program, rerunning is how you debug. With an agent, the only copy of the run that went wrong is whatever you kept at the time.
So keep it at the time. Run the agent under CodeInstances wherever it does real work, one instance per task:
codeinstances record -o runs/task-1841.instance -env AGENT_MODE -- python3 agent.py --task 1841

The model API is reached over HTTPS, so its replies are kept in the instance like any other answer: the prompt the agent sent, and what the model said back. So are the search results, the pages it fetched and the APIs it called. -env keeps named environment variables and sets them again on replay. Leave out the ones that hold keys; the model key normally travels in a header that’s redacted anyway.
The Same Run, Offline, at No Model Cost
In the morning, take the instance for the task that went wrong and replay it:
codeinstances replay runs/task-1841.instance
The agent gets the same model replies in the same order, so it makes the same choices and walks the same path, every time you run it. Nothing goes to the model, so the replays cost nothing and work with the network off. Add logging, attach a debugger, step through the moment it picked the wrong tool.
If you have a good run of the same task from another night, codeinstances diff good.instance runs/task-1841.instance lines the two up call by call and shows where they parted ways, down to the field in the model’s reply that sent the agent elsewhere.
Pick Up From the Step That Went Wrong
Say call 9 was the search that came back with the wrong customer. You don’t need to spend another twenty minutes of model calls getting back there:
codeinstances fork -at 9 runs/task-1841.instance
Calls 1 to 8 are answered from the recording in a moment, and from call 9 on the agent runs live, against today’s model and today’s search. Or keep it offline and ask a what-if: what would the agent have done if that search had found nothing?
codeinstances fork -at 9 -offline -answer empty-search.json runs/task-1841.instance
The fork is a run of its own, and it names the run it came from and the step. Give a changed agent after -- and you’re testing the fix against the exact situation that broke it.
Keep It as Evidence
A kept agent run is a record of what the agent was asked, what it was told and what it did. Signing it shows who kept it and that nothing changed since:
codeinstances sign -key ops.key -signer nightly runs/task-1841.instance
codeinstances verify -trust ops.pub runs/task-1841.instance
Sign in a step of its own, after the agent has finished, not with record -sign. The agent runs as you, and a program that can read the key can sign whatever it likes. A store started with serve -trust ops.pub then takes only runs your signing step vouched for.
Limits That Matter for Agents
Streaming model responses are buffered whole, so on replay a streamed reply reaches the agent all at once. That’s fine for most agent code and wrong for code that reacts token by token. Agents that talk over WebSocket aren’t recorded. In Python or JavaScript the clock and random numbers aren’t captured, so an agent that branches on the time of day may not replay exactly. In Go, the hook package records them. Bodies aren’t redacted, so the prompts and the data in them are in the file. Store agent instances like you’d store logs that hold customer data.