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: