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.
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.
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.
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.
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'.
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
An LLM placed in a loop with tools and a goal, able to take actions and observe results until done.
Reason + Act: alternating Thought β Action β Observation cycles until a final answer.
Declaring callable functions to the model; it emits structured calls your code executes.
The control loop that runs the model, executes tool calls, and feeds results back β with iteration limits.
Decomposing goals into steps and carrying state across iterations for multi-step tasks.
Multiple specialized agents (planner, researcher, critic) collaborating β powerful but costlier and harder to control.
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];67let messages = [{ role: "user", content: "What is Tokyo's population x 2?" }];89while (true) {10 // The model decides: answer directly, or call a tool (function calling).11 const res = await llm.chat({ messages, tools });1213 if (res.toolCalls?.length) {14 for (const call of res.toolCalls) {15 const result = await runTool(call.name, call.args); // ACT16 messages.push({ role: "tool", name: call.name, content: result }); // OBSERVE17 }18 continue; // loop back so the model can REASON again19 }20 return res.content; // no tool call β final ANSWER21}22// Always cap iterations + budget to prevent infinite/runaway loops.
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
1Answering Questions That Need Live Data and Math
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.
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.
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
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.
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.
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.