Day 05 Mastery

LangGraph — Production Multi-Step Agent Workflows

Build production agent workflows with LangGraph. State machines, cycles, conditional routing, and human-in-the-loop patterns for reliable AI agents.

~1 hour Hands-on Precision AI Academy

Today's Objective

A research agent graph — define nodes (plan, search, analyze, write), define edges and conditions between them, and run a complex multi-step research task with full state visibility at every step.


  
code
pip install langgraph
01
Core Concepts

State, Nodes, and Edges

A LangGraph is built from three things: State (a TypedDict shared across all nodes), Nodes (Python functions that transform state), and Edges (connections between nodes, including conditional ones).

research_graph.py
python
from typing import TypedDict, Annotated
from langgraph.graph import StateGraph, END
from langchain_openai import ChatOpenAI

# 1. Define the state schema
class ResearchState(TypedDict):
    topic: str
    questions: list
    findings: str
    report: str
    iterations: int

model = ChatOpenAI(model="gpt-4o-mini")

# 2. Define nodes — each takes state, returns updated state
def plan_research(state: ResearchState) -> dict:
    response = model.invoke(
        f"Generate 3 research questions about: {state['topic']}"
    )
    questions = [q.strip() for q in response.content.split('\n') if q.strip()]
    return {"questions": questions, "iterations": 0}

def gather_findings(state: ResearchState) -> dict:
    questions_text = "\n".join(state["questions"])
    response = model.invoke(
        f"Answer these research questions concisely:\n{questions_text}"
    )
    return {"findings": response.content, "iterations": state["iterations"] + 1}

def write_report(state: ResearchState) -> dict:
    response = model.invoke(
        f"Write a 3-paragraph research report on {state['topic']} based on:\n{state['findings']}"
    )
    return {"report": response.content}

# 3. Build the graph
graph = StateGraph(ResearchState)
graph.add_node("plan", plan_research)
graph.add_node("gather", gather_findings)
graph.add_node("write", write_report)

# 4. Define edges
graph.set_entry_point("plan")
graph.add_edge("plan", "gather")
graph.add_edge("gather", "write")
graph.add_edge("write", END)

app = graph.compile()

# 5. Run it
result = app.invoke({"topic": "LangGraph vs plain LangChain agents"})
print(result["report"])

Conditional edges let you route to different nodes based on state. Add graph.add_conditional_edges("gather", check_quality, {"good": "write", "bad": "gather"}) to loop until quality passes.

100%
Course Complete

You finished LangChain in 5 Days

Chains, memory, RAG, agents, and LangGraph. You now know the full LangChain stack. The next step is deploying what you built.

Join the Live Bootcamp → Explore More Courses

Supporting References & Reading

Go deeper with these external resources.

Docs
LangGraph — Production Multi-Step Agent Workflows Official documentation for langchain.
GitHub
LangGraph — Production Multi-Step Agent Workflows Open source examples and projects for LangGraph — Production Multi-Step Agent Workflows
MDN
MDN Web Docs Comprehensive web technology reference

Day 5 Checkpoint

Before moving on, confirm understanding of these key concepts:

Course Complete
Return to Course Overview