HT
How Things Work
System Online

AI Agents & Tool Use

Turning an LLM into something that can act — the ReAct loop, function calling, agent control loops with guardrails, and multi-agent collaboration.

How It Works

An AI agent is an LLM placed in a loop with tools and a goal. Using the ReAct pattern, it reasons, calls tools via function calling, observes the results, and iterates until it can answer — letting a frozen model search, compute, query, and act in the real world. Planning, memory, and multi-agent collaboration extend this, but autonomy must be bounded with iteration and cost limits.

1
An LLM in a Loop With Tools

A bare LLM can only produce text. An agent wraps it in a loop and gives it tools — functions it can call (search, calculator, code execution, database queries, APIs). This lets the model take actions in the world and incorporate real results, not just answer from memory.

2
The ReAct Pattern

The dominant pattern is ReAct (Reason + Act): the model produces a Thought (what should I do next?), an Action (call a tool with arguments), then reads the Observation (the tool's result). It repeats this Thought→Action→Observation cycle, building up information until it can give a final Answer.

3
Tool Use / Function Calling

Tools are described to the model (name, purpose, parameter schema). Modern models support structured function calling: the model emits a JSON call to a chosen tool, your code executes it, and the result is fed back. This structured loop is far more reliable than parsing free-text 'actions'.

4
Planning & Multi-Agent Systems

More advanced agents plan ahead (decompose a goal into steps), keep memory across steps, and reflect on their own outputs to self-correct. Multi-agent systems assign roles — planner, researcher, coder, critic — and have agents collaborate, which can tackle complex tasks but adds cost and coordination challenges.

Key Concepts

🤖Agent

An LLM placed in a loop with tools and a goal, able to take actions and observe results until done.

🔁ReAct

Reason + Act: alternating Thought → Action → Observation cycles until a final answer.

🛠️Tool / Function Calling

Declaring callable functions to the model; it emits structured calls your code executes.

♻️Agent Loop

The control loop that runs the model, executes tool calls, and feeds results back — with iteration limits.

🗺️Planning & Memory

Decomposing goals into steps and carrying state across iterations for multi-step tasks.

👥Multi-Agent

Multiple specialized agents (planner, researcher, critic) collaborating — powerful but costlier and harder to control.

The agent loop with tools
tsx
1// An agent = LLM + tools + a loop. Tools are declared as callable functions.
2const tools = [
3 { name: "search", description: "Search the web", parameters: { query: "string" } },
4 { name: "calculator", description: "Evaluate math", parameters: { expr: "string" } },
5];
6
7let messages = [{ role: "user", content: "What is Tokyo's population x 2?" }];
8
9while (true) {
10 // The model decides: answer directly, or call a tool (function calling).
11 const res = await llm.chat({ messages, tools });
12
13 if (res.toolCalls?.length) {
14 for (const call of res.toolCalls) {
15 const result = await runTool(call.name, call.args); // ACT
16 messages.push({ role: "tool", name: call.name, content: result }); // OBSERVE
17 }
18 continue; // loop back so the model can REASON again
19 }
20 return res.content; // no tool call → final ANSWER
21}
22// Always cap iterations + budget to prevent infinite/runaway loops.
💡
Why This Matters

Agents are the frontier of applied AI — copilots, autonomous workflows, and tool-using assistants are all agents. Understanding the ReAct loop, function calling, and the very real failure modes (runaway loops, cost, reliability) is essential for building practical agentic systems and is an increasingly common AI-engineering interview topic.

Common Pitfalls

Unbounded loops — no max iterations/budget/timeout, so agents run away in cost and time.
Too many or overly powerful tools, expanding the failure surface and confusing the model.
Trusting tool inputs/outputs blindly — validate and sandbox (especially code execution and writes).
Reaching for multi-agent autonomy when a simple deterministic workflow would be cheaper and more reliable.
No observability — without logging the thought/action/observation trace, agent failures are impossible to debug.
Real-World Use Cases

1Answering Questions That Need Live Data and Math

Scenario

A user asks, 'What's our total revenue this quarter, and how does that compare to the same quarter last year as a percentage?' The answer requires querying a database and doing arithmetic — neither of which an LLM can do reliably alone.

Problem

A plain LLM would hallucinate numbers (it has no access to your database and is poor at exact arithmetic). It can't fetch live data or guarantee correct math from parametric memory.

Solution

Build an agent with a SQL query tool and a calculator. It reasons about what data it needs, calls the query tool, observes the results, calls the calculator for the percentage, and composes a grounded answer. Each fact and computation comes from a tool, not the model's guesswork.

💡

Takeaway: Agents extend LLMs with tools so they can fetch real data and perform exact operations. Offload anything factual or computational to a tool, and let the model orchestrate — not hallucinate — the result.

2The Runaway Agent Loop

Scenario

An autonomous agent given a vague goal kept calling tools in circles, never deciding it was done. It burned through thousands of API calls and a large bill before anyone noticed, and still produced no answer.

Problem

Without guardrails, agent loops can fail to terminate — re-reasoning, re-searching, or oscillating between tools indefinitely. Open-ended autonomy plus per-step LLM cost makes runaway loops both likely and expensive.

Solution

Add hard limits: a maximum iteration count, a token/cost budget, timeouts, and termination criteria. Constrain the toolset, validate tool inputs, and require the agent to justify continuing. Prefer the simplest workflow that solves the task — full autonomy only where it's truly needed.

💡

Takeaway: Agent autonomy must be bounded. Cap iterations, budget, and tools, and define clear stopping conditions — otherwise loops run away in cost and time. Use the least autonomy that gets the job done.

Join the discussion

Comments

?

Loading comments...

Cookie preferences

We use essential cookies for sign-in and preferences. With your permission, we also use basic analytics to improve lessons.