0Pricing
AI Agents · Lesson

Why Graphs Beat Flat Chains

Real agents need loops, retries, and conditionals — DAGs and graphs express what chains cannot.

Why Graphs Beat Flat Chains is a free AI Agents lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Where Chains Fall Short

LCEL chains are a great fit for linear flows: A -> B -> C. They struggle when you need:

  • Loops (retry until success)
  • Conditional branching (decide A or B based on state)
  • Human-in-the-loop pauses
  • Persistent state across calls

Real Agents Are Graphs

Agents make decisions step by step. A real agent might:

  1. Plan
  2. Search for info
  3. If found, draft answer; if not, search again
  4. Validate
  5. If invalid, revise; else, return

This is a graph, not a line.

Introducing LangGraph

LangGraph (by the LangChain team) lets you define agents as graphs:

  • Nodes — functions that read and update state
  • Edges — what node runs next
  • State — shared, persistent dict

Nodes Are Just Functions

from langgraph.graph import StateGraph

class AgentState(TypedDict):
    messages: list
    plan: str
    answer: str

def plan_step(state: AgentState) -> AgentState:
    plan = generate_plan(state['messages'])
    return {'plan': plan}

Edges Connect Nodes

from langgraph.graph import StateGraph, END

graph = StateGraph(AgentState)
graph.add_node('plan', plan_step)
graph.add_node('search', search_step)
graph.add_node('answer', answer_step)

graph.set_entry_point('plan')
graph.add_edge('plan', 'search')
graph.add_edge('search', 'answer')
graph.add_edge('answer', END)

app = graph.compile()

Running the Graph

result = app.invoke({'messages': [{'role': 'user', 'content': 'What is RAG?'}]})
print(result['answer'])

State Updates Are Merged

When a node returns a partial dict, LangGraph merges it into the state. You don't have to write the whole state every step.

Conditional Branching

Use add_conditional_edges to route based on state:

def needs_more_info(state):
    if state['confidence'] < 0.7:
        return 'search'
    return 'answer'

graph.add_conditional_edges('plan', needs_more_info, {
    'search': 'search',
    'answer': 'answer'
})

Loops Are Just Edges Back

graph.add_edge('search', 'plan')   # loop back to planner
# The planner decides if more search is needed.

Visualisation

LangGraph generates Mermaid diagrams of your graph — great for debugging structure:

from IPython.display import Image
Image(app.get_graph().draw_mermaid_png())

vs Hand-Rolled While Loops

You could do this with a Python while loop. LangGraph adds:

  • Explicit graph structure (visualisable)
  • Built-in checkpointing
  • Streaming of state updates
  • Human-in-the-loop primitives

LangGraph + LCEL

Nodes can be any callable — including LCEL chains. Mix and match:

summarise_chain = prompt | model | parser

def summarise_node(state):
    return {'summary': summarise_chain.invoke(state['text'])}

When to Use a Graph

Which scenario benefits most from LangGraph over a flat LCEL chain?

Recap

Graphs > chains when you need loops, branches, persistence, or human-in-the-loop. LangGraph gives you the structure.

Frequently asked questions

Is the “Why Graphs Beat Flat Chains” lesson free?

Yes — the full text of “Why Graphs Beat Flat Chains” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.

What will I learn in “Why Graphs Beat Flat Chains”?

Real agents need loops, retries, and conditionals — DAGs and graphs express what chains cannot. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start AI Agents?

No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Why Graphs Beat Flat Chains” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this AI Agents lesson?

Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Graphs Beat Flat Chains
  2. Nodes, Edges and State
  3. Conditional Routing and Branching
  4. Persisting Graph State (Checkpoints)
← Back to AI Agents