Skip to main content
GACS will never ask for your seed phrase, private keys, or payment. free and ad-free.
Back to lessonLab · Module 11 / 12
Lab

Advanced Agent Patterns

Hands-on lab with starter code you can copy + run, and guided steps to produce the artifact for this module.

Setup

  1. Open a fresh terminal in a new folder, e.g. mkdir lab-11 && cd lab-11.
  2. Create a Python 3.11 venv: python -m venv .venv && source .venv/bin/activate.
  3. Install the dependencies noted on each starter snippet (the small pip install … line above the code).
  4. Copy each starter into a file with the suggested name, then run it and capture the output.

Starter code

01 · LangGraph state machine — plan / act / reflect
pip install langgraph langchain-openai
save as: 11_01_lab.py
python
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?"}))
Run with: python 11_01_lab.py
02 · Self-reflection / critic loop
save as: 11_02_lab.py
python
def 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}")
Run with: python 11_02_lab.py

Guided steps

Work through each step in order. Stop when your output matches the expected artifact — then push your work to your lab folder.

  1. 1
    LangGraph 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:
  2. 2
    Critic 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:
  3. 3
    Hierarchical 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.