Introduction to AI Agents

Learn what AI agents are, how they differ from plain LLMs and chatbots, why they are useful for multi-step work, and where they are used today, with a tiny agent loop built from scratch in Python.

25 minBeginnerCode Examples

The Core Idea: An AI agent is a system where a language model decides what to do next, uses tools to act in the world, looks at the results, and repeats until a goal is reached. A chatbot answers once. An agent works on a task.

  1. Beginner: What Are AI Agents?

An AI agent is a program that pursues a goal on its own. A language model acts as its brain: it reads the situation, chooses an action (such as calling a tool), sees what happened, and chooses the next action. This repeating cycle continues until the task is finished or a limit is reached.

Think of a human assistant. You say book me the cheapest flight to Delhi next Friday, and they search, compare, pick, and book. You did not tell them each click. An agent is the software version of that assistant.

GoalWhat to achieve
A task in plain language, such as find the cheapest item.
ModelThe brain
An LLM that reasons about what to do next.
ToolsThe hands
Functions and APIs the agent can call: search, database, calculator.
LoopThe persistence
Act, observe the result, decide again, until done.

One-line definition: Agent = LLM + tools + a loop + a goal.

  1. Beginner: LLM vs Chatbot vs Agent

These three words are often mixed up. They are layers, each adding something to the one before it:

Plain LLMChatbotAI Agent
What it isA model that predicts the next tokenAn app around an LLM with a chat window and memory of the conversationAn LLM that plans and acts in a loop
Takes actions in the world?No, it only produces textUsually no, it replies with textYes, through tools
Handles multi-step tasks?Only inside one answerOne question and answer at a timeYes, it keeps going until the goal is met
Uses live data?No, only what it was trained onSometimesYes, it fetches what it needs
Who decides the next step?The person, every timeThe person, every timeThe model, within limits you set
ExampleA raw model API callA customer FAQ botA coding agent that edits files and runs tests

Important: The line is not sharp. Many products sit between a chatbot and an agent. The useful question is: does the model choose actions and loop on the results? If yes, it is acting as an agent.

  1. Medium: Why Agents Are Useful

Agents are useful when a task cannot be solved in one reply because the answer depends on steps, fresh data or actions:

NeedHow an agent helpsExample
Many stepsBreaks work into steps and carries them outResearch a topic, then write a report
Fresh or private dataCalls tools to fetch itCheck an order status in a database
Real actionsSends emails, edits files, books, pays (with permission)Create a calendar event
Handling surprisesReads results and adaptsRetry with another search when the first fails
Saving human timeDoes repetitive digital workSort and answer routine support tickets

Rule of thumb: If you can write the exact steps in advance, a normal workflow or script is cheaper and more reliable. Use an agent when the steps depend on what is found along the way.

  1. Medium: Real-World Agent Examples

AreaWhat the agent doesTypical tools
Coding assistantsRead a codebase, edit files, run tests, fix errorsFile system, terminal, test runner
Customer supportUnderstand a request, look up the order, answer or hand over to a humanOrder database, help-center search, ticket system
Research assistantsSearch sources, read pages, compare and summarize with citationsWeb search, document reader
Data analysisWrite queries, run them, build charts, explain resultsSQL, Python, plotting
Personal assistantsManage email, calendar and remindersEmail, calendar, notes
IT operationsWatch alerts, diagnose a fault, apply a safe fixMonitoring, logs, runbooks
Pattern to notice
In every example the agent is given a goal and a small, well-chosen set of tools. The quality of the tools often matters as much as the quality of the model.

  1. Advanced: Levels of Autonomy and When Not to Use an Agent

Agents are not all-or-nothing. It helps to think in levels, from least to most independent:

LevelWho decidesExample
0 · Plain LLMPerson, every stepAsk a question, get text
1 · Workflow with LLM stepsYour code decides the orderSummarize, then classify, then store
2 · Agent with toolsThe model picks tools inside limitsSupport agent with a lookup tool
3 · Agent with approvalThe model proposes, a human approves risky actionsRefund agent that asks before paying
4 · Autonomous agentThe model plans over many steps aloneLong-running research or coding agent

Costs of agents: More latency (many model calls), more cost (tokens add up), and less predictability (the path can differ every run). Start at the lowest level that solves the problem, and move up only when needed.

  1. Visual Explanation: Chatbot vs. Agent and the Four Ingredients

The figure compares a chatbot with an agent on the same question, then lists the four ingredients that make something an agent.

Part 1 · Chatbot vs. AgentSame question, different behavior
ChatbotOne call
Question: 17% of the cheapest item?
↓
Replies once from what it knows
↓
Cannot see the price list, guesses
AgentLoop with tools
Step 1: call the cheapest-item tool
↓
Step 2: call the percent tool
↓
Step 3: read the result and answer
↓
Answers from real data: 76.5
Part 2 · The Four IngredientsEvery agent has all four
Goal
What to do
Given in plain language.
Model
Reasoning
Chooses the next action.
Tools
Acting
Search, database, calculator, APIs.
Loop
Persistence
Repeat until done or a limit is hit.
Click to see when NOT to use an agentCompare ▼
If the steps are always the same, write a normal script or workflow. It is faster, cheaper and easier to test. Choose an agent only when the next step depends on what the previous step found.

Part 1 shows that the agent reaches the answer by looping through tools. Part 2 lists the four ingredients. Open the bar to see when a simple workflow is the better choice.

  1. Python Implementation: A Chatbot and a Tiny Agent Side by Side

This script runs the same question through a chatbot (one call) and a tiny agent (a loop with two tools). A simple rule-based function plays the role of the language model so you can see every step without an API key.

chatbot_vs_agent.pyPython 3.10+ · standard library only
# Chatbot vs agent: the agent loops (decide -> use a tool -> look at the result -> repeat)PRICES = {"laptop": 900, "phone": 600, "tablet": 450} def tool_cheapest():    name = min(PRICES, key=PRICES.get)    return name, PRICES[name] def tool_percent(value, pct):    return round(value * pct / 100, 2) TOOLS = {"cheapest": tool_cheapest, "percent": tool_percent} def chatbot(question):    # One call, no tools, no loop: it can only answer from what it already knows    return "I cannot see the live price list, so I can only guess." def fake_llm(goal, history):    # Stands in for a real model: reads what happened so far and picks the next step    used = [h["action"] for h in history]    if "cheapest" not in used:        return {"action": "cheapest", "args": {}}    if "percent" not in used:        price = history[-1]["result"][1]        return {"action": "percent", "args": {"value": price, "pct": 17}}    return {"action": "finish", "answer": "17% of the cheapest item costs " + str(history[-1]["result"])} goal = "What is 17% of the price of the cheapest item?"print("Chatbot:", chatbot(goal)) history = []for step in range(1, 6):    decision = fake_llm(goal, history)    if decision["action"] == "finish":        print("Agent final answer:", decision["answer"])        break    result = TOOLS[decision["action"]](**decision["args"])    history.append({"action": decision["action"], "result": result})    print("Step", step, "| action:", decision["action"], "| result:", result)
EXPECTED OUTPUT (your numbers may differ slightly)
Chatbot: I cannot see the live price list, so I can only guess.
Step 1 | action: cheapest | result: ('tablet', 450)
Step 2 | action: percent | result: 76.5
Agent final answer: 17% of the cheapest item costs 76.5

The chatbot can only guess, while the agent calls the first tool, reads its result, uses that result to call the second tool, and then answers. The loop of decide, act, observe is the whole idea of an agent. Later lessons replace the rule-based function with a real model.

Knowledge Check

Question 1: What most clearly separates an agent from a chatbot?
Question 2: When is a fixed workflow better than an agent?

Key Points

✓An AI agent is an LLM that decides actions, uses tools and loops on the results until a goal is reached.
✓A plain LLM predicts text, a chatbot adds a chat interface and memory, and an agent adds tools and an action loop.
✓Agents are useful for multi-step tasks that need fresh data or real actions.
✓Real examples: coding assistants, support agents, research assistants, data analysts and IT operations.
✓Autonomy is a spectrum: workflows, tool agents, agents with approval, and fully autonomous agents.
✓Use the simplest option that works. Agents cost more and are less predictable than fixed workflows.

The Big Picture

An Agent in One Line

Goal → model decides → tool acts → result observed → repeat until done

Everything in this series is about making that loop smarter, safer, more reliable and cheaper.

Up Next: You now know what an agent is. In How AI Agents Work you will open the box and study the architecture, the observe, reason, act loop and the agent lifecycle.