Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Openai Openai node Tool Execution Loop

From Leeroopedia
Knowledge Sources
Domains Function_Calling, Agentic_Patterns
Last Updated 2026-02-15 00:00 GMT

Overview

A principle for automatically executing tool calls in a loop where the model requests function invocations, the SDK executes them, and results are fed back until the model produces a final text response.

Description

The Tool Execution Loop implements an agentic pattern where the language model can call tools iteratively. The loop operates as follows: (1) send messages and tool definitions to the model, (2) if the model responds with tool calls, parse the arguments, execute the functions, and append tool results to the conversation, (3) send the updated conversation back to the model, (4) repeat until the model produces a final text response or the maximum iteration count is reached.

This pattern enables multi-step reasoning where the model can gather information from multiple sources before formulating its final answer.

Usage

Use this principle when building agentic applications where the model needs to interact with external systems across multiple turns. The SDK provides runTools() for automatic loop management.

Theoretical Basis

The tool execution loop follows a ReAct (Reasoning + Acting) pattern:

function runToolLoop(messages, tools, maxIterations):
    for i in range(maxIterations):
        response = callAPI(messages, tools)

        if response has no tool_calls:
            return response  // Final answer

        for tool_call in response.tool_calls:
            args = tool.parse(tool_call.arguments)
            result = tool.function(args)
            messages.append({
                role: 'tool',
                tool_call_id: tool_call.id,
                content: JSON.stringify(result),
            })

    throw MaxIterationsExceeded

The loop is bounded by maxChatCompletions (default 10) to prevent infinite loops.

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment