Skip to main content

Chapter 33: Agentic AI Overview: Autonomous Agents, Planning, Memory and Multi-Agent Systems

MODULE 9 | WEEK 33​

Chapter 33: Agentic AI Overview: Autonomous Agents, Planning, Memory and Multi-Agent Systems​

In the previous chapter you built an agent. This chapter looks at the big picture: what Agentic AI is, how agents plan, how they remember, how many agents work together, and most importantly: how to make them safe and trustworthy. This is the last chapter of Module 9, so at the end there is a mini project and a summary of the module. We will first understand all the concepts with small Python code (with real outputs), which will later be easy to combine with a real LLM.

33.1 What is Agentic AI?​

Agentic AI means AI systems that do not only give answers. When they get a goal, they decide for themselves, run tools and complete work in several steps, with at least human guidance. Based on "how much freedom" they are given, the systems form a line:

LevelHow does it work?Example
1. ChatbotOne question, one answer. No toolAnswering an FAQ
2. Workflow / ChainSteps are fixed in advance (written by
you); the LLM works in each step
Read email -> summarize -> put in
category -> save
3. AgentThe LLM itself decides the next step andA research assistant that searches by
which tool to run (loop)itself and writes the result
4. Multi-agent systemSeveral specialist agents, a manager, orA team of Researcher + Writer +
agents talking to each otherReviewer
<mark>ANALOGY</mark>​

A workflow is a taxi driver who follows the fixed route you told him. An agent is a driver to whom you only tell the destination; he changes the route himself by looking at the traffic. The second driver is more unpredictable, but he can also go on the wrong road and burn more petrol.

<mark>REMEMBER</mark>​

First choose the simplest solution. If the steps are known in advance, use a workflow (cheap, fast, predictable). Use an agent only when the route cannot be fixed in advance. More freedom = more cost, more chance of mistakes, and less control.

33.2 The Heart of an Agent: Perceive - Think - Act and ReAct​

The basic cycle of an agent (seen in the picture of Chapter 32):

Goal -> [ Perceive (look at the situation) -> Think (reason/decide) -> Act (run a tool) ] -> Observe (see the result) -> repeat

The famous way of doing this cycle is ReAct (Reason + Act): at each step the model writes a Thought (reasoning) and an Action (tool), and after seeing the tool's Observation (result), it decides the next step. Below, we build the ReAct structure with a scripted policy in place of the LLM (in a real LLM only policy() is replaced):

import ast, operator
KB = {"total_weeks": "49", "modules6_9_weeks": "5"} # store of course information
OPS = {ast.Add: operator.add, ast.Sub: operator.sub,
ast.Mult: operator.mul, ast.Div: operator.truediv}
def _ev(n):
if isinstance(n, ast.Constant):
return n.value
if isinstance(n, ast.BinOp) and type(n.op) in OPS:
return OPS[type(n.op)](_ev(n.left), _ev(n.right))
raise ValueError("not allowed")
def tool_lookup(key):
return KB.get(key, "not found")
def tool_calc(expr):
return str(round(_ev(ast.parse(expr, mode="eval").body), 2))
TOOLS = {"lookup": tool_lookup, "calc": tool_calc}
def policy(question, history):
"""In place of the LLM: look at what has been found so far and say the next step."""
obs = [o for _, _, o in history]
if len(obs) == 0:
return "First I need the combined weeks of Modules 6 and 9.", ("lookup", "modules6_9_weeks")
if len(obs) == 1:
return "Now I need the weeks of the whole course.", ("lookup", "total_weeks")
if len(obs) == 2:
return "Now I calculate the percentage.", ("calc", f"{obs[0]} / {obs[1]} * 100")
return "I have the answer.", None
def react(question, max_steps=6):
history = []
for step in range(1, max_steps + 1):
thought, action = policy(question, history)
print(f"Thought {step}: {thought}")
if action is None:
print(f"Final Answer: Modules 6 and 9 together are {history[-1][2]}% of the whole course.")
return
name, arg = action
obs = TOOLS[name](arg)
print(f"Action {step}: {name}[{arg}]")
print(f"Observ. {step}: {obs}")
history.append((thought, action, obs))
print("max_steps finished, no answer found")
react("What percentage of the course do Modules 6 and 9 cover together?")
OUTPUT​
Thought 1: First I need the combined weeks of Modules 6 and 9.
Action 1: lookup[modules6_9_weeks]
Observ. 1: 5
Thought 2: Now I need the weeks of the whole course.
Action 2: lookup[total_weeks]
Observ. 2: 49
Thought 3: Now I calculate the percentage.
Action 3: calc[5 / 49 * 100]
Observ. 3: 10.2
Thought 4: I have the answer.
Final Answer: Modules 6 and 9 together are 10.2% of the whole course.
<mark>NOTE</mark>​

• This structure is the same as what the Runner of the Agents SDK does inside: think, ask for a tool, see the result, repeat. The difference: in a real agent, the LLM decides the next step, so it works also on new, unknown questions, while our policy was written only for one question.

33.3 Planning: Task Decomposition and Reflection​

Breaking a big task into small steps (sub-tasks) is planning. Two common methods:

MethodHow?When is it good?
ReAct (step by step)Think at each step, decide the next step
there
Unknown, changing work
Plan-and-ExecuteFirst make the whole plan, then run the
steps (re-plan on a failure)
Long, big, ordered work
Reflection (self-check)The agent checks and improves its own
answer (critic)
Where quality is important

Earlier steps often have order conditions ("first clean the data, then the model"). This is called a dependency graph, and Python's graphlib finds the correct order by itself. We also see what happens when a step fails and the agent changes the plan:

from graphlib import TopologicalSorter
# each task:
which tasks must be done before it
plan = {
"collect_data": [],
"clean_data": ["collect_data"],
"make_chart": ["clean_data"],
"train_model": ["clean_data"],
"evaluate": ["train_model"],
"write_report": ["evaluate", "make_chart"],
}
order = list(TopologicalSorter(plan).static_order())
print("Order of the plan:")
for i, step in enumerate(order, 1):
print(f" {i}. {step}")
def execute(step, attempt=1):
if step == "train_model" and attempt == 1:
return False, "GPU memory finished" # first try fails
return True, "done"
print()
for step in order:
ok, msg = execute(step)
if not ok:
print(f"[FAIL] {step}: {msg} -> REPLAN: choose a smaller model")
ok, msg = execute(step, attempt=2)
print(f"[{'OK' if ok else 'FAIL'}] {step}")
OUTPUT​
Order of the plan:
1. collect_data
2. clean_data
3. make_chart
4. train_model
5. evaluate
6. write_report
[OK] collect_data
[OK] clean_data
[OK] make_chart
[FAIL] train_model: GPU memory finished -> REPLAN: choose a smaller model
[OK] train_model
[OK] evaluate
[OK] write_report

Reflection means a second step after writing the answer: "Is this answer correct and complete? Does it need improvement?" In this, a writer and a critic are often two separate prompts (or agents). The code for this is in 33.6.

33.4 Memory: How does an agent remember?​

An LLM has no memory of its own (Chapter 31). The agent gets its memory from your system. Types of memory:

TypeWhat is it?Where does it stay?Example
Short-termThe current conversationContext window (list of
messages)
The last 10 messages
Working memoryResults and notes during the
work
State of the run / scratchpadTool results, state of the plan
Long-term: factsCertain information about
the user
Database / vector store"Sara lives in Karachi,
learning Python"
Long-term: eventsRecord of past thingsVector store / logs"Attention test was given last
week"
ProceduralWays of doing workInstructions, skill files"Always run the calculator
first"

Short-term: window and summary​

When the context window fills up, the old talk is turned into a summary and the recent messages are kept in full:

class ConversationMemory:
def __init__(self, max_recent=3):
self.max_recent = max_recent
self.messages, self.summary = [], ""
def add(self, role, text):
self.messages.append((role, text))
while len(self.messages) > self.max_recent:
old_role, old_text = self.messages.pop(0) # the oldest one
# in a real system an LLM makes a summary here we add a short note
self.summary += f"{old_role}: {old_text[:20]}...; "
def context(self):
recent = "\n".join(f"{r}: {t}" for r, t in self.messages)
return f"[Summary so far] {self.summary or 'none'}\n{recent}"
mem = ConversationMemory(max_recent=3)
for role, text in [("user", "Mera naam Sara hai"),
("assistant", "Khushi hui Sara!"),
("user", "Mujhe attention samjhao"),
("assistant", "Attention ek soft lookup hai"),
("user", "Aur multi-head?")]:
mem.add(role, text)
print(mem.context())
OUTPUT​
[Summary so far] user: Mera naam Sara hai...; assistant: Khushi hui Sara!...;
user: Mujhe attention samjhao
assistant: Attention ek soft lookup hai
user: Aur multi-head?

Long-term: vector memory​

In long-term memory, every fact is stored as a vector, and when needed the most similar memories are found (like Chapter 30.7). Here is a small version with TF-IDF:

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
class VectorMemory:
def __init__(self):
self.items = []
def remember(self, text):
self.items.append(text)
def recall(self, query, k=2):
vec = TfidfVectorizer().fit(self.items + [query])
sims = cosine_similarity(vec.transform([query]), vec.transform(self.items))[0]
top = sims.argsort()[::-1][:k]
return [(round(float(sims[i]), 2), self.items[i]) for i in top]
memory = VectorMemory()
for fact in ["Sara ka shehar Karachi hai",
"Sara Python seekh rahi hai aur test me attention topic kamzor tha",
"Sara ko subah ki class pasand hai",
"Sara ka project idea cricket data par tha"]:
memory.remember(fact)
for s, text in memory.recall("attention ka test kaisa tha"):
print(s, "|", text)
OUTPUT​
0.32 | Sara Python seekh rahi hai aur test me attention topic kamzor tha
0.2 | Sara ka project idea cricket data par tha

SAFE PRACTICES​

  • Remember only what is necessary, and give the user the right to see and delete it (privacy).

  • Memory can be wrong or old; keep a date with it and update it.

  • Hidden instructions (prompt injection) can also come in through memory; remembered things are data, not orders.

33.5 Tools, Environment and MCP​

Type of toolWorkExampleRisk
InformationSearching/readingWeb search, database, RAGWrong or dangerous source
CalculationRunning codeCalculator, Python sandboxDamage through code
(sandbox is necessary)
ActionDoing something in the worldEmail, calendar, payment, file
delete
The most: cannot be undone
PerceptionSeeing/hearingImage, audio, screenWrong interpretation
Computer useRunning a browser/appFilling a form, clicking on a
page
Clicking the wrong place,
personal data

MCP (Model Context Protocol) is an open standard that gives one common way to connect AI applications with tools and data. Instead of writing a separate workaround for each tool, an MCP server is built once, and any MCPsupporting app (many SDKs, including the OpenAI Agents SDK) can use it. An example is the USB: one port connects a printer, mouse and keyboard.

DIAGRAM: AI app / Agent (MCP client) <--- MCP ---> MCP server -> Files / GitHub / Database / Calendar

SAFE PRACTICES​

  • Least privilege: give the agent only the tools it needs, and read-only permission wherever possible.

  • Before any irreversible action (sending money, email, delete), ask for human approval.

  • Sandbox: run a tool that executes code in a separate, safe environment.

  • Set limits: a timeout, rate limit and cost limit on every tool.

33.6 Multi-Agent Systems​

Why many agents? Each agent gets a small, clear job (better instructions, fewer tools, smaller context), the work can be done in parallel, and one agent can check another's work. Disadvantages: more cost, more complexity, and one agent's mistake can spread to the next. Famous patterns:

DIAGRAM: Manager -> Worker A, Worker B -> Manager combines results; Triage -> Specialist; Researcher -> Writer -> Editor; Writer <-> Critic

PatternHow does it work?When?
Manager-WorkerThe manager divides the work, workersBig work, many experts
do it, the manager joins the results
Triage + HandoffThe first agent looks at the question andSupport, different departments
gives it to the right specialist
PipelineOne after another (Researcher -> WriterWork with an order
-> Editor)
Writer-Critic loopWrite, check, improve, repeatWhere quality is important

In Chapter 32.8 we saw handoffs and agents-as-tools with the SDK. Now a team of Researcher -> Writer -> Reviewer in pure Python, in which the writer improves the draft based on the reviewer's objection:

FACTS = {"attention": ["every token looks at every other token", "weights are made by softmax"]}
def researcher(topic):
return FACTS.get(topic, [])
def writer(topic, facts, feedback=None):
text = f"About {topic.title()}: " + "; ".join(facts) + "."
if feedback == "example needed":
text += " Example: the attention of 'wo' on 'Sara'."
return text
def reviewer(draft):
if "Example" not in draft:
return False, "example needed"
return True, "looks good"
def team(topic, max_rounds=3):
facts = researcher(topic)
print("Researcher found:", facts)
feedback = None
for round_no in range(1, max_rounds + 1):
draft = writer(topic, facts, feedback)
approved, feedback = reviewer(draft)
verdict = "APPROVED"
if approved
else "objection: " + feedback
print(f"Round {round_no}: reviewer -> {verdict}")
if approved:
return draft
return draft
print()
print(team("attention").replace(" Example", "\nExample"))

OUTPUT Researcher found: ['every token looks at every other token', 'weights are made by softmax'] Round 1: reviewer -> objection: example needed Round 2: reviewer -> APPROVED About Attention: every token looks at every other token; weights are made by softmax. Example: the attention of 'wo' on 'Sara'.

OUTPUT​

In a real system, researcher , writer and reviewer are all LLM agents (with the Agents SDK, three Agent s and a manager that runs them with as_tool or handoffs). max_rounds is the safety limit that always guarantees the loop finishes.

33.7 Safety, Reliability and Evaluation​

Making agents is easy; making them trustworthy is hard. First see how mistakes spread: if the probability that each step is correct is p, then after n steps the probability that the whole task is correct is p^n:

print("step | p=0.99 p=0.95 p=0.90")
for n in [1, 5, 10, 20]:
row = " ".join(f"{p ** n:6.2f}" for p in [0.99, 0.95, 0.90])
print(f"{n:5d} | {row}")
OUTPUT​
step | p=0.99 p=0.95 p=0.90
1 | 0.99 0.95 0.90
5 | 0.95 0.77 0.59
10 | 0.90 0.60 0.35
20 | 0.82 0.36 0.12

See: even if each step is 95% correct, after 20 steps the whole task is only about 36% correct. So long agents need small steps, checks and a way to go back. The main risks and their solutions:

RiskHow does it happen?Protection
Endless loopThe agent runs the same tool again and
again
max_turns, budget and loop detection
Wrong tool/argumentsThe model chooses the wrong function
or input
Strict schema, validation, clear tool
names
Hallucinated actionThe agent says "I sent the email" when it
did not
Trust the real result of the tool; check
the answer that says it was done
Prompt injectionA hidden instruction in a web page or
email misleads the agent ("send your
data")
External text is only data; limited tool
permissions; approval
Too much permissionThe agent has the power to delete or
pay
Least privilege, sandbox
CostLong runs, big modelsToken/money limits, smaller model,
caching
PrivacyPersonal data in logs/tracesAnonymize, limit how long it is kept

Human-in-the-loop: approval​

The easiest and safest protection is to ask a human for permission before running risky actions:

RISKY = {"send_email", "delete_file", "make_payment"}
def run_action(tool, args, approve):
if tool in RISKY and not approve(tool, args):
return f"STOPPED: no human permission for {tool}"
return f"{tool}({args}) ran"
always_no = lambda tool, args:
False
always_yes = lambda tool, args:
True
print(run_action("search_web", {"q": "attention"}, always_no)) # safe: direct
print(run_action("send_email", {"to": "sir@example.com"}, always_no))
print(run_action("send_email", {"to": "sir@example.com"}, always_yes))
OUTPUT​
search_web({'q': 'attention'}) ran
STOPPED: no human permission for send_email
send_email({'to': 'sir@example.com'}) ran

Evaluation: testing the agent​

Saying "it ran" about an agent is not enough. Make a test set (10 to 50 questions whose correct answer is known in advance) and run it again after every change (prompt, tool or model). This is called regression testing. Measures to check: success rate, choosing the right tool, number of steps, cost, time, and staying safe:

def mini_agent(question):
"""Small imitation agent: runs a tool based on keywords."""
q = question.lower()
if "week" in q and "29" in q:
return "Transformers aur Attention Mechanism"
if "week" in q and "32" in q:
return "OpenAI Agent SDK aur Function Calling"
if "percent" in q:
return "10.2"
return "mujhe maloom nahi"
tests = [
("Week 29 me kya hai?", "attention"),
("Week 32 me kya hai?", "function calling"),
("49 me se 5 kitne percent?", "10.2"),
("Week 30 me kya hai?", "llm"), # the agent does not know this
("Biryani ki recipe?", "maloom nahi"), # correctly refusing is also a success
]
passed = 0
for q, expected in tests:
answer = mini_agent(q)
ok = expected in answer.lower()
passed += ok
print(f"{'PASS' if ok else 'FAIL'} | {q:28s} -> {answer}")
print(f"\nSuccess rate: {passed}/{len(tests)} = {100 * passed / len(tests):.0f}%")
OUTPUT​
PASS | Week 29 me kya hai? -> Transformers aur Attention Mechanism
PASS | Week 32 me kya hai? -> OpenAI Agent SDK aur Function Calling
PASS | 49 me se 5 kitne percent? -> 10.2
FAIL | Week 30 me kya hai? -> mujhe maloom nahi
PASS | Biryani ki recipe? -> mujhe maloom nahi
Success rate: 4/5 = 80%

The FAIL line is the most valuable: it shows where the agent lacks a tool or information. Keep growing the test set over time and add the real wrong answers of real users to it.

ETHICS / RESPONSIBILITY​

  • Transparency: tell the user that they are talking to an AI, and which action the agent took.

  • Humans for important decisions: in matters of jobs, loans, health and law, AI only helps; the final decision is human.

  • Bias and fairness: measure the performance of the agent separately for different languages and groups.

  • Personal data: take only the necessary data, keep it safe, and give the right to delete it.

  • Accountability: decide in advance who is responsible for a mistake, and keep logs.

  • Misuse: stay away from making agents for deepfakes, fraud or spam.

33.8 Real-world Use Cases and Career​

Use caseWhat does the agent do?ToolsRisk level
Customer supportUnderstands questions,
checks orders, answers,
hands to a human when
needed
FAQ/RAG, order database,
ticket system
Medium
Research assistantSearches for sources on a
question, reads, makes a
summary
Web search, documentsLow to medium
Coding agentWrites code, runs tests, fixes
bugs
Files, terminal, testsHigh (sandbox necessary)
Data analysis agentCleans data, makes charts
and reports
Python/Pandas, SQLMedium
Education tutorAnswers questions, gives
practice, remembers progress
Quiz bank, memory, RAGLow
Business operationsSorts leads, invoices,
scheduling
CRM, email, calendarHigh (irreversible actions)

Opportunities in Pakistan: customer support in Urdu/Roman Urdu (online shops, WhatsApp), a FAQ agent for a university or institute, proposal and report helpers for freelancers, sales and data analysis for small businesses, and tutors in education. In all of these, the right language, culture and data protection make a real difference.

Path to learn (after this module)

  • Be strong in Python + APIs + async (the essence of Modules 1 to 6).

  • Run prompts, RAG and structured output on your own files.

  • Build the habit of evaluation: a test set for every project.

  • Deployment: an app with FastAPI or Streamlit , Docker, and logging (in Module 7 MLOps).

  • Portfolio: 3 to 4 good projects, a clean README and a demo video on GitHub/LinkedIn.

33.9 Mini Project: Course Assistant Agent​

Goal: make a Course Assistant that answers the questions of the students of this AI and Data Science course, does calculations, gives practice, and remembers the conversation. This project synthesizes Chapters 32 and 33 and uses the RAG concepts from Chapter 30.

PartRequirementFrom where?
Knowledge (RAG)Answers from the syllabus and your own
notes, with sources
Chapter 30.7-30.8
ToolsSyllabus lookup, calculator, quiz maker,
study planner
Chapter 32.5
MemoryStudent's name, weak topics (session +
vector memory)
Chapter 32.9, 33.4
GuardrailsOnly course questions; refuse personal
data
Chapter 32.9
Safetymax_turns, cost limit, approval for
actions that need permission
Chapter 33.7
EvaluationA test set of at least 15 questions and
the success rate
Chapter 33.7

Deliverables​

  1. Working code (Colab notebook or .py ) and a README (what it does, how to run it).

  2. Architecture diagram (picture): tools, memory, RAG, guardrails.

  3. Test set and success rate report (how many PASS, reasons for FAIL).

  4. Demo: a 2 to 3 minute screen recording.

RubricMarks
The agent does the right work (tools, RAG, memory)35
Safety and guardrails (limits, approval, cost limit)20
Evaluation (test set, result, analysis)20
Code cleanliness and README15
Demo and explanation10
Total100

33.10 Module 9 Summary​

ChapterMain ideaImportant code / tool
32: Agents SDKFunction calling,@function_tool,
Runner, handoffs, sessions
Agent,Runner,function_tool
33: Agentic AIReAct, planning, memory, multi-agent,
safety and evaluation
ReAct loop,graphlib, vector memory

Next step: In Module 7 (Weeks 34 to 40) you will learn MLOps: taking the model to the real world with an API (FastAPI), Docker, MLflow, CI/CD and logging. Now you have a complete toolbox from Python to Generative and Agentic AI.

33.11 Common Mistakes and Hands-on​

<mark>COMMON MISTAKES</mark>​
  • <mark>Making an agent for every problem, when a simple workf</mark> l <mark>ow is enough.</mark>

  • Leaving the agent unrestricted (no limits, no approval, no budget).

  • Not doing evaluation, and trusting "it worked in my example".

  • Long, multi-step work without checks and a way back (the effect of p^n).

  • Keeping everything in memory (privacy, and old or wrong information).

  • Treating outside text (web, email) as an order (prompt injection).

  • Using many agents in multi-agent when not needed (cost and confusion).

<mark>HANDS-ON</mark>​
  • <mark>ReAct: in 33.2 add a new fact to</mark> KB <mark>and change</mark> policy <mark>for one more step.</mark>

  • Plan: add a new task "deploy" to plan that depends on evaluate ; see the order.

  • Memory: add 5 more memories to VectorMemory and test recall with 3 different questions.

  • Multi-agent: add one more rule to reviewer (for example, a sentence limit) and see how many rounds it takes.

  • Reliability: extend the p^n table to p = 0.98 and n = 50; what result did you get?

  • Evaluation: add 5 more questions to tests and see the success rate; write down the reason for each FAIL.

<mark>KEY TAKEAWAYS</mark>​
  • <mark>In Agent</mark> i <mark>c AI, the LLM takes a goal, decides the steps by itself, runs tools, and completes work in several steps.</mark>

  • Chatbot -> Workflow -> Agent -> Multi-agent: freedom increases, and so do cost and risk; always choose the simplest solution first.

  • ReAct: the cycle of Thought -> Action -> Observation; planning (plan-and-execute) and reflection (critic).

  • Memory: short-term (context), working, long-term (facts/events, vector memory). Keep privacy and the risk of wrong memory in mind.

  • MCP is an open standard for connecting tools and data; least privilege and human approval are necessary.

  • Multi-agent patterns: manager-worker, triage + handoff, pipeline, writer-critic.

  • Reliability: the effect of p^n; max_turns, budget, guardrails, tracing and evaluation (test set) are all necessary.

  • Responsibility: transparency, personal data, bias, accountability and avoiding misuse.