The tool loop is the core pattern for agentic behavior: the model calls tools, you execute them and return results, and the model continues until it has enough information to respond.
The full loop, one phase per call
Phase 1: send the user message + tool definitions. Model returns either text (loop ends) or one-or-more tool_calls. Phase 2: for each tool_call, run the handler and capture the result. Phase 3: send the tool results back as a new turn (Chat Completions: tool-role messages with tool_call_id; Responses: input list with function_call_output items). Model returns either text or another tool_call. Loop until text.
Production loops always have a max_iterations cap (8-12 is typical). Without it, a model can ping-pong tools forever under bad prompting and burn through your budget in minutes. The cap is a circuit breaker, not a soft limit — when hit, abort with a clear error rather than 'maybe one more turn'.