Advanced Agent Patterns
Hands-on lab with starter code you can copy + run, and guided steps to produce the artifact for this module.
Setup
- Open a fresh terminal in a new folder, e.g.
mkdir lab-11 && cd lab-11. - Create a Python 3.11 venv:
python -m venv .venv && source .venv/bin/activate. - Install the dependencies noted on each starter snippet (the small pip install … line above the code).
- Copy each starter into a file with the suggested name, then run it and capture the output.
Starter code
from typing import TypedDict, List
from langgraph.graph import StateGraph, END
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-5-mini")
class S(TypedDict):
question: str
plan: List[str]
answer: str
def plan(s):
s["plan"] = llm.invoke(f"List 3 steps to answer: {s['question']}").content.split("\n")
return s
def act(s):
s["answer"] = llm.invoke(f"Execute plan:\n{s['plan']}").content
return s
def reflect(s):
fb = llm.invoke(f"Is this answer complete? {s['answer']}").content
return END if "yes" in fb.lower() else "plan"
g = StateGraph(S)
g.add_node("plan", plan); g.add_node("act", act)
g.set_entry_point("plan")
g.add_edge("plan", "act")
g.add_conditional_edges("act", reflect, {"plan":"plan", END:END})
print(g.compile().invoke({"question":"How does RoPE work?"}))python 11_01_lab.pydef reflect_and_revise(question, draft, llm):
critique = llm(f"Critique this answer for errors:\nQ:{question}\nA:{draft}")
if "no issues" in critique.lower():
return draft
return llm(f"Revise answer using critique.\nQ:{question}\nA:{draft}\nCritique:{critique}")python 11_02_lab.pyGuided steps
Work through each step in order. Stop when your output matches the expected artifact — then push your work to your lab folder.
- 1LangGraph plan-act-reflect
Do — Build the 3-node LangGraph state machine; allow it to loop back to plan if reflect fails.
Expected artifact — A graph that resolves a multi-step research question within ≤3 loops.
Run free: - 2Critic loop
Do — Add a critic that revises every answer once. Compare answer quality before/after on 15 prompts.
Expected artifact — critic.md with side-by-side answers and a +N quality delta.
Run free: - 3Hierarchical agents
Do — Build a manager agent that dispatches sub-tasks to specialist sub-agents.
Expected artifact — Transcript on a research task showing manager → researcher → writer hand-offs.
Run free:
Authoritative sources
Independent primary sources used to check and corroborate the guidance on this page.
- NIST National Initiative for Cybersecurity Education (NICE)
US national framework for cybersecurity work roles and competencies.
- Financial Action Task Force (FATF)
Global AML/CFT standards referenced by financial-crime curricula.
- FBI Internet Crime Complaint Center — Annual Reports
Primary loss and typology data used in fraud-investigation training.
- US Federal Trade Commission — Consumer Sentinel Data Book
Official complaint statistics for teaching fraud trend analysis.
