AI Agent Overview

Understand how AI agents work, when to use them, and how they differ from ordinary model calls and fixed workflows.

🎯 Introduction

We have already learned how to use function calling to let a model request tools, such as an inventory lookup. In real applications, however, a user may need more than a single lookup. For example, someone buying a batch of products may also need suitable alternatives if stock is insufficient.

An AI agent lets the model choose the next action based on the user’s request and tool results, reducing the work of arranging separate tool sequences for different tasks.

This article uses a product purchasing example to explain why AI agents are useful, how they work, and which tasks suit them.


🧱 Why Do We Need AI Agents?

Suppose we are building a product assistant. Users might ask it to:

  • Check the stock level for product P100.
  • Check whether there are enough mechanical keyboards to buy 15 units.
  • Buy 15 mechanical keyboards; if stock is insufficient, find suitable alternatives and compare prices.

The first request needs only a stock lookup. The second requires finding a product ID before checking stock. The third requires using the stock result to decide whether to search for alternatives and check their prices and availability.

Using a Fixed Workflow

We can define the execution flow in code: choose the business logic that matches the request, then search for alternatives if stock is insufficient.

This is a valid approach. When business rules are clear and the steps are predictable, writing the logic directly is usually simpler and more reliable.

But supporting more purchasing constraints—such as a preferred brand, a budget, or combinations of products based on stock and price—requires more conditional logic. Even when the tools stay the same, different tasks may require calling them in different orders.

Using an Agent

Another approach is to provide product search, stock lookup, and price lookup tools, then let the model select tools based on the request instead of defining a separate sequence for every case.

After executing a tool, the application returns its result to the model. If stock is sufficient, the model can answer immediately. If it is insufficient, the model can keep looking for alternatives that meet the user’s requirements. If essential information is missing, it can ask the user.

This still uses the function calling introduced earlier. The difference is that the application allows the model to keep selecting tools based on results, rather than requiring a final answer after one tool execution.

The main value of an agent is that the model participates in decisions during execution, rather than the developer specifying every tool call in advance.


🧱 What Is an AI Agent?

An AI agent is an application system that makes decisions toward a task goal and completes work by executing tools, obtaining results, and deciding what to do next.

An agent is not a new kind of large language model. We can use the same model APIs as before; the application changes how it calls the model and executes tools.

To understand the basic process, start with a simple request: a user wants to buy 15 mechanical keyboards and asks whether enough are in stock.

Suppose the application provides two tools:

ToolPurpose
find_productLook up a product ID by name
get_stockCheck stock by product ID

Here is one possible execution sequence:

I want to buy 15 mechanical keyboards. Are there enough in stock?
Agent
Decision 1Product ID missing
Look up product IDP100
Decision 2Product ID found
Check stock for P10012 units
Decision 3Enough information is available. No more tool calls are needed.
12 in stock, 3 short

On the first model call, the user has not supplied a product ID, so the model selects find_product. The application runs the tool, obtains P100, and returns the result to the model.

Using that ID, the model selects get_stock. The application executes it and reports 12 units in stock. The model now has enough information, so it answers without requesting another tool.

If the user supplies the product ID initially, the model can skip product search. If no product is found, it can report the failed lookup or ask the user for clarification instead of checking stock with a nonexistent ID.

These steps are selected by the model based on the current task and tool results, rather than prescribed in advance by the program.

The model proposes the next action; the application executes it. In this course’s architecture, the model accesses databases, files, and business APIs indirectly through tools provided by the application.


🧱 Core Components of an Agent

A basic tool-calling agent consists of a model, tools, and execution logic. The application also maintains task state and sets execution limits.

ComponentResponsibilityProduct lookup example
ModelSelect the next action based on the request and available resultsDecide to look up the product ID first
ToolsPerform queries or business operationsCheck product stock
Execution logicCall the model, execute tools, return results, and decide whether to continueSend the product ID back to the model
StateStore messages and tool results for the current taskRecord the discovered product ID P100
Execution controlsLimit calls, handle failures, and manage permissionsPrevent repeated queries or stock changes without confirmation

State retains information gathered during the task so the model can continue from previous results. Long-term memory can additionally preserve purchasing preferences across conversations, but it is not required for an agent.

Tools can still come from the same sources: ordinary JavaScript functions, RAG knowledge retrieval functions, or an MCP server.

Agent Loop

In the product lookup above, the application repeatedly calls the model, executes tools, and returns results. This repeated process is called the agent loop.

Each model request includes the current task’s message history and available tools. If the model requests a tool, the application executes an allowed operation, appends its result to the history, and calls the model again. If the model returns a final answer without requesting tools, the application can end the loop.

If a tool fails, the application can return the error to the model so it can adjust its approach. The application should stop when it reaches a call limit, exceeds a budget, or encounters an unrecoverable error. Ending the loop does not guarantee success; evaluate the actual results.

The next article implements this loop manually with the OpenAI SDK. Later, we will simplify it with LangChain.js and createAgent().


⚖️ Agents Compared with Other AI Applications

Not every AI application needs an agent. Choose an approach based on the task:

ApproachHow it runsSuitable tasks
Ordinary model callThe model generates an answer directly from the inputSummarization and translation
WorkflowThe developer defines the flow, which can include model calls, tools, and conditional branchesProcessing orders under predefined review rules
AgentWithin application limits, the model selects the next action based on the task and resultsProduct purchasing queries with varying requirements

Workflows can include model decisions and loops, and agents can be part of workflows. These concepts are not mutually exclusive. The main distinctions are who determines the execution path and how much freedom the model has to select actions.

When Should You Avoid an Agent?

If a user clicks “Check stock” and the application already knows the product ID and API endpoint, call the API directly. No model is needed.

An agent may make multiple model calls, increasing latency and cost compared with a fixed flow, and its behavior may vary. Operations such as deducting stock or taking payment still require programmatic permission and business rule checks rather than leaving every decision to the model.


❓ Questions and Answers

Q1: Does an agent require RAG, MCP, and long-term memory?

No. A model, ordinary tools, and an execution loop are enough for a basic agent. External knowledge, remote tools, and long-term memory are optional, depending on the problem the application needs to solve.


🧪 Exercises

Decide whether each request is better served by a fixed program or an agent, and explain why. If you choose an agent, list the tools and possible stopping conditions:

  1. Check stock for a known product ID and display it in a table.
  2. Find products from a user’s description, check stock, look up acceptable alternatives if needed, and compare prices.

Check your answer: the first task only needs the stock API. The second can use product search, stock lookup, and price lookup tools. When a suitable product is found, the agent can recommend a purchase. If none is available or a call limit is reached, it must also stop and report the actual outcome. A fixed workflow can handle the second task too if its business rules are sufficiently clear.


🎉 Congratulations! You now understand the purpose of agents and how they run.