CHAPTER 02 · The Agent Loop Pattern · 2 / 8
Why both the request and the result must be appended
A subtlety that trips up first implementations: when the model requests a tool, you must append both the model's tool-request turn and the tool-result turn before the next call. The providers require this pairing: the model needs to see "I asked for X, and X returned Y" to continue coherently. Most provider APIs also require that every tool call in a turn gets a corresponding result. Skipping a result, or mismatching ids, breaks the next call. A robust loop therefore guarantees a result for every call, synthesising an error result if a tool failed to produce one.