How to use this course
1. Read one lesson at a time, in order. Each lesson has the same shape, so you always know what comes next.
2. Read “What it is”, then “Why it matters”.
3. Copy the Python code, save it, and run it. Change one small thing and run it again. That is how you learn.
4. Do the “Try it yourself” task. It is small on purpose.
5. Answer the “Check” questions. Click a question to see the answer. If you got it wrong, read the lesson again.
6. Tick “Done” when you finish a lesson. Your progress is saved in this browser.
Work in order: Module 1 builds the loop and memory. Module 2 adds tools. Module 3 organises it with GAME. Module 4 keeps tools tidy with decorators. Module 5 builds the full Inventory Management Agent.
Setup — Python and running code
What you need
Python 3.10 or newer. That is all. No extra packages, no internet, no API key.
Check your Python
Open a terminal and run:
python3 --versionYou should see Python 3.10 or newer, for example Python 3.12. If you see an older version, install a newer Python first.
How to run the code
- Copy the code from a lesson.
- Save it in a file, for example agent.py, in a folder for this course.
- In the terminal, go to that folder and run: python3 agent.py
- Read the output. Change one line, run it again, and see what changes.
Keep one file per lesson at first, for example lesson01.py, lesson02.py. Later, build one file called agent.py that grows with the course.
No API key needed — the fake model
Every Python example in this course uses a small function called fake_llm. It is not a real model. It reads keywords in the prompt and returns a fixed answer. That is enough to learn the loop, tools, memory, and GAME, and it works offline.
How to swap the fake model for a real OpenAI call — the 5-line change. Only do this if you want to, and only after the course works with the fake model. Do not do it to follow the course.
# 1. Install once: pip install openai
# 2. Put your key in an environment variable, never in the code:
# export OPENAI_API_KEY="your-key-here"
# 3. Replace fake_llm with this real function:
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
def fake_llm(prompt):
response = client.chat.completions.create(model="gpt-4o-mini", messages=[{"role": "user", "content": prompt}])
return response.choices[0].message.contentKeep the same function name, fake_llm, so all the other code keeps working. If the real model returns free text instead of “ACTION: …”, add the format rules from Module 1 to your prompt, as shown in the lessons.
Try the Agent Loop here
Type a request, for example check stock of apples, add 10 apples, or what is low in stock? The simulated agent below picks a tool, runs it, and shows each loop step. No server, no key, no Python needed for this box.
Press “Run the agent” to see the loop.
Try these: “check stock of bread”, “add 10 apples”, “what is low in stock?”, “hello”.
Module 1 — Agentic AI Concepts
What an AI agent is, how it works in a loop, and how it talks to you and to a program.
20 lessons in this module.
Video: Introduction
What it is
An AI agent is a program that can do a task on its own, step by step. You give it a goal, such as 'check the stock of apples'. The agent thinks, picks an action, sees the result, and then decides what to do next. It is not just a chatbot that answers once. It works in a loop until the goal is done.
Why it matters
This is the base idea for the whole course. Every later lesson adds one part to this simple idea: memory, tools, or a clear plan.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Programmatic prompting: your program builds the prompt, not you typing it.
def make_prompt(task, item):
return f"You are a helpful shop assistant. Task: {task}. Item: {item}. Answer in one short sentence."
for task, item in [("check stock", "apples"), ("check stock", "bread")]:
prompt = make_prompt(task, item)
print("PROMPT:", prompt)
print("MODEL:", fake_llm(prompt))
print()
Try it yourself
Change the item in the code from 'apples' to 'bread' and run it again.
Check
What is an AI agent, in one sentence?
A program that works toward a goal, step by step, on its own.
What makes an agent different from a normal chatbot?
An agent acts in a loop and can use tools, not just answer once.
Video: Flipped Interaction Pattern
What it is
Normal software waits for you to click or type. The flipped pattern turns this around: the AI asks you questions. For example, the agent asks 'Which item do you want to check?' or 'How many should I add?' You answer, and it continues. The agent leads the conversation, and you guide it.
Why it matters
This pattern makes agents useful for real tasks. The agent can ask for missing facts instead of guessing, so it makes fewer mistakes.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Prompting, part 2: give the model a role, a goal, and a format.
def make_prompt(role, goal, item):
return (
f"Role: {role}.\n"
f"Goal: {goal}.\n"
f"Item: {item}.\n"
"Format: ACTION: <action> | ITEM: <item>"
)
prompt = make_prompt("shop assistant", "check stock", "apples")
print(prompt)
print("---")
print(fake_llm(prompt))
Try it yourself
Change the role in the code from 'shop assistant' to 'warehouse helper' and run it.
Check
Who asks the questions in the flipped pattern?
The AI agent asks the user.
Why is that useful?
The agent can ask for missing facts instead of guessing.
Video: The Agent Loop
What it is
The agent loop has four steps, repeated: 1) Look at the goal and what happened so far. 2) Think and pick the next action. 3) Do the action. 4) Look at the result. Then repeat. The loop stops when the goal is done, or after a set number of steps so it cannot run forever.
Why it matters
Without a loop, an agent can only do one step. With a loop, it can fix its own mistakes, try a second tool, and finish a multi-step task.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# The Agent Loop: think -> act -> see the result -> think again -> stop.
# The loop stops when the model says it is done, or after 3 steps.
def run_agent(user_request):
print("User:", user_request)
for step in range(1, 4):
prompt = f"Request: {user_request}. Step {step}. What should I do next?"
answer = fake_llm(prompt)
print(f"Step {step} - Model says: {answer}")
if "check_stock" in answer:
print("Step", step, "- Tool result: apples = 12 in stock.")
print("Final answer: We have 12 apples in stock.")
return
print("Final answer: Done.")
return
run_agent("check stock of apples")
Try it yourself
Change the step limit in the code from 3 to 5 and run it again.
Check
What are the four steps of the agent loop?
Look, think and pick, do, then look at the result.
Why set a step limit?
So the loop cannot run forever.
Video: Adding Structure to AI Agent Outputs
What it is
A plain model answer is free text, which is hard for a program to read. Structure means a fixed format, for example 'ACTION: check_stock | ITEM: apples'. Your code can then split the text on '|' and read the action and the item. This is called structured output.
Why it matters
Structured output lets your program act safely. It knows exactly which tool to call and with what item, instead of guessing from a long sentence.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Prompting, part 3: add examples and rules, so the answer is steady.
EXAMPLES = "Example: 'check stock of apples' -> ACTION: check_stock | ITEM: apples"
def make_prompt(user_request):
return (
"You are a shop assistant.\n"
"Rules: answer only with ACTION and ITEM. Do not add extra words.\n"
+ EXAMPLES + "\n"
f"Request: {user_request}"
)
for request in ["check stock of apples", "hello"]:
print(make_prompt(request))
print("MODEL:", fake_llm(request))
print()
Try it yourself
Add a new example line for 'add stock of bread' and run the code.
Check
What is structured output?
An answer in a fixed format that a program can read easily.
Give one example.
ACTION: check_stock | ITEM: apples
Reading: Running the Code Samples in the Course
What it is
Every code sample in this course runs with plain Python 3.10 or newer. No extra packages and no internet are needed. You save the code in a file, for example agent.py, and run it with 'python agent.py'. Each sample uses the small fake_llm function in the code, so it works offline.
Why it matters
You can follow the whole course without paying for a model or setting up keys. That keeps the focus on how agents work.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Programmatic prompting: your program builds the prompt, not you typing it.
def make_prompt(task, item):
return f"You are a helpful shop assistant. Task: {task}. Item: {item}. Answer in one short sentence."
for task, item in [("check stock", "apples"), ("check stock", "bread")]:
prompt = make_prompt(task, item)
print("PROMPT:", prompt)
print("MODEL:", fake_llm(prompt))
print()
Try it yourself
Save the code as agent.py and run 'python agent.py'. Check that you see PROMPT and MODEL lines.
Check
What do you need to run the samples?
Python 3.10 or newer, nothing else.
Do you need an API key?
No. The samples use a small fake model that runs offline.
Reading: Try Out Programmatic Prompting
What it is
Programmatic prompting means your program builds the prompt. You do not type the prompt by hand each time. Your code puts the role, the goal, the item, and the rules into a text string, then sends that string to the model. The same code can build hundreds of prompts, one for each item or task.
Why it matters
This is how agents work at scale. One program can ask the model about every item in a shop, not just one item you typed.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Programmatic prompting: your program builds the prompt, not you typing it.
def make_prompt(task, item):
return f"You are a helpful shop assistant. Task: {task}. Item: {item}. Answer in one short sentence."
for task, item in [("check stock", "apples"), ("check stock", "bread")]:
prompt = make_prompt(task, item)
print("PROMPT:", prompt)
print("MODEL:", fake_llm(prompt))
print()
Try it yourself
Add a third item, 'milk', to the list in the code and run it.
Check
What does 'programmatic' mean here?
The program builds the prompt, not a person typing it.
Why is that better than typing?
The same code can build many prompts, fast and without typos.
Reading: Try Out the Customer Service Agent
What it is
A customer service agent is a simple, useful first example. A customer asks a question, for example about stock or an order. The agent reads the question, picks a tool, gets the answer, and replies in a friendly way. It can also ask a follow-up question if a fact is missing.
Why it matters
Customer service shows why agents matter in real work: they answer common questions, check real data with tools, and free a person for harder cases.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Your first agent: one small class that holds the loop and the memory.
class Agent:
def __init__(self, name):
self.name = name
self.memory = []
def remember(self, fact):
self.memory.append(fact)
def run(self, user_request):
print(f"{self.name} got: {user_request}")
self.remember(f"User asked: {user_request}")
answer = fake_llm(user_request)
print("Model:", answer)
if "check_stock" in answer:
self.remember("Apples in stock: 12.")
print("Final answer: We have 12 apples in stock.")
print("Memory:", self.memory)
agent = Agent("ShopAgent")
agent.run("check stock of apples")
Try it yourself
Change the agent name from 'ShopAgent' to 'HelpAgent' and run it.
Check
What does the customer service agent do first?
It reads the customer question and picks the next action.
When should it ask a follow-up question?
When a fact it needs is missing.
Reading: Learning More & Staying Connected
What it is
Learning agents is a long road. The best way to stay on track is small, regular practice: one lesson a day, one small program a day. Keep your notes and your code in one folder. When you finish this course, the next step in this repo is the MCP course with TypeScript, which shows how agents share tools across programs.
Why it matters
A course gives you the map. Daily practice is what makes the ideas stick, so you can build your own agents later.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
Try it yourself
Write down in a notebook: one idea for an agent you would like to build. Keep it to one sentence.
Check
What is the best way to learn after this course?
Small, regular practice: one lesson and one small program at a time.
What is the next course in this repo?
AI Agents with Model Context Protocol (MCP) and TypeScript.
Assignment: Knowledge Check: Agent Loop & Flipped Interaction
What it is
This is a short check of Module 1 so far. The agent loop repeats: look, think, act, look at the result. The flipped interaction pattern means the agent asks the user questions. Structured output means the answer follows a fixed format, so code can read it.
Why it matters
A short check helps you see what is clear and what to read again, before the next module adds tools.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# The Agent Loop: think -> act -> see the result -> think again -> stop.
# The loop stops when the model says it is done, or after 3 steps.
def run_agent(user_request):
print("User:", user_request)
for step in range(1, 4):
prompt = f"Request: {user_request}. Step {step}. What should I do next?"
answer = fake_llm(prompt)
print(f"Step {step} - Model says: {answer}")
if "check_stock" in answer:
print("Step", step, "- Tool result: apples = 12 in stock.")
print("Final answer: We have 12 apples in stock.")
return
print("Final answer: Done.")
return
run_agent("check stock of apples")
Try it yourself
Without looking, write the four loop steps on paper, then check them against the code comments.
Check
In the flipped pattern, who asks the questions?
The AI agent.
What does the loop do after an action?
It looks at the result, then thinks again.
Assignment: Understanding Agentic AI Concepts
What it is
This check covers the whole idea of Module 1: an agent, a goal, a loop, memory, and structured output. An agent works toward a goal. Memory lets it remember facts from earlier steps. Structured output lets code read the model's answer and act on it.
Why it matters
If you can explain these five words — agent, goal, loop, memory, structured output — in plain words, you are ready for Module 2.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Memory: a simple list. The agent keeps what it learned, and uses it later.
memory = []
def remember(fact):
memory.append(fact)
print("Remembered:", fact)
def run_agent(user_request):
print("User:", user_request)
remember("User likes apples.")
remember("Apples in stock: 12.")
print("Memory so far:", memory)
print("Model:", fake_llm(user_request))
print("Final answer: We have 12 apples. I remembered you like apples.")
run_agent("check stock of apples")
Try it yourself
Explain each of those five words out loud in one sentence each.
Check
What is a goal in this course?
The task the agent is trying to finish.
What does memory do?
It keeps facts from earlier steps, so the agent can use them later.
Hands-on: Programmatic Prompting for Agents
What it is
You build a prompt with code. Use an f-string: f'You are a helpful shop assistant. Task: {task}. Item: {item}.' The {task} and {item} parts are filled in by your program. Change the values, and you get a new prompt without retyping the whole text.
Why it matters
This is the first building block. Every agent in this course starts with a prompt built this way.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Programmatic prompting: your program builds the prompt, not you typing it.
def make_prompt(task, item):
return f"You are a helpful shop assistant. Task: {task}. Item: {item}. Answer in one short sentence."
for task, item in [("check stock", "apples"), ("check stock", "bread")]:
prompt = make_prompt(task, item)
print("PROMPT:", prompt)
print("MODEL:", fake_llm(prompt))
print()
Try it yourself
Make a prompt for 'add stock' of 'milk' using the same make_prompt function.
Check
What is an f-string?
A Python string where {name} is filled in with a value.
What changes when you change the item?
The whole prompt changes, without retyping it.
Hands-on: Programmatic Prompting for Agents II
What it is
Now add three parts to the prompt: a role, a goal, and a format. Role: who the model should act as. Goal: what it should do. Format: how the answer should look. Clear parts make the model's answer clearer and steadier.
Why it matters
A model with a clear role and format is easier to trust. Your code can then read the answer with confidence.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Prompting, part 2: give the model a role, a goal, and a format.
def make_prompt(role, goal, item):
return (
f"Role: {role}.\n"
f"Goal: {goal}.\n"
f"Item: {item}.\n"
"Format: ACTION: <action> | ITEM: <item>"
)
prompt = make_prompt("shop assistant", "check stock", "apples")
print(prompt)
print("---")
print(fake_llm(prompt))
Try it yourself
Add a new goal, 'list low stock', and build a prompt for it.
Check
What are the three parts in this lesson?
Role, goal, and format.
Why give a format?
So the program can read the answer easily.
Hands-on: Programmatic Prompting for Agents III
What it is
Now add examples and rules. Examples show the model the exact answer shape. Rules say what not to do, for example 'answer only with ACTION and ITEM'. With examples and rules, the model is much less likely to add extra words or invent a new format.
Why it matters
Examples are the cheapest way to make a model steady. One good example often works better than a long description.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Prompting, part 3: add examples and rules, so the answer is steady.
EXAMPLES = "Example: 'check stock of apples' -> ACTION: check_stock | ITEM: apples"
def make_prompt(user_request):
return (
"You are a shop assistant.\n"
"Rules: answer only with ACTION and ITEM. Do not add extra words.\n"
+ EXAMPLES + "\n"
f"Request: {user_request}"
)
for request in ["check stock of apples", "hello"]:
print(make_prompt(request))
print("MODEL:", fake_llm(request))
print()
Try it yourself
Add a second example for 'hello' and run the code.
Check
What do examples do?
They show the model the exact answer shape to copy.
What do rules do?
They limit the answer, for example to only ACTION and ITEM.
Hands-on: Giving Agents Memory
What it is
Memory is a list of facts. After each step, the agent adds a fact, such as 'Apples in stock: 12.' Before the next step, it can look at that list. In this course, memory is a plain Python list. Later, a real system might use a file or a database, but the idea is the same: keep what you learned.
Why it matters
Without memory, the agent forgets each step and repeats the same work. With memory, it learns as it goes.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Memory: a simple list. The agent keeps what it learned, and uses it later.
memory = []
def remember(fact):
memory.append(fact)
print("Remembered:", fact)
def run_agent(user_request):
print("User:", user_request)
remember("User likes apples.")
remember("Apples in stock: 12.")
print("Memory so far:", memory)
print("Model:", fake_llm(user_request))
print("Final answer: We have 12 apples. I remembered you like apples.")
run_agent("check stock of apples")
Try it yourself
Add a third fact to memory, 'Bread in stock: 3.', and print the list again.
Check
How is memory stored in this lesson?
As a plain Python list of facts.
When does the agent add to memory?
After each step, when it learns a new fact.
Hands-on: Practicing Programmatic Prompting for Agents
What it is
Practice time. Write your own make_prompt function from scratch, without copying. It should take a task and an item, return a prompt string, and use an f-string. Then call it three times with different items and print each prompt and the fake model answer.
Why it matters
Practice turns reading into skill. Writing the function yourself is the test that you understood the idea.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Programmatic prompting: your program builds the prompt, not you typing it.
def make_prompt(task, item):
return f"You are a helpful shop assistant. Task: {task}. Item: {item}. Answer in one short sentence."
for task, item in [("check stock", "apples"), ("check stock", "bread")]:
prompt = make_prompt(task, item)
print("PROMPT:", prompt)
print("MODEL:", fake_llm(prompt))
print()
Try it yourself
Write the function from memory, then compare it with the code above. Fix one difference.
Check
What should your function take as inputs?
A task and an item.
What should it return?
A prompt string.
Hands-on: Practicing Programmatic Prompting for Agents — Solution
What it is
Here is one correct solution. Your solution may look a little different, and that is fine, as long as it builds a prompt with an f-string, takes a task and an item, and returns the prompt string. Compare your version line by line: does yours include the role, the task, and the item?
Why it matters
Checking your work against a solution shows you what you got right and what to fix next time.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Prompting, part 2: give the model a role, a goal, and a format.
def make_prompt(role, goal, item):
return (
f"Role: {role}.\n"
f"Goal: {goal}.\n"
f"Item: {item}.\n"
"Format: ACTION: <action> | ITEM: <item>"
)
prompt = make_prompt("shop assistant", "check stock", "apples")
print(prompt)
print("---")
print(fake_llm(prompt))
Try it yourself
Run the solution code, then change one word in it and run it again to see the effect.
Check
Does a solution have to match word for word?
No, as long as it does the same job.
What must it include?
A role, a task, and an item, built with an f-string.
Hands-on: Building Your First Agent
What it is
Your first agent is a small class. A class is a plan for an object. The Agent class has a name, a memory list, and a run method. The run method takes a user request, asks the fake model, and prints a final answer. One class, one job, easy to read and test.
Why it matters
A class keeps the agent's facts and actions in one place. Later lessons add tools and a GAME structure to this same class.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Your first agent: one small class that holds the loop and the memory.
class Agent:
def __init__(self, name):
self.name = name
self.memory = []
def remember(self, fact):
self.memory.append(fact)
def run(self, user_request):
print(f"{self.name} got: {user_request}")
self.remember(f"User asked: {user_request}")
answer = fake_llm(user_request)
print("Model:", answer)
if "check_stock" in answer:
self.remember("Apples in stock: 12.")
print("Final answer: We have 12 apples in stock.")
print("Memory:", self.memory)
agent = Agent("ShopAgent")
agent.run("check stock of apples")
Try it yourself
Create a second agent with a different name and run it with 'hello'.
Check
What does the Agent class hold?
A name and a memory list.
What does run() do?
It takes a request, asks the model, and prints a final answer.
Hands-on: AI Agent
What it is
This lesson puts the pieces together: a goal, a prompt, a model answer, and a final answer. The agent does not yet use tools. It reads the request, asks the model what to do, and answers. This is the simplest full agent, and it is the base for every later agent.
Why it matters
A simple full agent is easier to test than a big one. If this works, the later parts will work too.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Your first agent: one small class that holds the loop and the memory.
class Agent:
def __init__(self, name):
self.name = name
self.memory = []
def remember(self, fact):
self.memory.append(fact)
def run(self, user_request):
print(f"{self.name} got: {user_request}")
self.remember(f"User asked: {user_request}")
answer = fake_llm(user_request)
print("Model:", answer)
if "check_stock" in answer:
self.remember("Apples in stock: 12.")
print("Final answer: We have 12 apples in stock.")
print("Memory:", self.memory)
agent = Agent("ShopAgent")
agent.run("check stock of apples")
Try it yourself
Ask the agent about bread instead of apples. What changes in the output?
Check
What is the simplest full agent made of?
A goal, a prompt, a model answer, and a final answer.
Why start simple?
A simple agent is easy to test and fix.
Hands-on: Environment Interface
What it is
The environment is the world the agent acts in. In this course it is a ShopEnvironment class with a stock dictionary. The agent never changes the stock dictionary directly. It calls env.check_stock(item) or env.add_stock(item, amount). This wall between agent and world is called an interface.
Why it matters
An interface makes the agent safe and testable. You can swap a real shop for a test shop without changing the agent.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Environment Interface: the agent does NOT touch the shop directly.
# It talks to an Environment object. That makes testing easy.
class ShopEnvironment:
def __init__(self):
self.stock = {"apples": 12, "bread": 3, "milk": 0}
def check_stock(self, item):
return self.stock.get(item, 0)
def add_stock(self, item, amount):
self.stock[item] = self.stock.get(item, 0) + amount
return self.stock[item]
env = ShopEnvironment()
print("Apples:", env.check_stock("apples"))
print("Bread:", env.check_stock("bread"))
print("After adding 10 apples:", env.add_stock("apples", 10))
print("All stock:", env.stock)
Try it yourself
Add a new item, 'cheese', with 5 in stock, to the Environment and check it.
Check
What is the environment here?
A ShopEnvironment object with a stock dictionary.
Why not let the agent change stock directly?
So the agent is safe and easy to test.
Hands-on: AI Agent Feedback and Memory
What it is
Feedback means the result of an action. After the agent calls check_stock, it gets a number back, for example 12. It writes that fact into memory. Next time, it can use that fact without calling the tool again. Feedback plus memory is how an agent learns during a task.
Why it matters
Feedback closes the loop. Without it, the agent would not know if its action worked.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Feedback and Memory: after each tool use, the agent records the result.
class Agent:
def __init__(self, env):
self.env = env
self.memory = []
def run(self, user_request):
answer = fake_llm(user_request)
print("Model:", answer)
if "check_stock" in answer:
result = self.env.check_stock("apples")
self.memory.append(f"Checked apples: {result} in stock.")
print("Tool result:", result)
print("Final answer: We have", result, "apples.")
print("Memory:", self.memory)
class ShopEnvironment:
def __init__(self):
self.stock = {"apples": 12, "bread": 3}
def check_stock(self, item):
return self.stock.get(item, 0)
Agent(ShopEnvironment()).run("check stock of apples")
Try it yourself
Run the agent twice. Does the memory grow? Print the memory after each run.
Check
What is feedback?
The result that comes back after an action.
What does the agent do with feedback?
It writes it into memory, so it can use it later.
Module 2 — AI Agents, Tools, Actions, & Language
How agents use tools, how to name and describe tools well, and how the model and the code talk to each other.
13 lessons in this module.
Video: GAIL — Goals, Actions, Information, Language
What it is
GAIL is a short way to remember the four parts of an agent. Goals: what it must do. Actions: what it can do, usually tools. Information: what it knows, from memory and tool results. Language: how it talks to the model and to you. If one part is missing, the agent is weak.
Why it matters
GAIL gives you a checklist. When an agent fails, check each part: is the goal clear, are the actions right, is the information fresh, is the language clear?
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# GAIL: Goals, Actions, Information, Language.
# Four small parts. Each part has one job. Together they make an agent.
from dataclasses import dataclass, field
@dataclass
class Goal:
text: str
done: bool = False
@dataclass
class Agent:
goal: Goal
memory: list = field(default_factory=list)
def act(self, user_request):
print("Goal:", self.goal.text)
print("Information (memory):", self.memory)
answer = fake_llm(user_request)
print("Language - model said:", answer)
if "check_stock" in answer:
print("Action: check_stock('apples') -> 12 in stock.")
self.memory.append("Apples: 12")
self.goal.done = True
print("Goal done?", self.goal.done)
agent = Agent(goal=Goal("Answer the stock question"))
agent.act("check stock of apples")
Try it yourself
Change the goal text to 'List low stock items' and run the code.
Check
What do the letters in GAIL stand for?
Goals, Actions, Information, Language.
Which part holds what the agent knows?
Information, from memory and tool results.
Video: Giving Agents Tools
What it is
A tool is a Python function the agent can call, for example check_stock or add_stock. The model cannot run the function itself. It only picks the tool name. Your code runs the real function and gives the result back to the model. That split keeps the agent safe.
Why it matters
Tools let an agent touch the real world: check data, change stock, send a message. Without tools, an agent can only talk.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Tools in Python: plain functions, stored in a dictionary by name.
# The agent picks a tool by name, then calls it.
def check_stock(item):
"""Check how many of an item are in stock."""
stock = {"apples": 12, "bread": 3, "milk": 0}
return stock.get(item, 0)
def add_stock(item, amount):
"""Add items to stock. Returns the new total."""
return f"Added {amount} {item}."
TOOLS = {
"check_stock": check_stock,
"add_stock": add_stock,
}
# The model picks the tool name. The program runs the real function.
tool_name = "check_stock"
result = TOOLS[tool_name]("apples")
print("Tool:", tool_name)
print("Result:", result)
print("Tools available:", list(TOOLS))
Try it yourself
Add a new tool function, get_price, that returns a price, and add it to TOOLS.
Check
Who runs the tool function?
Your Python code, not the model.
What does the model do?
It picks which tool to use.
Video: Tool Descriptions and Naming
What it is
A tool needs a clear name and a clear description. A good name is a verb plus a thing: check_stock, add_stock, list_low_stock. A good description says what it does, what it needs, and what it returns, in one sentence. The model reads the description to choose the right tool.
Why it matters
Bad names confuse the model. It may pick the wrong tool, or a tool that does not fit the task. Clear names save you from that error.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Tools in Python: plain functions, stored in a dictionary by name.
# The agent picks a tool by name, then calls it.
def check_stock(item):
"""Check how many of an item are in stock."""
stock = {"apples": 12, "bread": 3, "milk": 0}
return stock.get(item, 0)
def add_stock(item, amount):
"""Add items to stock. Returns the new total."""
return f"Added {amount} {item}."
TOOLS = {
"check_stock": check_stock,
"add_stock": add_stock,
}
# The model picks the tool name. The program runs the real function.
tool_name = "check_stock"
result = TOOLS[tool_name]("apples")
print("Tool:", tool_name)
print("Result:", result)
print("Tools available:", list(TOOLS))
Try it yourself
Rewrite the description of check_stock in your own words, keeping it to one sentence.
Check
What makes a good tool name?
A verb plus a thing, for example check_stock.
What should a description say?
What the tool does, what it needs, and what it returns.
Video: Tool Results and Agent Feedback
What it is
After a tool runs, it returns a result. That result is feedback. The agent reads the feedback, writes it into memory, and decides the next step. For example, check_stock returns 12, so the agent knows it does not need to add stock. If it returned 0, the agent would add stock next.
Why it matters
Feedback is how the agent learns from the real world during a task, instead of guessing.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Feedback and Memory: after each tool use, the agent records the result.
class Agent:
def __init__(self, env):
self.env = env
self.memory = []
def run(self, user_request):
answer = fake_llm(user_request)
print("Model:", answer)
if "check_stock" in answer:
result = self.env.check_stock("apples")
self.memory.append(f"Checked apples: {result} in stock.")
print("Tool result:", result)
print("Final answer: We have", result, "apples.")
print("Memory:", self.memory)
class ShopEnvironment:
def __init__(self):
self.stock = {"apples": 12, "bread": 3}
def check_stock(self, item):
return self.stock.get(item, 0)
Agent(ShopEnvironment()).run("check stock of apples")
Try it yourself
Change the stock of apples to 0 in the code and see how the final answer should change.
Check
What is a tool result?
The value that comes back after a tool runs.
What does the agent do with it?
It reads it, remembers it, and picks the next step.
Reading: Try Out an Agent that Calls Python Functions
What it is
In this reading you run an agent that calls Python functions. The model says 'check_stock', your code finds that function in a dictionary, calls it with 'apples', and gets 12 back. Then your code tells the model the result, and the model writes the final answer for the user.
Why it matters
This is the core pattern of tool use. Once you see it work once, you can add any function as a tool.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Tools in Python: plain functions, stored in a dictionary by name.
# The agent picks a tool by name, then calls it.
def check_stock(item):
"""Check how many of an item are in stock."""
stock = {"apples": 12, "bread": 3, "milk": 0}
return stock.get(item, 0)
def add_stock(item, amount):
"""Add items to stock. Returns the new total."""
return f"Added {amount} {item}."
TOOLS = {
"check_stock": check_stock,
"add_stock": add_stock,
}
# The model picks the tool name. The program runs the real function.
tool_name = "check_stock"
result = TOOLS[tool_name]("apples")
print("Tool:", tool_name)
print("Result:", result)
print("Tools available:", list(TOOLS))
Try it yourself
Run the code, then call the add_stock tool by hand with ('bread', 5).
Check
How does the code find the right function?
It looks up the tool name in a dictionary called TOOLS.
What happens after the function runs?
The result goes back to the agent, so it can answer.
Reading: Try Out LLM Function Calling
What it is
LLM function calling is the same idea, with a standard format. The model does not just write text. It writes a structured call, such as a tool name and its inputs. Your code reads that call, runs the function, and returns the result. Real models can do this directly. In this course, fake_llm simulates it with text.
Why it matters
Function calling is the bridge between language and code. It turns words into actions, safely.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# LLM Function Calling (simulated): the model returns a tool call,
# your code reads it, runs the Python function, and gives the result back.
def check_stock(item):
stock = {"apples": 12, "bread": 3}
return stock.get(item, 0)
def list_low_stock():
return ["bread (3 left)", "milk (0 left)"]
TOOLS = {"check_stock": check_stock, "list_low_stock": list_low_stock}
def run_agent(user_request):
reply = fake_llm(user_request)
print("Model reply:", reply)
if "check_stock" in reply:
print("Calling check_stock('apples')...")
result = TOOLS["check_stock"]("apples")
print("Tool result:", result)
print("Final answer: We have", result, "apples in stock.")
elif "list_low_stock" in reply:
print("Tool result:", TOOLS["list_low_stock"]())
run_agent("check stock of apples")
run_agent("what is low in stock?")
Try it yourself
Run the code with the request 'what is low in stock?' and follow the printed steps.
Check
What does the model return in function calling?
A structured tool call: a tool name and its inputs.
What does your code do with it?
It runs the real Python function and returns the result.
Reading: Try Out an Agent Loop with Function Calling
What it is
Now put the loop and function calling together. Each loop step: the model picks a tool, your code runs it, the result goes back into the loop. The loop repeats until the goal is done. This is the full agent loop you will reuse in the capstone.
Why it matters
Loop plus tools is a real agent. It can do multi-step work, not just one call.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# LLM Function Calling (simulated): the model returns a tool call,
# your code reads it, runs the Python function, and gives the result back.
def check_stock(item):
stock = {"apples": 12, "bread": 3}
return stock.get(item, 0)
def list_low_stock():
return ["bread (3 left)", "milk (0 left)"]
TOOLS = {"check_stock": check_stock, "list_low_stock": list_low_stock}
def run_agent(user_request):
reply = fake_llm(user_request)
print("Model reply:", reply)
if "check_stock" in reply:
print("Calling check_stock('apples')...")
result = TOOLS["check_stock"]("apples")
print("Tool result:", result)
print("Final answer: We have", result, "apples in stock.")
elif "list_low_stock" in reply:
print("Tool result:", TOOLS["list_low_stock"]())
run_agent("check stock of apples")
run_agent("what is low in stock?")
Try it yourself
Add a third request, 'hello', and run the loop for it.
Check
What happens in each loop step?
The model picks a tool, the code runs it, the result goes back in.
When does the loop stop?
When the goal is done, or after the step limit.
Reading: Exercise: Extend the Function Calling Agent
What it is
Your task in this reading: add one new tool to the agent. Pick something simple, for example get_stock_of_all_items, which returns the whole stock dictionary. Add it to TOOLS, give it a clear description, and test it by asking the agent a question that needs it.
Why it matters
Extending an agent is the real skill. New tasks usually mean one new tool, not a new agent.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# LLM Function Calling (simulated): the model returns a tool call,
# your code reads it, runs the Python function, and gives the result back.
def check_stock(item):
stock = {"apples": 12, "bread": 3}
return stock.get(item, 0)
def list_low_stock():
return ["bread (3 left)", "milk (0 left)"]
TOOLS = {"check_stock": check_stock, "list_low_stock": list_low_stock}
def run_agent(user_request):
reply = fake_llm(user_request)
print("Model reply:", reply)
if "check_stock" in reply:
print("Calling check_stock('apples')...")
result = TOOLS["check_stock"]("apples")
print("Tool result:", result)
print("Final answer: We have", result, "apples in stock.")
elif "list_low_stock" in reply:
print("Tool result:", TOOLS["list_low_stock"]())
run_agent("check stock of apples")
run_agent("what is low in stock?")
Try it yourself
Add the get_stock_of_all_items tool described above, then run the agent.
Check
What is the first step to add a tool?
Write the Python function.
What is the second step?
Add it to the TOOLS dictionary with a clear description.
Assignment: Understanding the AI Agent Loop
What it is
This assignment checks the loop with tools. The model picks a tool. The code runs it. The result is feedback. The loop repeats. A good tool has a clear name and description. A bad tool name can make the model pick the wrong tool.
Why it matters
If you can trace one loop step on paper — model, tool, result, next step — you understand Module 2.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# The Agent Loop: think -> act -> see the result -> think again -> stop.
# The loop stops when the model says it is done, or after 3 steps.
def run_agent(user_request):
print("User:", user_request)
for step in range(1, 4):
prompt = f"Request: {user_request}. Step {step}. What should I do next?"
answer = fake_llm(prompt)
print(f"Step {step} - Model says: {answer}")
if "check_stock" in answer:
print("Step", step, "- Tool result: apples = 12 in stock.")
print("Final answer: We have 12 apples in stock.")
return
print("Final answer: Done.")
return
run_agent("check stock of apples")
Try it yourself
On paper, trace the loop for 'check stock of apples': write model, tool, result, final answer.
Check
Who picks the tool?
The model picks the name; the code runs it.
What is feedback in the loop?
The tool result, used to pick the next step.
Hands-on: Agent Tools in Python
What it is
Write three small tool functions: check_stock, add_stock, and list_low_stock. Each one does one job and returns a clear value. Store them in a dictionary named TOOLS. Test each one by calling it directly, before you let the agent use it.
Why it matters
Testing tools first saves time. If a tool works alone, it will work inside the agent.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Tools in Python: plain functions, stored in a dictionary by name.
# The agent picks a tool by name, then calls it.
def check_stock(item):
"""Check how many of an item are in stock."""
stock = {"apples": 12, "bread": 3, "milk": 0}
return stock.get(item, 0)
def add_stock(item, amount):
"""Add items to stock. Returns the new total."""
return f"Added {amount} {item}."
TOOLS = {
"check_stock": check_stock,
"add_stock": add_stock,
}
# The model picks the tool name. The program runs the real function.
tool_name = "check_stock"
result = TOOLS[tool_name]("apples")
print("Tool:", tool_name)
print("Result:", result)
print("Tools available:", list(TOOLS))
Try it yourself
Call each tool directly in the code and print the results.
Check
How many jobs should one tool do?
One job only.
Where do you store tools?
In a dictionary, by name.
Hands-on: Using Function Calling Capabilities with LLMs
What it is
Here you use the simulated function calling pattern: fake_llm returns a tool call as text, your code reads the tool name from it, runs the function, and prints the result. Change the request and watch which tool the model picks.
Why it matters
This pattern is the same one real models use. Learning it with a fake model first keeps it simple and free.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# LLM Function Calling (simulated): the model returns a tool call,
# your code reads it, runs the Python function, and gives the result back.
def check_stock(item):
stock = {"apples": 12, "bread": 3}
return stock.get(item, 0)
def list_low_stock():
return ["bread (3 left)", "milk (0 left)"]
TOOLS = {"check_stock": check_stock, "list_low_stock": list_low_stock}
def run_agent(user_request):
reply = fake_llm(user_request)
print("Model reply:", reply)
if "check_stock" in reply:
print("Calling check_stock('apples')...")
result = TOOLS["check_stock"]("apples")
print("Tool result:", result)
print("Final answer: We have", result, "apples in stock.")
elif "list_low_stock" in reply:
print("Tool result:", TOOLS["list_low_stock"]())
run_agent("check stock of apples")
run_agent("what is low in stock?")
Try it yourself
Try the requests 'check stock of apples' and 'what is low in stock?' and compare the tools picked.
Check
What does fake_llm simulate here?
A real model returning a tool call.
What does your code do next?
It runs the named Python function.
Hands-on: An Agent Loop with Function Calling
What it is
Build the full loop: for step in range(3), ask the model, read the tool name, run the tool, print the result, and stop when the goal is done. Add a print line at each step, so you can see the loop working.
Why it matters
Seeing each step printed is the best way to learn the loop. Later you can hide the prints, but keep them while you learn.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# LLM Function Calling (simulated): the model returns a tool call,
# your code reads it, runs the Python function, and gives the result back.
def check_stock(item):
stock = {"apples": 12, "bread": 3}
return stock.get(item, 0)
def list_low_stock():
return ["bread (3 left)", "milk (0 left)"]
TOOLS = {"check_stock": check_stock, "list_low_stock": list_low_stock}
def run_agent(user_request):
reply = fake_llm(user_request)
print("Model reply:", reply)
if "check_stock" in reply:
print("Calling check_stock('apples')...")
result = TOOLS["check_stock"]("apples")
print("Tool result:", result)
print("Final answer: We have", result, "apples in stock.")
elif "list_low_stock" in reply:
print("Tool result:", TOOLS["list_low_stock"]())
run_agent("check stock of apples")
run_agent("what is low in stock?")
Try it yourself
Add a print line that shows the step number at the start of each loop step.
Check
What are the loop steps with function calling?
Ask the model, run the tool, use the result, repeat.
Why print each step?
So you can see and fix the loop while you learn.
Hands-on: Agent Tool Design Best Practices
What it is
Good tool design: one job per tool, a clear verb-noun name, a one-sentence description, simple inputs, and a clear return value. Avoid tools that do too much, need unclear inputs, or return a long messy text. Write the description first, then the code.
Why it matters
Well-designed tools make the model choose correctly. Poorly designed tools cause most agent mistakes.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Tools in Python: plain functions, stored in a dictionary by name.
# The agent picks a tool by name, then calls it.
def check_stock(item):
"""Check how many of an item are in stock."""
stock = {"apples": 12, "bread": 3, "milk": 0}
return stock.get(item, 0)
def add_stock(item, amount):
"""Add items to stock. Returns the new total."""
return f"Added {amount} {item}."
TOOLS = {
"check_stock": check_stock,
"add_stock": add_stock,
}
# The model picks the tool name. The program runs the real function.
tool_name = "check_stock"
result = TOOLS[tool_name]("apples")
print("Tool:", tool_name)
print("Result:", result)
print("Tools available:", list(TOOLS))
Try it yourself
Pick one tool and rewrite its name and description to follow the rules above.
Check
What should you write first, code or description?
Write the description first, then the code.
What is a bad tool?
One that does too much, or has unclear inputs or a messy return.
Module 3 — GAME: A Conceptual Framework for AI Agents
GAME is a clear way to design an agent: Goals, Actions, Memory, Environment. Each part is separate, so you can change one part without breaking the others.
10 lessons in this module.
Video: Overview of the GAME Framework
What it is
GAME stands for Goals, Actions, Memory, Environment. Goals: what the agent must do. Actions: the tools it can use. Memory: what it has learned. Environment: the world it acts in, for example the shop stock. The Agent class connects the four parts.
Why it matters
GAME stops a big agent from becoming a mess. Each part has one job, so you can test and change each part alone.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# GAME: Goals, Actions, Memory, Environment.
# Each part is its own small class. The Agent class connects them.
class Goal:
def __init__(self, text):
self.text = text
self.done = False
class Memory:
def __init__(self):
self.facts = []
def add(self, fact):
self.facts.append(fact)
class Environment:
def __init__(self):
self.stock = {"apples": 12, "bread": 3}
def check_stock(self, item):
return self.stock.get(item, 0)
class Agent:
def __init__(self, goal_text):
self.goal = Goal(goal_text)
self.memory = Memory()
self.env = Environment()
def run(self, user_request):
print("Goal:", self.goal.text)
reply = fake_llm(user_request)
print("Model:", reply)
if "check_stock" in reply:
result = self.env.check_stock("apples")
self.memory.add(f"Apples: {result}")
print("Action result:", result)
self.goal.done = True
print("Memory:", self.memory.facts)
print("Goal done?", self.goal.done)
Agent("Answer the stock question").run("check stock of apples")
Try it yourself
Change the goal text to 'Check bread stock' and run the code.
Check
What do the letters in GAME stand for?
Goals, Actions, Memory, Environment.
What connects the four parts?
The Agent class.
Video: Simulating Agents in ChatGPT
What it is
You can test an agent idea by talking it through in a chat, before you write code. Pretend to be the agent: state the goal, pick an action, write the tool result, then pick the next action. This is called simulating. It shows if your goal, actions, and memory are clear enough.
Why it matters
Simulating saves coding time. If the idea is unclear in a chat, it will be unclear in code too.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# GAME: Goals, Actions, Memory, Environment.
# Each part is its own small class. The Agent class connects them.
class Goal:
def __init__(self, text):
self.text = text
self.done = False
class Memory:
def __init__(self):
self.facts = []
def add(self, fact):
self.facts.append(fact)
class Environment:
def __init__(self):
self.stock = {"apples": 12, "bread": 3}
def check_stock(self, item):
return self.stock.get(item, 0)
class Agent:
def __init__(self, goal_text):
self.goal = Goal(goal_text)
self.memory = Memory()
self.env = Environment()
def run(self, user_request):
print("Goal:", self.goal.text)
reply = fake_llm(user_request)
print("Model:", reply)
if "check_stock" in reply:
result = self.env.check_stock("apples")
self.memory.add(f"Apples: {result}")
print("Action result:", result)
self.goal.done = True
print("Memory:", self.memory.facts)
print("Goal done?", self.goal.done)
Agent("Answer the stock question").run("check stock of apples")
Try it yourself
On paper, simulate the agent for 'check stock of apples' in four short lines.
Check
What is simulating an agent?
Talking through the agent steps in a chat, before coding.
Why simulate first?
To check if the goal and actions are clear enough.
Reading: Try Out the Agent Framework
What it is
In this reading you run a small GAME agent. It has a Goal object, a Memory object, an Environment object, and an Agent that uses them. Run it, read the printed lines, and see which line comes from which part. That mapping is the whole framework.
Why it matters
Running a framework once is better than reading about it ten times. The printed lines show the parts working together.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# GAME: Goals, Actions, Memory, Environment.
# Each part is its own small class. The Agent class connects them.
class Goal:
def __init__(self, text):
self.text = text
self.done = False
class Memory:
def __init__(self):
self.facts = []
def add(self, fact):
self.facts.append(fact)
class Environment:
def __init__(self):
self.stock = {"apples": 12, "bread": 3}
def check_stock(self, item):
return self.stock.get(item, 0)
class Agent:
def __init__(self, goal_text):
self.goal = Goal(goal_text)
self.memory = Memory()
self.env = Environment()
def run(self, user_request):
print("Goal:", self.goal.text)
reply = fake_llm(user_request)
print("Model:", reply)
if "check_stock" in reply:
result = self.env.check_stock("apples")
self.memory.add(f"Apples: {result}")
print("Action result:", result)
self.goal.done = True
print("Memory:", self.memory.facts)
print("Goal done?", self.goal.done)
Agent("Answer the stock question").run("check stock of apples")
Try it yourself
Add a print line that shows the memory after the run.
Check
Which part holds the stock?
The Environment.
Which part holds what was learned?
The Memory.
Hands-on: Designing AI Agents with GAME
What it is
Design before you code. Write down, in plain words: 1) Goal, 2) Actions (tools), 3) Memory (what to keep), 4) Environment (what the agent can touch). Do this for a shop agent, then for an agent of your own. A one-page design is enough.
Why it matters
A short design stops you from coding the wrong agent. It also makes the code easier to write, because each part is already named.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# GAME: Goals, Actions, Memory, Environment.
# Each part is its own small class. The Agent class connects them.
class Goal:
def __init__(self, text):
self.text = text
self.done = False
class Memory:
def __init__(self):
self.facts = []
def add(self, fact):
self.facts.append(fact)
class Environment:
def __init__(self):
self.stock = {"apples": 12, "bread": 3}
def check_stock(self, item):
return self.stock.get(item, 0)
class Agent:
def __init__(self, goal_text):
self.goal = Goal(goal_text)
self.memory = Memory()
self.env = Environment()
def run(self, user_request):
print("Goal:", self.goal.text)
reply = fake_llm(user_request)
print("Model:", reply)
if "check_stock" in reply:
result = self.env.check_stock("apples")
self.memory.add(f"Apples: {result}")
print("Action result:", result)
self.goal.done = True
print("Memory:", self.memory.facts)
print("Goal done?", self.goal.done)
Agent("Answer the stock question").run("check stock of apples")
Try it yourself
Write a GAME design for a 'library book' agent: goal, actions, memory, environment.
Check
What do you write first in a GAME design?
The goal, in plain words.
How long should the design be?
One page is enough.
Hands-on: Simulating GAME Agents in Conversation
What it is
Simulate a GAME agent in a conversation with yourself or with a chatbot. You play the agent, the other side plays the user and the environment. Say the goal out loud, pick an action, get a result, update memory, and repeat. Note where you get stuck: that is where the design needs work.
Why it matters
Simulation finds unclear steps early, when they are cheap to fix.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# GAME: Goals, Actions, Memory, Environment.
# Each part is its own small class. The Agent class connects them.
class Goal:
def __init__(self, text):
self.text = text
self.done = False
class Memory:
def __init__(self):
self.facts = []
def add(self, fact):
self.facts.append(fact)
class Environment:
def __init__(self):
self.stock = {"apples": 12, "bread": 3}
def check_stock(self, item):
return self.stock.get(item, 0)
class Agent:
def __init__(self, goal_text):
self.goal = Goal(goal_text)
self.memory = Memory()
self.env = Environment()
def run(self, user_request):
print("Goal:", self.goal.text)
reply = fake_llm(user_request)
print("Model:", reply)
if "check_stock" in reply:
result = self.env.check_stock("apples")
self.memory.add(f"Apples: {result}")
print("Action result:", result)
self.goal.done = True
print("Memory:", self.memory.facts)
print("Goal done?", self.goal.done)
Agent("Answer the stock question").run("check stock of apples")
Try it yourself
Simulate the shop agent for 'add 10 apples' and write down each step.
Check
Who plays the environment in a simulation?
The other side, or you, giving tool results.
What do you note when you get stuck?
That step needs a clearer design.
Hands-on: Modular AI Agent Design
What it is
Modular means each part lives in its own place. Goals in one class, Memory in another, Environment in another, tools in their own file. The Agent class only connects them. If you change the Environment, you do not need to change the Agent.
Why it matters
Modular code is easier to test, fix, and reuse. You can use the same Memory class in many agents.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# GAME: Goals, Actions, Memory, Environment.
# Each part is its own small class. The Agent class connects them.
class Goal:
def __init__(self, text):
self.text = text
self.done = False
class Memory:
def __init__(self):
self.facts = []
def add(self, fact):
self.facts.append(fact)
class Environment:
def __init__(self):
self.stock = {"apples": 12, "bread": 3}
def check_stock(self, item):
return self.stock.get(item, 0)
class Agent:
def __init__(self, goal_text):
self.goal = Goal(goal_text)
self.memory = Memory()
self.env = Environment()
def run(self, user_request):
print("Goal:", self.goal.text)
reply = fake_llm(user_request)
print("Model:", reply)
if "check_stock" in reply:
result = self.env.check_stock("apples")
self.memory.add(f"Apples: {result}")
print("Action result:", result)
self.goal.done = True
print("Memory:", self.memory.facts)
print("Goal done?", self.goal.done)
Agent("Answer the stock question").run("check stock of apples")
Try it yourself
Move the Environment class into its own section in the code and test that it still works.
Check
What does modular mean?
Each part lives in its own place, with one job.
What is the benefit?
You can change one part without breaking the others.
Hands-on: Agent Loop Customization
What it is
The basic loop is the same, but you can change it for a task. Examples: stop early when stock is zero, ask the user a question in the middle, or try a second tool if the first fails. Customization means small changes to the loop, not a new loop each time.
Why it matters
Real tasks need small loop changes. Learning to change the loop safely is a key agent skill.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# The Agent Loop: think -> act -> see the result -> think again -> stop.
# The loop stops when the model says it is done, or after 3 steps.
def run_agent(user_request):
print("User:", user_request)
for step in range(1, 4):
prompt = f"Request: {user_request}. Step {step}. What should I do next?"
answer = fake_llm(prompt)
print(f"Step {step} - Model says: {answer}")
if "check_stock" in answer:
print("Step", step, "- Tool result: apples = 12 in stock.")
print("Final answer: We have 12 apples in stock.")
return
print("Final answer: Done.")
return
run_agent("check stock of apples")
Try it yourself
Change the loop so it stops early and prints 'Out of stock!' when the result is 0.
Check
What is loop customization?
Small changes to the loop for one task.
Give one example.
Stop early when stock is zero.
Hands-on: Implementing GAME in Code
What it is
Now write the GAME classes in Python: Goal, Memory, Environment, and Agent. Each class is short. The Agent run method uses the other three. Run it for apples, then for bread, and check the memory after each run.
Why it matters
Writing the classes yourself makes GAME real. It is no longer just letters; it is code you can reuse.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# GAME: Goals, Actions, Memory, Environment.
# Each part is its own small class. The Agent class connects them.
class Goal:
def __init__(self, text):
self.text = text
self.done = False
class Memory:
def __init__(self):
self.facts = []
def add(self, fact):
self.facts.append(fact)
class Environment:
def __init__(self):
self.stock = {"apples": 12, "bread": 3}
def check_stock(self, item):
return self.stock.get(item, 0)
class Agent:
def __init__(self, goal_text):
self.goal = Goal(goal_text)
self.memory = Memory()
self.env = Environment()
def run(self, user_request):
print("Goal:", self.goal.text)
reply = fake_llm(user_request)
print("Model:", reply)
if "check_stock" in reply:
result = self.env.check_stock("apples")
self.memory.add(f"Apples: {result}")
print("Action result:", result)
self.goal.done = True
print("Memory:", self.memory.facts)
print("Goal done?", self.goal.done)
Agent("Answer the stock question").run("check stock of apples")
Try it yourself
Run the GAME agent for bread by changing the item in the code.
Check
Which class holds the goal?
The Goal class.
Which class runs the loop?
The Agent class.
Hands-on: How Your Agent Communicates with the LLM: The Agent Language
What it is
Agent Language is the text the agent sends to the model and the text it gets back. It includes the goal, the memory facts, the last tool result, and the format for the answer. A clear Agent Language means the model gets all the facts it needs, in the same order every time.
Why it matters
The model can only act on what it is told. A clear, steady language gives it the right facts, every time.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Prompting, part 3: add examples and rules, so the answer is steady.
EXAMPLES = "Example: 'check stock of apples' -> ACTION: check_stock | ITEM: apples"
def make_prompt(user_request):
return (
"You are a shop assistant.\n"
"Rules: answer only with ACTION and ITEM. Do not add extra words.\n"
+ EXAMPLES + "\n"
f"Request: {user_request}"
)
for request in ["check stock of apples", "hello"]:
print(make_prompt(request))
print("MODEL:", fake_llm(request))
print()
Try it yourself
Build a prompt that includes one memory fact, then print it.
Check
What is Agent Language?
The text the agent sends to the model and gets back.
What should it include?
The goal, memory facts, the last result, and the answer format.
Hands-on: Putting It All Together: Document Your Code with a README Agent
What it is
Your last GAME task: a README agent. It lists the files in a folder, then writes a short README.md that names the project and lists the files. It uses the same loop, memory, and tools as the shop agent, with two new tools: list_files and write_readme.
Why it matters
This shows the pattern is general. The same agent shape works for a shop, a library, or a code project.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# README Agent: an agent that reads a project folder and writes a README.
# It uses the same loop, memory, and tools as before, with a new tool.
import pathlib
TOOLS = {}
def tool(func):
TOOLS[func.__name__] = func
return func
@tool
def list_files(folder="."):
"""List files in a folder."""
return [p.name for p in pathlib.Path(folder).iterdir()]
@tool
def write_readme(text):
"""Write text to README.md."""
pathlib.Path("README.md").write_text(text)
return "README.md written."
def run_readme_agent():
files = TOOLS["list_files"](".")
print("Files found:", files[:5])
text = "# My Project\n\nFiles: " + ", ".join(files[:5]) + "\n"
print(TOOLS["write_readme"](text))
print(text)
run_readme_agent()
Try it yourself
Run the README agent, then open the README.md file it wrote and read it.
Check
What does the README agent do?
It lists files and writes a short README.md.
What is the same as the shop agent?
The loop, memory, and tool pattern.
Module 4 — Agent Tool Management
How to keep tools organised as your agent grows: register tools with decorators, group them, and refactor without breaking the agent.
4 lessons in this module.
Reading: Try Out the README Agent with the Decorator
What it is
In this reading you run the README agent again, but now its tools use a decorator. A decorator is a line starting with @ above a function, for example @tool. That one line registers the function as a tool. You do not need to add it to a dictionary by hand.
Why it matters
Decorators keep the tool list correct by itself. When you add a function, it is a tool. When you remove it, it is gone.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Decorators: one line above a function registers it as a tool.
# No separate list to update by hand. The tool list builds itself.
TOOLS = {}
def tool(func):
"""Register a function as an agent tool."""
TOOLS[func.__name__] = func
func.description = func.__doc__ or ""
return func
@tool
def check_stock(item):
"""Check how many of an item are in stock."""
return {"apples": 12, "bread": 3}.get(item, 0)
@tool
def list_low_stock():
"""List items with low stock."""
return ["bread (3)", "milk (0)"]
print("Tools registered:", list(TOOLS))
print("check_stock('apples') ->", TOOLS["check_stock"]("apples"))
print("Description:", TOOLS["check_stock"].description)
Try it yourself
Run the code and check that both tools appear in the printed list.
Check
What is a decorator here?
A line starting with @ above a function, for example @tool.
What does @tool do?
It registers the function as a tool, by itself.
Hands-on: Keeping Agent Tools Up to Date with Python Decorators
What it is
Write the @tool decorator yourself. It takes a function, adds it to the TOOLS dictionary by its name, saves its description, and returns the function. Then add @tool above check_stock and list_low_stock. Print TOOLS to see both names appear.
Why it matters
A decorator is a small piece of code that changes a function. Here it does one job: register the tool.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Decorators: one line above a function registers it as a tool.
# No separate list to update by hand. The tool list builds itself.
TOOLS = {}
def tool(func):
"""Register a function as an agent tool."""
TOOLS[func.__name__] = func
func.description = func.__doc__ or ""
return func
@tool
def check_stock(item):
"""Check how many of an item are in stock."""
return {"apples": 12, "bread": 3}.get(item, 0)
@tool
def list_low_stock():
"""List items with low stock."""
return ["bread (3)", "milk (0)"]
print("Tools registered:", list(TOOLS))
print("check_stock('apples') ->", TOOLS["check_stock"]("apples"))
print("Description:", TOOLS["check_stock"].description)
Try it yourself
Add a third tool with @tool, for example get_price, and check it appears in TOOLS.
Check
What does the decorator save?
The function, by name, and its description.
Why is this better than a manual list?
The list builds itself, so it cannot go out of date.
Hands-on: Tool Organization for Agents
What it is
As tools grow, group them. Put stock tools together, file tools together, and message tools together. In code, that can mean one file per group, or one dictionary per group. Give each group a clear name, so you can find a tool fast.
Why it matters
Organisation matters when an agent has 20 tools, not 2. A clear grouping stops you from adding the same tool twice.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Decorators: one line above a function registers it as a tool.
# No separate list to update by hand. The tool list builds itself.
TOOLS = {}
def tool(func):
"""Register a function as an agent tool."""
TOOLS[func.__name__] = func
func.description = func.__doc__ or ""
return func
@tool
def check_stock(item):
"""Check how many of an item are in stock."""
return {"apples": 12, "bread": 3}.get(item, 0)
@tool
def list_low_stock():
"""List items with low stock."""
return ["bread (3)", "milk (0)"]
print("Tools registered:", list(TOOLS))
print("check_stock('apples') ->", TOOLS["check_stock"]("apples"))
print("Description:", TOOLS["check_stock"].description)
Try it yourself
Group your tools in the code with a comment line, for example '# Stock tools', above each group.
Check
How do you group tools?
By job: stock tools together, file tools together, and so on.
Why group them?
So you can find a tool fast and avoid adding it twice.
Hands-on: Refactoring Our README Agent
What it is
Refactoring means cleaning the code without changing what it does. For the README agent: use the @tool decorator for both tools, group the tools, remove any repeated code, and keep the run function short. Run it before and after, and check the output is the same.
Why it matters
Refactoring makes code easier to read and change later. The output must stay the same; only the code gets cleaner.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# README Agent: an agent that reads a project folder and writes a README.
# It uses the same loop, memory, and tools as before, with a new tool.
import pathlib
TOOLS = {}
def tool(func):
TOOLS[func.__name__] = func
return func
@tool
def list_files(folder="."):
"""List files in a folder."""
return [p.name for p in pathlib.Path(folder).iterdir()]
@tool
def write_readme(text):
"""Write text to README.md."""
pathlib.Path("README.md").write_text(text)
return "README.md written."
def run_readme_agent():
files = TOOLS["list_files"](".")
print("Files found:", files[:5])
text = "# My Project\n\nFiles: " + ", ".join(files[:5]) + "\n"
print(TOOLS["write_readme"](text))
print(text)
run_readme_agent()
Try it yourself
Refactor the README agent code: use @tool for both tools, then run it and compare the output.
Check
What is refactoring?
Cleaning the code without changing what it does.
How do you know it worked?
The output is the same before and after.
Module 5 — Rethinking How Software is Built in the Age of AI Agents
What changes when agents can build software, why hallucination can be useful, and how to build a full Inventory Management Agent as your capstone.
5 lessons in this module.
Video: Build the Impossible with AI Agents
What it is
Agents let one person build things that once needed a team. An agent can write code, test it, fix errors, and write the README, step by step. The person stays in charge: they set the goal, check the work, and decide what is good enough. The agent does the repeated work.
Why it matters
This changes what is possible for a beginner. A clear goal and a good loop can build a real, working program.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Capstone: Inventory Management Agent.
# Reuses the loop, memory, tools, GAME structure, and decorators.
TOOLS = {}
def tool(func):
TOOLS[func.__name__] = func
return func
class Environment:
def __init__(self):
self.stock = {"apples": 12, "bread": 3, "milk": 0}
def check_stock(self, item):
return self.stock.get(item, 0)
def add_stock(self, item, amount):
self.stock[item] = self.stock.get(item, 0) + amount
return self.stock[item]
ENV = Environment()
@tool
def check_stock(item):
"""Check stock for one item."""
return ENV.check_stock(item)
@tool
def add_stock(item, amount=10):
"""Add stock for one item."""
return ENV.add_stock(item, amount)
@tool
def list_low_stock():
"""List items with fewer than 5 in stock."""
return [k for k, v in ENV.stock.items() if v < 5]
class Agent:
def __init__(self):
self.memory = []
def run(self, user_request):
print("User:", user_request)
for step in range(1, 4):
reply = fake_llm(user_request)
print(f"Step {step}: {reply}")
if "check_stock" in reply:
result = check_stock("apples")
self.memory.append(f"Apples: {result}")
print("Final answer: We have", result, "apples.")
return
if "add_stock" in reply:
print("Final answer: Added 10 apples. Now:", add_stock("apples", 10))
return
if "list_low_stock" in reply:
print("Final answer: Low stock:", list_low_stock())
return
print("Final answer: I checked the inventory.")
agent = Agent()
agent.run("check stock of apples")
agent.run("what is low in stock?")
print("Stock now:", ENV.stock)
print("Memory:", agent.memory)
Try it yourself
Run the capstone code once, then read the printed steps and note which step is the loop.
Check
Who stays in charge when an agent builds software?
The person: they set the goal and check the work.
What does the agent do?
The repeated work, step by step.
Video: Rethinking How We Teach Innovation
What it is
When agents can build the first version fast, teaching changes. The skill is no longer typing every line. It is setting a clear goal, breaking it into steps, checking the result, and fixing what is wrong. That is what this course has taught you: goal, loop, tools, memory, and clear language.
Why it matters
Innovation becomes trying ideas fast, learning from the result, and improving. Agents make that cycle short.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# GAME: Goals, Actions, Memory, Environment.
# Each part is its own small class. The Agent class connects them.
class Goal:
def __init__(self, text):
self.text = text
self.done = False
class Memory:
def __init__(self):
self.facts = []
def add(self, fact):
self.facts.append(fact)
class Environment:
def __init__(self):
self.stock = {"apples": 12, "bread": 3}
def check_stock(self, item):
return self.stock.get(item, 0)
class Agent:
def __init__(self, goal_text):
self.goal = Goal(goal_text)
self.memory = Memory()
self.env = Environment()
def run(self, user_request):
print("Goal:", self.goal.text)
reply = fake_llm(user_request)
print("Model:", reply)
if "check_stock" in reply:
result = self.env.check_stock("apples")
self.memory.add(f"Apples: {result}")
print("Action result:", result)
self.goal.done = True
print("Memory:", self.memory.facts)
print("Goal done?", self.goal.done)
Agent("Answer the stock question").run("check stock of apples")
Try it yourself
Write a one-sentence goal for an agent you would like to build, then break it into three steps.
Check
What is the main skill now?
Setting a clear goal, breaking it into steps, and checking the result.
What do agents make faster?
Building a first working version to test an idea.
Video: Hallucination is a New Form of Computing
What it is
Hallucination means the model makes up a fact that is not true. That sounds only bad, but it can also be useful: the model can suggest a new idea, a new name, or a new way to solve a problem. The key is to check it. Use tools and real data to test what the model suggests, and keep only what is true.
Why it matters
A made-up idea is a starting point, not an answer. Tools and checks turn a guess into a tested result.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Feedback and Memory: after each tool use, the agent records the result.
class Agent:
def __init__(self, env):
self.env = env
self.memory = []
def run(self, user_request):
answer = fake_llm(user_request)
print("Model:", answer)
if "check_stock" in answer:
result = self.env.check_stock("apples")
self.memory.append(f"Checked apples: {result} in stock.")
print("Tool result:", result)
print("Final answer: We have", result, "apples.")
print("Memory:", self.memory)
class ShopEnvironment:
def __init__(self):
self.stock = {"apples": 12, "bread": 3}
def check_stock(self, item):
return self.stock.get(item, 0)
Agent(ShopEnvironment()).run("check stock of apples")
Try it yourself
Ask the fake model a question, then check its answer against the real stock in the code.
Check
What is hallucination?
The model makes up a fact that is not true.
How do you handle it?
Check it with tools and real data, and keep only what is true.
Video: New Ways to Access and Extract Information
What it is
Agents can read many sources: files, feeds, web pages, and databases. They can pull out the facts they need, for example a price or a stock number, and use them in the next step. The pattern is the same: get the information with a tool, put it in memory, and use it to act.
Why it matters
Information is only useful when the agent can get it, check it, and use it. Tools make that possible.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Environment Interface: the agent does NOT touch the shop directly.
# It talks to an Environment object. That makes testing easy.
class ShopEnvironment:
def __init__(self):
self.stock = {"apples": 12, "bread": 3, "milk": 0}
def check_stock(self, item):
return self.stock.get(item, 0)
def add_stock(self, item, amount):
self.stock[item] = self.stock.get(item, 0) + amount
return self.stock[item]
env = ShopEnvironment()
print("Apples:", env.check_stock("apples"))
print("Bread:", env.check_stock("bread"))
print("After adding 10 apples:", env.add_stock("apples", 10))
print("All stock:", env.stock)
Try it yourself
Use the Environment to get the stock of bread, then print it as a sentence.
Check
What can an agent read?
Files, feeds, web pages, databases, and other sources, through tools.
What does it do with the facts?
It puts them in memory and uses them to act.
Hands-on: The Inventory Management Agent
What it is
Capstone: build a full Inventory Management Agent. It uses everything from this course: the agent loop, memory, tools with @tool, the GAME structure, and the Environment. It can check stock, add stock, and list low stock. It asks the model what to do, runs the tool, remembers the result, and answers in plain words. Run it for three requests and check the stock and memory at the end.
Why it matters
This is your finished agent. If it works for a shop, the same shape works for a library, a warehouse, or any list of items you need to manage.
Note: Swap note: to use a real model, replace fake_llm with a real call. See the Setup section for the 5-line change. No key is needed to follow this course.
# A small simulated model. It does NOT call the internet.
# It reads keywords in the prompt and returns a fixed, clear answer.
# This lets every example in this course run offline, with no API key.
def fake_llm(prompt):
text = prompt.lower()
if "stock" in text and "apple" in text:
return "ACTION: check_stock | ITEM: apples"
if "stock" in text:
return "ACTION: check_stock | ITEM: apples"
if "add" in text or "restock" in text:
return "ACTION: add_stock | ITEM: apples | AMOUNT: 10"
if "low" in text:
return "ACTION: list_low_stock"
if "hello" in text or "hi" in text:
return "Hello! I am your agent. How can I help?"
return "I will check that for you step by step."
# Capstone: Inventory Management Agent.
# Reuses the loop, memory, tools, GAME structure, and decorators.
TOOLS = {}
def tool(func):
TOOLS[func.__name__] = func
return func
class Environment:
def __init__(self):
self.stock = {"apples": 12, "bread": 3, "milk": 0}
def check_stock(self, item):
return self.stock.get(item, 0)
def add_stock(self, item, amount):
self.stock[item] = self.stock.get(item, 0) + amount
return self.stock[item]
ENV = Environment()
@tool
def check_stock(item):
"""Check stock for one item."""
return ENV.check_stock(item)
@tool
def add_stock(item, amount=10):
"""Add stock for one item."""
return ENV.add_stock(item, amount)
@tool
def list_low_stock():
"""List items with fewer than 5 in stock."""
return [k for k, v in ENV.stock.items() if v < 5]
class Agent:
def __init__(self):
self.memory = []
def run(self, user_request):
print("User:", user_request)
for step in range(1, 4):
reply = fake_llm(user_request)
print(f"Step {step}: {reply}")
if "check_stock" in reply:
result = check_stock("apples")
self.memory.append(f"Apples: {result}")
print("Final answer: We have", result, "apples.")
return
if "add_stock" in reply:
print("Final answer: Added 10 apples. Now:", add_stock("apples", 10))
return
if "list_low_stock" in reply:
print("Final answer: Low stock:", list_low_stock())
return
print("Final answer: I checked the inventory.")
agent = Agent()
agent.run("check stock of apples")
agent.run("what is low in stock?")
print("Stock now:", ENV.stock)
print("Memory:", agent.memory)
Try it yourself
Run the capstone, then add a new item, 'cheese', with 2 in stock, and ask the agent what is low in stock.
Check
What does the capstone reuse?
The loop, memory, tools, GAME structure, and decorators.
What can it do?
Check stock, add stock, and list low stock.
Capstone checklist
Your capstone is the Inventory Management Agent in Module 5. Tick each item when it works.
What to do next
Well done — you built a full agent in Python, from a simple prompt to a full Inventory Management Agent.
- Keep your capstone code. Add one new tool of your own, for example a tool that returns the total value of the stock.
- Try the same agent for a different shop: change the items in the Environment, keep the loop and tools the same.
- Next course in this repo: MCP for AI Agents: Tools, Servers and Clients, in the Workshop folder. It shows how agents share tools across programs, using TypeScript instead of Python. You already know the ideas — loop, tools, memory, GAME — so you can focus on the new language and the new protocol.
- After MCP, the repo also has courses on Prompt Engineering, Claude Code, and Trustworthy AI. Take them one at a time.