~/TechPurAI
~/tutorials/llm-and-advanced-ai/how-ai-agents-work
advanced·part 17 of 22·3 min read

How AI Agents Work

Updated Aug 16, 2026AI

The AI Fundamentals series introduced the real distinction between a chatbot and an agent — an agent can take real, independent action via tools. This part covers exactly how that actually works internally, the real reasoning loop underneath every agent this series builds toward.

The real, complete agent loop

text
1. Receive a real, user request
2. The model decides: can I answer directly, or do I need a TOOL?
3. If a tool is needed, the model outputs a real, structured tool
   call — which specific tool, with what real arguments
4. Your application code actually EXECUTES that real tool call
5. The real result is sent back to the model
6. The model decides AGAIN: is this enough to answer, or is
   ANOTHER tool call needed?
7. Repeat steps 3-6 until the model has enough real information to
   generate a final answer

This is the real, precise, technical version of the AI Fundamentals series' own conceptual introduction — genuinely a loop, not a single step, and the model itself decides at each real iteration whether it has enough information yet.

A real, concrete trace through this loop

text
Real request: "Cancel GreenDesk customer #4471's subscription and
  refund their last invoice"

Iteration 1: model decides it needs customer #4471's real account
  details first → calls get_customer(customer_id=4471)
Iteration 2: model receives the real customer data, decides it needs
  the real last invoice → calls get_last_invoice(customer_id=4471)
Iteration 3: model has enough real information → calls
  cancel_subscription(customer_id=4471) AND
  issue_refund(invoice_id="INV-9021")
Iteration 4: model receives real confirmation of both actions →
  generates a final, real summary message for the human operator

This is genuinely the same underlying mechanism as the AI Fundamentals series' own multi-step example — now shown as the real, explicit loop structure running underneath it, iteration by iteration.

The real, technical tool-calling format

python
tools = [
    {
        "name": "get_customer",
        "description": "Look up a real GreenDesk customer by ID",
        "input_schema": {
            "type": "object",
            "properties": {"customer_id": {"type": "integer"}},
            "required": ["customer_id"],
        },
    },
]

response = client.messages.create(
    model="claude-sonnet-5",
    max_tokens=1024,
    tools=tools,
    messages=[{"role": "user", "content": "Cancel customer #4471's subscription and refund their last invoice"}],
)

# the real, structured response indicates the model wants to call a tool
if response.stop_reason == "tool_use":
    tool_call = next(block for block in response.content if block.type == "tool_use")
    print(tool_call.name, tool_call.input)
    # → "get_customer", {"customer_id": 4471}

response.stop_reason == "tool_use" is the real, structural signal that the model isn't done generating text — it's requesting your actual code execute something and report back, precisely step 3 of the loop above.

The real, complete Python loop implementation

python
def run_agent_loop(user_request: str, tools: list, tool_functions: dict) -> str:
    messages = [{"role": "user", "content": user_request}]

    while True:
        response = client.messages.create(
            model="claude-sonnet-5", max_tokens=1024, tools=tools, messages=messages,
        )
        messages.append({"role": "assistant", "content": response.content})

        if response.stop_reason != "tool_use":
            return response.content[0].text  # real, final answer

        tool_call = next(block for block in response.content if block.type == "tool_use")
        real_result = tool_functions[tool_call.name](**tool_call.input)

        messages.append({
            "role": "user",
            "content": [{"type": "tool_result", "tool_use_id": tool_call.id, "content": str(real_result)}],
        })

This while True loop is the real, literal, working implementation of the seven-step loop described at the top of this part — it keeps calling the model, executing whatever real tool it requests, and feeding the result back, until the model's stop_reason indicates it has genuinely finished.

Why it matters

Notice this loop has NO built-in limit on how many iterations it can run — a real, genuine safety concern this part deliberately leaves visible rather than hiding. Part 18 covers the real, necessary guardrails (iteration limits, permission scoping, confirmation steps) a production agent genuinely needs before this loop is safe to run against real, consequential actions like the cancel/refund example above.

Next: giving an agent real tools and guardrails — the production safety measures this raw loop is deliberately missing.

VK

Vijay Kumar

Founder of TechPurAI — writing hands-on tutorials and honest tool breakdowns.

LinkedIn ↗
← previous16. Keeping a Knowledge-Base Chatbot Up to Datenext →18. Giving an Agent Real Tools and Guardrails