Agents while loop LLM call tool dispatch

also: Agentic AI · AI Agents · Autonomous Agents

patterns app-dev

Agents are just while loops with an LLM as the transition function — with extra steps

Under the hood

  • while loop A control flow statement that repeats a block of code as long as a condition holds. Wikipedia ↗
  • LLM call A single request/response to a language model API — the atomic unit of agent work. Wikipedia ↗
  • tool dispatch Selecting and invoking a function from a registry based on a name and arguments. Wikipedia ↗

What they say

AI agents are “autonomous entities that can reason, plan, and take action to achieve complex goals.” They “break down tasks, use tools, and adapt their approach based on intermediate results.”

What it actually is

An agent is a loop. The observe→think→act pattern1 maps directly to: send messages to LLM, get back a tool call or final response, execute the tool, repeat.

The pattern in pseudocode

messages = [system_prompt, user_message]

while True:
    response = llm.chat(messages)

    if response.has_tool_calls():
        for call in response.tool_calls:
            result = dispatch(call.name, call.arguments)
            messages.append(tool_result(call.id, result))
    else:
        print(response.text)
        break

That’s the entire architecture. Everything else is optimization.2

The “extra steps”

  1. “Planning” — prompting the LLM to output a plan before acting (chain of thought)
  2. “Memory” — appending to the message array, or summarizing older messages when it gets too long (array management)
  3. “Reflection” — asking the LLM to evaluate its own output (another LLM call)
  4. “Multi-agent” — running multiple loops, sometimes feeding outputs between them (nested loops, message passing)

What you already know

If you’ve polled a job status endpoint, you’ve written the agent loop. The pattern is identical:

while (!done) {
  const status = await fetch('/job/123');
  if (status.result) break;
  await sleep(1000);
}

Replace the fetch with an LLM call. Replace status.result with “the LLM returned text instead of a tool call.” That’s it.

The real engineering challenge isn’t the loop — it’s managing the context window, handling errors gracefully, and knowing when to stop.3

Footnotes

  1. Intelligent agent — Wikipedia — the CS definition behind the marketing term. The observe→think→act loop has been the standard formulation since the 1990s.

  2. Building effective agents — Anthropic, 2024. Notable for explicitly recommending simple loops over complex frameworks, and for the section on when not to use agents at all.

  3. Anthropic tool use overview — The API response structure you’re actually dispatching on. The stop_reason: "tool_use" field is the loop condition.