Lesson 1 of 7 · What Is an Agent?
What Is an AI Agent?
AI agents explained simply: a language model that pursues a goal by choosing actions, using tools and observing results. See it in Python, with examples.
- 5 min read
- Beginner
- Reviewed
- Explain what an AI agent is in one sentence
- Name the parts of an agent: goal, model, tools and loop
- Follow an agent's steps towards a goal in Python
- Recognise everyday examples of AI agents
From answering to doing
A chat assistant answers a question and stops. Ask it “What's the cheapest day to fly to Rome next week?” and, without access to live prices, the best it can do is suggest how you might find out.
An agent goes and finds out. It checks the price for Monday, then Tuesday, and so on, compares them, and reports back. It works towards a goal, choosing its own next step each time, using tools to act on the world.
The parts of an agent
| Part | Role | In the flight example |
|---|---|---|
| Goal | What the agent is trying to achieve | Find the cheapest day to fly to Rome |
| Model | Decides the next step | Chooses which day to check next, or to finish |
| Tools | Let it act and observe | check_price(day) |
| Loop | Repeats think, act, observe | Runs until it has an answer (or hits a limit) |
A tiny agent
Here is that agent in Python. A real agent uses a language model to decide each step. So this page can run without an API key, the decide function stands in for the model with a simple rule. The loop around it is the real structure:
# A tiny agent with one goal: find the cheapest day to fly to Rome next week.
PRICES = {"Mon": 189, "Tue": 142, "Wed": 131, "Thu": 175, "Fri": 210} # what the airline's API would return
def check_price(day): # the TOOL: the agent's way of acting on the world
return PRICES[day]
def decide(goal, notes):
"""Stands in for the LLM: picks the next step from what it knows so far.
(A real model would make this decision by reading the goal and the notes.)"""
for day in PRICES:
if day not in notes:
return ("check_price", day)
cheapest = min(notes, key=notes.get)
return ("finish", f"Fly on {cheapest}: {notes[cheapest]} euros, the cheapest day.")
goal = "Find the cheapest day to fly to Rome next week"
notes = {} # what the agent has observed so far
for step in range(1, 11): # the agent loop, with a safety limit of 10 steps
action, detail = decide(goal, notes)
if action == "finish":
print(f"Step {step}: done -> {detail}")
break
price = check_price(detail) # act...
notes[detail] = price # ...and observe
print(f"Step {step}: check_price({detail!r}) -> {price}")Each step is the same three moves: decide what to do, act with a tool, observe the result and remember it. After checking all five days, the agent decides it has enough information and finishes. The range(1, 11) makes sure it can never loop forever.
Common mistakes
Give the agent a stopping rule: book the first day that fits the budget instead of checking every day.
prices = {"Mon": 189, "Tue": 142, "Wed": 131, "Thu": 175}
budget = 150
# The agent checks days in order. Make it stop as soon as a price is within
# the budget, and print "Book <day>" for that day.
for day in prices:
print(f"Checked {day}: {prices[day]}")
What makes a system an AI agent rather than a plain chatbot?
Summary
- An AI agent uses a language model to pursue a goal by choosing actions, using tools and reacting to results.
- Its parts are a goal, a model, tools and a loop.
- Each step is decide, act, observe, with a limit so it can't run forever.
- Coding, research and support assistants are common examples.
Next: what exactly changes when you turn an LLM into an agent.
Finished reading? Tick it off to track your progress.