Writing
·6 min read

How AI Agents Actually Work: The Tool Use Loop Explained

I built a chat interface that makes the agentic loop visible: the AI reasons about tools, calls them, gets results, and synthesizes a response — and you can see every step. Here's the implementation and why tool descriptions matter more than schemas.

aiagentstool usefunction callingengineering

“AI agents” is everywhere right now. But most explanations are either too abstract (“the model reasons and takes actions”) or too implementation-specific. I built a demo that makes the actual mechanism visible: a chat interface where you can watch every step of the agentic loop as it happens.

The app is at tool-use-demo.vercel.app. Try asking “What's the weather in Tokyo and what time is it there?” — you'll see it call two tools, receive two results, then synthesize a response.

What the loop actually looks like

Here's the agentic loop in pseudocode:

while true:
  response = claude(messages, tools=tool_definitions)
  
  if response.stop_reason == "end_turn":
    return response.text  # Done
  
  # Model wants to call tools
  tool_calls = [block for block in response.content 
                if block.type == "tool_use"]
  
  # Execute each tool
  tool_results = []
  for call in tool_calls:
    result = execute_tool(call.name, call.input)
    tool_results.append(result)
  
  # Give results back to model
  messages.append({"role": "assistant", "content": response.content})
  messages.append({"role": "user", "content": tool_results})

That's it. The model either returns a final text response (end_turn) or requests tool calls. If it requests tools, you run them and loop. The loop continues until the model says it's done.

In the demo, I cap iterations at 5 as a safety limit. In practice, simple queries resolve in 1-2 iterations.

Tool definitions: the schema vs. the description

Each tool has three parts: a name, an input_schema (JSON Schema), and a description. Most people focus on the schema — the actual types and required fields. The description matters more.

The model uses descriptions to decide when to call a tool, not just how. Compare:

  • ❌ “Get weather” — the model might not use this for “is it raining in London?”
  • ✅ “Get the current weather for any city. Returns temperature, conditions, humidity, and wind speed.” — clear on what it returns and when to use it

For the calculator tool, I specified: “Evaluate a mathematical expression. Supports arithmetic, percentages, square roots, powers.” This tells the model both the capability and the scope — so it doesn't try to use it for things it can't handle.

The implementation

The server-side agentic loop is in app/api/agent/route.ts. Tool implementations live in lib/tools.ts.

The five tools in the demo:

  • get_weather — looks up weather data for a city (city-indexed lookup for demo; real API in production)
  • calculate — evaluates math expressions with eval (restricted to arithmetic — no arbitrary code)
  • search_web — simulated search results (in production: Brave Search API, Serper, etc.)
  • get_time — uses Intl.DateTimeFormat to get current time in any IANA timezone
  • get_exchange_rate — approximate Feb 2026 rates for 20+ currencies

Making it visible

The API returns the full trace: every tool call, every result, and the final response. The UI renders these as a step-by-step timeline:

  • Tool calls — color-coded blocks with tool name and inputs (expandable)
  • Tool results — expandable key-value panels with the raw output
  • Final response — Claude's synthesized answer with tool badges

This is intentional. The whole point of the demo is making the mechanism legible. A chat interface that hides tool calls is a better product experience — but a worse learning tool.

The production generalization

The same loop pattern powers every AI agent in production:

  • Customer service bots that look up order status before answering
  • Legal AI (like Clio) that queries case databases before drafting
  • Coding agents that read files, run tests, and apply patches
  • Research tools that search, read, and synthesize papers

The tools change. The loop is the same.

Try it at tool-use-demo.vercel.app. Ask something that combines multiple tools — like “What's the weather in Vancouver and convert the temperature to Fahrenheit?” — and watch the loop run.