{"slug": "the-agentic-loop-three-loops-in-a-trench-coat", "title": "The Agentic Loop: Three loops in a trench coat", "summary": "Building autonomous AI agents involves three nested loops: an inference loop for LLM chat completions, a tool loop for executing tool calls, and a third loop for managing state and context. The author argues that oversimplifying agent loops as a single loop misses the complexity required for production systems.", "body_md": "# The Agentic Loop: Three loops in a trench coat\n\n### The building blocks for autonomous agents aren't as simple as they seem.\n\nAgent loops are often oversimplified. They’re presented as a single loop, when really it’s three loops in a trench coat that make up an “agentic” experience for a customer. I’m here to write (yes, I *wrote* this, insane right?) yet-another-blog about agent loops. The example code blocks are also pseudo-code and for illustrating these ideas. Also I’ve omitted streaming, which complicates the post but the shape of these stays the same.\n\n## The Inference Loop\n\nThe most reductive way to explain Large Language Models is that they take text, and predict the next charact... err... tokens. Unlike your ex, they really do complete your sentences. This is achieved with an inference loop.\n\nYour inference loop has three responsibilities:\n\nMake chat completion API calls (infer the next words)\n\nPass a tool usage request to your tool loop (more later)\n\nManage chat history persistence (of tool results, or more user messages)\n\nWhen you’re building an agent, the first “outer” loop you’re creating is the inference loop. This is the loop that sends a system prompt, user and assistant messages, and available tools to an LLM’s “chat completion” endpoint. [Anthropic has one](https://platform.claude.com/docs/en/api/go/beta/messages/create). [OpenAI has one](https://developers.openai.com/api/reference/resources/chat/subresources/completions/methods/create). [OpenRouter makes all of them easy to use](https://openrouter.ai/docs/api/api-reference/chat/create-a-chat-completion).\n\nOnce an LLM returns its messages, it’s your responsibility to append them to a “chat history” so that a conversation can be continued. This can be an array, a database table, Redis keys, whatever. Your prerogative.\n\n```\nchatMessages = [\n  {\n    role: \"system\",\n    content: \"You are a couples therapist. You help people either make it work, or make them realize it will never work. The user you're speaking with is named Tom.\"\n  },\n  {\n    role: \"user\",\n    content: \"I kinda miss Laney, what should I write to her?\"\n  }\n]\n\ncontinueInferenceLoop = true\nwhile continueInferenceLoop\n  inferred = aiClient.completeChat(messages: chatMessages)\n  \n  chatMessages.push({\n    role: \"assistant\",\n    content: inferred.message.content,\n    toolCalls: inferred.toolCalls,\n  })\n  \n  if inferred.toolCalls.length == 0\n    continueInferenceLoop = false\n  else\n    # Handle tools (see more later)\n  end\nend\n\nfinalResult = chatMessages.last.content\nputs \"Assistant responded with: #{finalResult}\"\n```\n\nThe API design of (most) Large Language Model providers is a stateless design. This means the model provider has no idea what your previous conversation was with it. **You need to provide the entire conversation every time.** This is why you see the warning of “Large conversations use tokens faster” — yes I typed an em-dash — because you are sending the tome of messages you’ve built up about whether you should reach out to your ex.\n\nDon’t do it, Tom.\n\n## The Tool Loop\n\nLLMs are brains in a jar. They provide no functional value on their own. **The tools you give an LLM are what make it an agent.**\n\nWhen you tell a model “here are the tools you have” in your outer inference loop, the model may try to “use” them in its inference (response). This is the same thing as a brain sending an electrical signal telling your index finger to hover over the enter key of the email you desperately want to send Laney. Tom, we need to set boundaries my man.\n\nThe separate tool definitions you include in your API request are usually serialized into the system prompt field of the token stream the model processes. And it may infer the usage of *multiple* tools in one turn. (Hence: Tool Loop).\n\n```\nchatMessages = [\n  {\n    role: \"system\",\n    content: \"You are a couples therapist. You help people either make it work, or make them realize it will never work. The user you're speaking with is named Tom.\"\n  },\n  {\n    role: \"user\",\n    content: \"I kinda miss Laney, what should I write to her?\"\n  }\n]\n\ntools = [\n  {\n    type: \"function\",\n    function: {\n      name: \"send_ex_girlfriend_an_email\",\n      parameters: {\n        type: \"object\",\n        properties: { email_message: {type: \"string\"} },\n        required: [\"email_message\"]\n      }\n    }\n  }\n]\n\ncontinueInferenceLoop = true\nwhile continueInferenceLoop\n  inferred = aiClient.completeChat(\n    messages: chatMessages,\n    tools: tools\n  )\n  \n  chatMessages.push({\n    role: \"assistant\",\n    content: inferred.message.content,\n    toolCalls: inferred.toolCalls,\n  })\n  \n  if inferred.toolCalls.length == 0\n    continueInferenceLoop = false\n  else\n    # Behold... the Tool Loop!\n    while tool = inferred.toolCalls.shift\n      # Do tool things in here like sending an email\n      # You might have a switch/case statement to control\n      # which tool based on the name.\n      # Tool calls also have an ID\n      toolResult = \"...see next section...\"\n      \n      # Tool result shapes vary by an provider's API\n      chatMessages.push({\n        role: \"user\",\n        content: [{\n          type: \"tool_result\",\n          toolCallId: tool.toolCallId,\n          content: toolResult\n        }]\n      })\n    end\n  end\nend\n```\n\nYour tool loop must look up the tool the model inferred it should use, and call your own function with the parameters the model provided as well. But keep in mind a few things:\n\nBecause tool calls are\n\n*also inferred text*- it means a tool name, or function parameters, can be hallucinated. You should be defensive to these bogus tool calls with something like`Tool Not Found: \"call_my_ex_girlfriend\"`\n\nThe API responds with a\n\n`tool_call_id`\n\n(or`tool_use_id`\n\nif you’re using Anthropic). This ID is used for the API & Model to correlate calls with responses. If you send a subsequent completion request without the tool call’s ID, the API will error.For some providers, tool results\n\n**don’t have error codes or error states**- The resulting content*is*the error. Use XML tags to denote an error like`<tool_call_error>Phone number blocked</tool_call_error>`\n\nAnthropic does have an\n\n`is_error`\n\nfield, though.\n\n## The Human Loop\n\nI debated the name of this loop. This loop doesn’t need to exist, really. It also doesn’t need to be a human. But I believe AI should benefit humans, so I’m sticking with it. If you’d like to be a contrarian you could also call it “The Safety Loop” or “The Sanity Loop” – both would work.\n\nAssuming you like control over agentic implementations, you’ll need to implement your third loop to approve/deny a tool, or steer the model another direction.\n\nI also must concede that this “loop” is not a programmatic loop. It is more of a blocking function call, with someone or something looping on their own whether or not to proceed with the tool call. The loop is not in the bounds of your code, it resides within the bounds of the approver.\n\n```\nwhile tool = inferred.toolCalls.shift\n  result = \"\"\n  \n  case tool.name\n  when \"send_ex_girlfriend_an_email\"\n    # Block this loop. \n    # Maybe this sends an SMS, Email, or displays a button with the decision.\n    decision = wait_for_approval(tool.parameters)\n    \n    if decision.approved?\n      send_email(tool.parameters[\"email_message\"])\n      result = \"Email sent\"\n    elsif decision.denied_with_instructions?\n      result = \"<tool_call_denied_with_instructions>\n        #{decision.instructions}\n      </tool_call_denied_with_instructions>\"\n    else\n      result = \"<tool_call_error>Tool call was not approved</tool_call_error>\"\n    end\n  else\n    result = \"<tool_call_error>The tool '#{tool.name}' was not found</tool_call_error>\"\n  end\n  \n  chatMessages.push({\n    role: \"user\",\n    content: [{\n      type: \"tool_result\", \n      toolCallId: tool.toolCallId, \n      content: result\n    }]\n  })\nend\n```\n\nThe Human Loop is arguably the *hardest* part to implement in agentic systems. You can’t have a piece of code block for hours. What if the server restarts? What if you have thousands of other requests coming in you need to respond to? The first two loops (inference and tool) are simple enough. The human loop ups the ante of difficulty. This is why durable execution frameworks exist, like Temporal.\n\nBut the human loop is necessary, because it’s the only thing stopping Tom from *actually* sending that message to Laney. IT WAS TWO YEARS AGO TOM, MOVE ON!\n\n## Looping Back\n\nPutting it all together:\n\n**Inference Loop**- Calls a chat completion API, and delegates tool calls to your....** Tool Loop**- Which handles the tool usage request the model is attempting to make and hands off approval to your...** Human Loop**- to ask for approval, or a new direction. The result of this propagates back the tool’s result.\n\nThese three loops are the building blocks of agentic systems. They are used for RAG, progressive discovery, automatic tool approval checks, and more.\n\nNow go build one!", "url": "https://wpnews.pro/news/the-agentic-loop-three-loops-in-a-trench-coat", "canonical_source": "https://www.bobbytables.io/p/the-agentic-loop-three-loops-in-a", "published_at": "2026-07-14 14:39:56+00:00", "updated_at": "2026-07-14 14:47:58.893111+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "ai-agents", "ai-tools", "ai-infrastructure"], "entities": ["Anthropic", "OpenAI", "OpenRouter"], "alternates": {"html": "https://wpnews.pro/news/the-agentic-loop-three-loops-in-a-trench-coat", "markdown": "https://wpnews.pro/news/the-agentic-loop-three-loops-in-a-trench-coat.md", "text": "https://wpnews.pro/news/the-agentic-loop-three-loops-in-a-trench-coat.txt", "jsonld": "https://wpnews.pro/news/the-agentic-loop-three-loops-in-a-trench-coat.jsonld"}}