CHAPTER 02 · The Agent Loop Pattern · 6 / 8
The event timeline
A loop that just returns final text throws away everything interesting that happened along the way. A production loop should emit an ordered event timeline as it runs: "model reasoned," "called tool X," "tool X returned," "model wrote text." This timeline is gold:
- It drives a rich UI (showing tool activity as it happens, Chapter 8).
- It's the durable record of the turn (persisted as the conversation's content, Chapter 9).
- It's the raw material for observability and debugging (Chapter 15).
Design the loop to record an explicit, typed event for every meaningful action, in order. Don't try to reconstruct what happened from the final text afterward; capture it as it occurs.