logo_smallAxellero.io

Agentic Loop

How AI agents reason and act iteratively to complete tasks.

The agentic loop is the core execution pattern that enables AI agents to autonomously complete complex tasks through iterative reasoning and action.

How It Works

When an AI agent receives a task, it enters a reasoning loop:

┌─────────────────────────────────────────────────────────┐
│                    AGENTIC LOOP                         │
├─────────────────────────────────────────────────────────┤
│                                                         │
│    ┌──────────┐                                         │
│    │  INPUT   │ ← User message or trigger               │
│    └────┬─────┘                                         │
│         │                                               │
│         ▼                                               │
│    ┌──────────┐                                         │
│    │  REASON  │ ← LLM analyzes context and decides      │
│    └────┬─────┘                                         │
│         │                                               │
│         ▼                                               │
│    ┌──────────┐     ┌──────────┐                        │
│    │  DECIDE  │────▶│ RESPOND  │ → Final answer         │
│    └────┬─────┘     └──────────┘                        │
│         │                                               │
│         ▼ (if tool needed)                              │
│    ┌──────────┐                                         │
│    │   ACT    │ ← Execute tool call                     │
│    └────┬─────┘                                         │
│         │                                               │
│         ▼                                               │
│    ┌──────────┐                                         │
│    │ OBSERVE  │ ← Process tool results                  │
│    └────┬─────┘                                         │
│         │                                               │
│         └─────────────────────────────┐                 │
│                                       │                 │
│         ┌─────────────────────────────┘                 │
│         │                                               │
│         ▼                                               │
│    ┌──────────┐                                         │
│    │  REASON  │ ← Continue with new context             │
│    └──────────┘                                         │
│                                                         │
└─────────────────────────────────────────────────────────┘

Loop Stages

1. Input

The agent receives a task through:

  • User message in chat
  • API call
  • Trigger from another agent (handoff)
  • Scheduled execution

2. Reason

The LLM processes:

  • Current user input
  • Conversation history (memory)
  • System prompt instructions
  • Available tools and their descriptions
  • Knowledge base context (if connected)

Based on this context, the LLM decides the next action.

3. Decide

The agent makes one of two decisions:

Option A: Respond directly

  • The agent has enough information to answer
  • No tool call is needed
  • Loop terminates with a response

Option B: Use a tool

  • Additional information or action is needed
  • Agent selects the appropriate tool
  • Loop continues to Act stage

4. Act

The agent executes the selected tool:

  • Calls external APIs
  • Queries databases
  • Runs JavaScript code
  • Retrieves from knowledge base

5. Observe

The agent processes tool results:

  • Tool output is added to context
  • Results inform the next reasoning step
  • Errors are handled and may trigger retries

6. Iterate

The loop repeats from the Reason stage with:

  • Original user input
  • Updated context including tool results
  • Accumulated conversation history

Loop Control

Max Turns

The maxTurns parameter limits iterations to prevent infinite loops:

SettingBehavior
maxTurns: 5Agent stops after 5 tool calls
maxTurns: 1Single tool call, then respond
maxTurns: 0No tool calls allowed

Set appropriate maxTurns limits to control costs and prevent runaway agents. Start with lower values and increase as needed.

Termination Conditions

The loop ends when:

  • Agent generates a response without tool call
  • maxTurns limit is reached
  • An error occurs and cannot be recovered
  • A guardrail blocks execution

Example: Research Task

User: "What's the weather in Tokyo and should I bring an umbrella?"

Turn 1 - Reason:
  Agent thinks: "I need weather data for Tokyo"
  Decision: Call weather tool

Turn 1 - Act:
  Tool: getWeather(location: "Tokyo")
  Result: { temp: 22, conditions: "Partly cloudy", rain_chance: 70% }

Turn 2 - Reason:
  Agent thinks: "I have weather data. 70% rain chance is high."
  Decision: Respond to user

Turn 2 - Respond:
  "It's 22°C and partly cloudy in Tokyo with a 70% chance of rain.
   Yes, I'd recommend bringing an umbrella!"

Loop complete (2 turns)

Best Practices

Tool Descriptions

Clear tool descriptions help the LLM decide when to use each tool:

Good: "Search the product database by name, SKU, or category.
      Returns product details including price and availability."

Bad: "Search products"

System Prompt Guidance

Guide the agent's reasoning in the system prompt:

When answering questions:
1. First check if you have the information in context
2. If not, use the appropriate search tool
3. Verify information before responding
4. If uncertain, ask for clarification

Memory Management

  • Short-term memory retains recent conversation turns
  • Tool call memory stores summaries of previous tool results
  • Long-term memory persists across sessions

Configure memory based on task complexity:

  • Simple Q&A: Minimal memory
  • Multi-step research: Full tool memory
  • Ongoing conversations: Long-term enabled

Debugging the Loop

Use the Logs panel in Axellero Studio to inspect:

  • Each turn's input and output
  • Tool calls and their results
  • Where the loop terminated
  • Error messages if any

Common issues:

  • Loop not using tools: Check tool descriptions and system prompt
  • Excessive iterations: Lower maxTurns or improve tool descriptions
  • Wrong tool selection: Make tool names and descriptions more specific