LangGraph Agents

Course Content

LangGraph Agents

7 sections · 49 lessons

What kinds of workflows are hard with DAGs but natural with LangGraph?


What you need to know

Patterns that need a cycle or a pause

  • Critique and revise — draft, critique, revise, repeat until the critic approves or a budget runs out.
  • Tool loop (ReAct) — the model calls a tool, reads the result, decides whether to call another. The number of iterations is unknown in advance.
  • Retry with repair — output fails validation, the error is fed back, the model tries again.
  • Agentic RAG — retrieve, grade the documents, rewrite the query if they are weak, retrieve again.
  • Human-in-the-loop — stop before a risky action and resume when a person answers.
  • Handoffs with return — a researcher hands to a writer, who can hand back for more evidence.

Why unrolling fails

If you unroll a loop to three fixed copies, a case that needs four fails and a case that needs one wastes two. If you hide a while loop inside one node, you lose per-iteration checkpoints, streaming and the ability to interrupt in the middle. A cycle with a counter in state and a conditional exit gives you all of these.

When a DAG is still right

Batch ETL, nightly embeddings refresh, a fixed extract-transform-load. If you cannot name the loop or the pause, keep the DAG.

A real-life example

A code-fixing agent for an internal CI system must make a failing test pass.

  • As a DAG, the team hard-coded "generate fix, run tests, generate fix, run tests". About 30% of bugs needed three or more attempts and failed; easy bugs paid for two attempts anyway.
  • As a graph, generate → run_tests → (passed? END : generate) with attempts in state and an exit at 4. Easy bugs finish in one pass, hard ones get up to four, and the trace shows exactly which attempt fixed it.

Follow-up questions to expect

  • "Can't Airflow do loops?" — Not inside one DAG run; you would trigger new runs. That loses the in-memory context and makes the loop hard to follow.
  • "How do you stop an infinite cycle?" — A counter in state checked by the conditional edge, plus an explicit recursion_limit as a backstop.
  • "Is a cycle always better?" — No. A cycle costs tokens every time it turns. Use it only when repeating actually improves the result.