Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:AI QL Tuui Agentic Tool Loop

From Leeroopedia
Knowledge Sources
Domains LLM, Agentic_Systems, Tool_Use
Last Updated 2026-10-24 00:00 GMT

Overview

An agentic inference pattern where the LLM iteratively requests tool calls, executes them, and re-infers with the results until no further tools are needed.

Description

The Agentic Tool Loop implements the ReAct (Reasoning + Acting) pattern for LLM-driven tool use. After each LLM completion, the system checks whether the assistant requested any tool calls. If so, it executes each tool call against the appropriate MCP server, converts the results to OpenAI message format, appends them to the conversation, and re-invokes the LLM. This loop continues until the assistant produces a response without tool calls, signaling completion. The pattern enables autonomous multi-step reasoning where the LLM decides which tools to use and in what order.

Usage

Use this principle when building agentic chat systems where the LLM should autonomously invoke external tools (file reading, web search, database queries) to answer user questions, and the system must handle the request-execute-reinfer cycle without user intervention.

Theoretical Basis

The loop follows a guarded iteration:

function agenticLoop(sessionId):
    createCompletion(session)
    if result == 'done':
        postToolCall(sessionId)

function postToolCall(sessionId):
    lastMessage = getHistory(sessionId).last
    if isEmptyTools(lastMessage.tool_calls):
        delete lastMessage.tool_calls
        return  # Loop terminates
    for each toolCall in lastMessage.tool_calls:
        result = mcpStore.callTool(toolCall.name, toolCall.args)
        messages = contentConvert(result, toolCall.id)
        append messages to conversation
    if toolCalled and sessionId == current:
        processInference(sessionId)  # Loop continues

The termination condition is no tool calls in the assistant response. Each iteration adds tool results to the conversation context, expanding the LLM's knowledge for the next inference round.

Related Pages

Implemented By

Page Connections

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