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 Shell Team Aish Agentic Loop Continuation

From Leeroopedia
Knowledge Sources
Domains LLM, Agent_Architecture
Last Updated 2026-10-29 00:00 GMT

Overview

Pattern for orchestrating an iterative LLM tool-calling loop that processes user input, executes tool calls, feeds results back, and continues until the assistant stops calling tools or a limit is reached.

Description

Agentic Loop Continuation is the core orchestration pattern for an AI agent that can call tools. The loop starts with the user's message and conversation context, sends them to the LLM, and then iterates: if the LLM response contains tool calls, each tool is executed (after security preflight), and the results are appended to the conversation as new messages. The loop sends the updated conversation back to the LLM and repeats until the LLM responds with text only (no tool calls), or a maximum iteration limit is reached.

The loop is bounded by a maximum iteration count (default 20) to prevent infinite cycling. A cancellation token allows the user to interrupt the loop at any point. Each iteration emits events for the UI to render streaming output, tool calls, and tool results in real time.

This pattern solves the fundamental problem of agentic AI: the model needs to act (call tools) and observe (see results) iteratively to accomplish multi-step tasks, but the loop must be bounded, cancellable, and observable.

Usage

Use this principle when building an AI agent that iteratively calls tools to accomplish a task. The loop should be the single entry point for processing user input, and it should handle the full cycle: prompt assembly → LLM call → tool normalization → security preflight → tool execution → result feedback → repeat.

Theoretical Basis

The loop is a bounded iterative tool-calling cycle:

# Abstract algorithm (NOT real implementation)
def process_input(user_msg, context_messages, system_message, stream):
    cancel_token.reset()
    messages = assemble(system_message, context_messages, user_msg)

    for iteration in range(MAX_ITERATIONS):
        if cancel_token.is_cancelled():
            break

        response = stream_simple(dialect, ctx, messages, tools, stream)
        messages.append(response.assistant_message)

        if not response.tool_calls:
            break  # natural stop

        normalized = normalize_tool_call_batch(response.tool_calls)
        for call in normalized.calls:
            decision = security.decide(call)
            if decision.is_blocked:
                messages.append(blocked_result(call))
                continue
            if decision.needs_confirmation:
                if not user.confirms():
                    messages.append(cancelled_result(call))
                    continue
            result = tool.execute(call)
            messages.append(tool_result(call, result))

    return ProcessResult(text=last_assistant_text, messages=messages)

Key design ideas:

  • Bounded iteration: a maximum turn count prevents infinite loops
  • Cancellable: a shared cancellation token allows user interruption at any iteration
  • Tool result feedback: tool results are appended as messages and fed back to the LLM
  • Event emission: each step emits events for real-time UI rendering

Related Pages

Implemented By

Page Connections

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