The Agent class hands the loop a shallow copy of its messages and tools on each run. The loop mutates that copy freely — pushing partial messages, replacing them as deltas arrive — while the class only commits fully completed messages into its own transcript via message_end events. If the loop crashes mid-turn, the owner’s persistent state stays consistent: no half-written message ever enters the history. Crash safety emerges from the boundary design rather than from extra cleanup code.
Related:
- A stateless loop core plus a stateful wrapper separates orchestration from session state
- Stream partial messages in place so interrupted contexts stay structurally complete
- LLM Agents
- Tools expire, but problems don’t. Every tool is an answer to a question that existed before it did — the same problem the write-ahead log solves in databases and file systems