CHAPTER 08 · Streaming and Real-Time UX · 5 / 10
Ordering: flush before you act
Keep events in the order the user expects. A common bug: the model writes some text, then calls a tool, but the text hasn't been flushed, so the tool's output appears before the text that preceded it. The fix is to flush any buffered text whenever the model transitions to a tool call, so the timeline stays chronological. Emit a tool_call_start event at that moment too, so the UI can immediately show activity rather than a dead gap while the tool runs.