AI Agents Explained: When AI Takes Actions on Its Own
Chatbots answer; agents act. Learn how AI agents combine an LLM with tools and a think-act-observe loop to complete real tasks — with an agent-loop diagram, a runnable Python demo, and the safety limits that matter.
So far the LLMs in this series have answered questions. But the tools making headlines now — coding assistants that edit your files, research bots that browse and compile reports, assistants that book things and run workflows — do something more: they take actions. These are AI agents. An agent wraps the LLM you met in Lesson 20 in a loop and a set of tools, turning "a model that talks" into "a system that does." This lesson shows exactly how that works.
Chatbot vs. agent: the key difference
A plain chatbot is one step: you ask, it replies, done. An agent is given a goal and figures out the steps itself — it can look things up, run code, call other software, check the result, and try again — repeating until the goal is met.
| Chatbot | Agent |
|---|---|
| Answers in one shot | Works through multiple steps toward a goal |
| Only produces text | Uses tools (search, code, APIs) to act |
| No memory of a plan | Observes results and adjusts |
The agent loop
At the heart of every agent is a simple cycle: the LLM reasons about what to do, acts by calling a tool, observes the result, and decides whether it's done or needs another step. Repeat until the goal is reached.
Tools: how an agent touches the real world
An LLM on its own can only produce text. Tools are functions you give the agent so its text can do things — a web search, a calculator, running code, sending an email, querying a database, or even a RAG lookup over your documents. The model decides which tool to call and with what input; your code runs the tool and feeds the result back. This wiring is often called function calling or tool use.
See an agent loop in Python (runnable)
Here's the loop with two tools. To keep it runnable without an API key, a simple rule-based function stands in for the LLM's decision — but the structure (reason → act → observe → repeat) is exactly what a real agent does. Watch it decide, use a tool, and finish.
# Tools the agent is allowed to use
def calculator(expr): return eval(expr)
def search(query): return "GyaanPost is a blog for learning AI."
tools = {"calculator": calculator, "search": search}
# Stand-in for the LLM 'brain' that decides the next step.
# A real agent would ask an LLM here.
def decide(goal, notes):
if "notes" not in notes and any(c.isdigit() for c in goal):
return ("calculator", "15 * 12")
if "notes" not in notes:
return ("search", goal)
return ("finish", notes["notes"])
# The agent loop
def run_agent(goal, max_steps=4):
notes = {}
for step in range(max_steps):
action, arg = decide(goal, notes) # 1. Reason
if action == "finish":
print("FINAL:", arg); return
result = tools[action](arg) # 2. Act
print(f"Step {step+1}: {action}('{arg}') -> {result}")
notes["notes"] = result # 3. Observe
print("Stopped: hit step limit.")
run_agent("What is 15 * 12?")
Swap that decide() function for a real LLM call and you have a genuine agent. Everything else — the tools, the loop, the observations — stays the same.
The ReAct pattern
The most common agent style is called ReAct (Reason + Act). The model writes out its Thought, chooses an Action (a tool + input), reads the Observation (the tool's output), and loops. Making the model "think out loud" before acting — the chain-of-thought idea from Lesson 21 — leads to noticeably better decisions.
Memory and planning
Beefier agents add two things: memory (notes or a scratchpad so they don't lose track across many steps, sometimes backed by a vector store), and planning (breaking a big goal into a checklist of sub-tasks, then working through it). These make agents capable of longer, multi-step jobs.
Agents in the real world
- Coding agents that read a repo, edit files, run tests and fix bugs (this is what Claude Code does).
- Research agents that search the web, read pages and write a summarised report.
- Customer-support agents that look up orders, check policies and take action.
- Data agents that write and run queries to answer business questions.
The catch: agents make mistakes
Autonomy cuts both ways. An agent can take a wrong action, get stuck in a loop, misuse a tool, or confidently pursue the wrong plan — and because it acts, mistakes have real consequences. That's why serious agents use guardrails: limits on which tools they can call, approval steps before risky actions (deleting data, spending money, sending messages), and a human in the loop. Give an agent power gradually, and watch what it does.
The one-line definition: an AI agent = an LLM + tools + a loop that lets it reason, act, and keep going until a goal is done.
The first time I watched an agent read an error message, decide on its own to open the right file, make a fix and re-run the test — all without me — the leap from "chatbot" to "agent" finally felt real to me. It's genuinely powerful, and also exactly why I keep a close eye on what actions I let one take. My advice: start agents with read-only or low-risk tools before you ever give them the keys.
Key takeaways
- An AI agent is an LLM given tools and a loop, so it can act — not just answer.
- The core cycle is Reason → Act → Observe → repeat until the goal is met.
- Tools / function calling let the model search, run code, call APIs, or do RAG lookups.
- The ReAct pattern (think, then act) plus memory and planning power multi-step tasks.
- Agents are powerful but error-prone — use guardrails and human oversight, especially for risky actions.
Continue the series: ← Lesson 22: RAG — Give AI Your Own Data · Next: Lesson 24 — How AI Generates Images: Diffusion Models Explained →
Frequently Asked Questions
What is an AI agent?
An AI agent is a system that combines a large language model with tools and a loop, so it can take actions to reach a goal instead of only replying with text. It reasons about what to do, uses a tool such as search or code, observes the result, and repeats until the task is complete.
How is an AI agent different from a chatbot?
A chatbot answers in a single step and only produces text. An agent is given a goal and works through multiple steps, using tools to act, checking the results, and adjusting its plan. That reason-act-observe loop is the main difference.
What are tools in an AI agent?
Tools are functions the agent is allowed to call so its decisions can affect the real world — for example web search, a calculator, running code, calling an API, or doing a RAG lookup over your documents. The model chooses which tool to use and your code runs it and returns the result.
Are AI agents safe to use?
Agents are powerful but can make mistakes, get stuck, or take the wrong action, and because they act, errors have real consequences. Safe agents use guardrails such as limited tool access, approval steps before risky actions, and human oversight, especially for anything that deletes data, spends money or sends messages.