Black Friday50% off your first month on any monthly plan with EARLY50 See plans
All 29 lessons
  1. Foundations: what RAG is and a working pipeline

    1. 1. Getting started: what RAG is and when to use it
    2. 2. Types of RAG: a map from naive to agentic
    3. 3. Lab: build the smallest RAG pipeline end to end
    4. 4. How embeddings and vector search find meaning
    5. 5. Document types: what each one breaks, and how to prepare it
  2. Better retrieval: chunking and search

    1. 6. Chunking documents so answers survive
    2. 7. Chunking strategies compared: which to use when
    3. 8. Keyword search vs vector search, and fusing both
    4. 9. Retrieval algorithms compared: which to use when
    5. 10. Reranking the top results before the model reads them
    6. 11. Rewriting the question before you search
  3. Answers you can trust, measure and debug

    1. 12. Lab: grounded answers with citations and refusals
    2. 13. Measuring a RAG system with an evaluation set
    3. 14. Lab: evaluation methods and frameworks: Ragas and DeepEval
    4. 15. Troubleshooting a RAG system, symptom by symptom
  4. Production in a company

    1. 16. Access control: only retrieve what the user may see
    2. 17. Defending against prompt injection in retrieved text
    3. 18. Running RAG in production: freshness, cost and monitoring
    4. 19. Real-world RAG: what worked, what failed, and why
  5. Agentic RAG

    1. 20. Lab: from a fixed pipeline to an agent with a search tool
    2. 21. Self-correcting retrieval: grade, retry, fall back
    3. 22. GraphRAG and routing across many sources
    4. 23. Lab: numbers, tables and financial documents
  6. Frameworks: LangChain, LangGraph and LangSmith

    1. 24. The RAG toolbox: what each library does and why you import it
    2. 25. The framework landscape: LangChain, LangGraph, LlamaIndex, Haystack
    3. 26. Lab: Ask Brightline in LangChain
    4. 27. Lab: agentic RAG as a LangGraph graph
    5. 28. Lab: tracing, evaluating and monitoring with LangSmith
    6. 29. Wrap-up: your production agentic RAG checklist
See the leaderboard

20 min

Lab: agentic RAG as a LangGraph graph

Build the grade, rewrite and retry agent from earlier as an explicit LangGraph graph with a loop cap.

By the end of this lesson you can

  • Model an agent's state as a typed dict and its steps as nodes
  • Use conditional edges to choose between rewriting and answering
  • Cap the loop with a counter in state, and draw the graph

From earlier lessons · 1 of 2

In LangChain, how do you see the exact prompt sent to the model?

Pick one answer.

From earlier lessons · 2 of 2

Why does lesson 21's agent grade retrieved chunks?

Pick one answer.

Lesson 21's agent works, but its control flow lives in a while loop, three helper functions and a global list. A new teammate asks: "When exactly does it rewrite the question, and what stops it?" The answer takes ten minutes of reading. In LangGraph it's a picture.

Where we stand

Lesson 26's LangChain pipeline searches once and answers, even from irrelevant chunks. We'll add lesson 21's behaviour, grade then rewrite then retry, as an explicit graph. A graph has three parts: a state shared by every step, nodes that read and update it, and edges that decide which node runs next.

The design follows the LangGraph agentic RAG tutorial, checked against langgraph 1.2. That tutorial relies on LangGraph's general recursion limit, so we add an explicit rewrite cap.

[27][28]

Step 1: the tool and the state

python
import os
from typing import Literal
from langchain.tools import tool
from langchain.messages import HumanMessage
from langchain_anthropic import ChatAnthropic
from langgraph.graph import MessagesState
@tool
def retrieve_policy(query: str) -> str:
"""Search the Brightline policy handbook."""
found = vector_store.similarity_search(query, k=2) # lesson 26's store
return "\n\n".join(f"[{d.metadata['source']}] {d.page_content}" for d in found)
MAX_REWRITES = 2
class State(MessagesState): # the chat messages, plus our own counter
rewrites: int
model = ChatAnthropic(model=os.environ["CLAUDE_MODEL"])
vector_store is from lesson 26, built from the same four documents.
python
print(retrieve_policy.invoke({"query": "VPN error 0x80"})[:70])
Real output: [it-vpn] badge PIN. Error VPN-0x80 means the token expired. Lesson 26's store holds 120-character chunks, so the chunk with the error code matches best.

Step 2: the nodes

Each node is a plain function that takes the state and returns the fields it changes. LangGraph merges them in. The first node lets the model decide whether to search at all.

python
def latest_question(state):
return next(m.content for m in reversed(state["messages"]) if m.type == "human")
def generate_query_or_respond(state: State):
return {"messages": [model.bind_tools([retrieve_policy]).invoke(state["messages"])]}
def rewrite_question(state: State):
reply = model.invoke("Rewrite this question so a policy search finds better results. "
f"Reply with the question only:\n{latest_question(state)}")
return {"messages": [HumanMessage(reply.text)], "rewrites": state.get("rewrites", 0) + 1}
def generate_answer(state: State):
reply = model.invoke("Answer using only this context. If it isn't there, say you don't know.\n\n"
f"Context:\n{state['messages'][-1].content}\n\nQuestion: {latest_question(state)}")
return {"messages": [reply]}

Step 3: the grader, where the cap lives

A conditional edge is a function that returns the name of the next node. Our grader asks the model for a structured yes or no. It checks the counter first, so after two rewrites it answers with what it has, instead of looping.

python
from pydantic import BaseModel, Field
class GradeDocuments(BaseModel):
"""Whether the retrieved documents help answer the question."""
binary_score: Literal["yes", "no"] = Field(description="'yes' if relevant, else 'no'")
def grade_documents(state: State) -> Literal["generate_answer", "rewrite_question"]:
if state.get("rewrites", 0) >= MAX_REWRITES:
return "generate_answer" # the cap: stop rewriting
grade = model.with_structured_output(GradeDocuments).invoke(
f"Question: {latest_question(state)}\n\nDocuments:\n{state['messages'][-1].content}\n\n"
"Do the documents help answer the question?")
return "generate_answer" if grade.binary_score == "yes" else "rewrite_question"

Step 4: wire the graph

python
from langgraph.graph import StateGraph, START, END
from langgraph.prebuilt import ToolNode, tools_condition
workflow = StateGraph(State)
workflow.add_node(generate_query_or_respond)
workflow.add_node("retrieve", ToolNode([retrieve_policy]))
workflow.add_node(rewrite_question)
workflow.add_node(generate_answer)
workflow.add_edge(START, "generate_query_or_respond")
workflow.add_conditional_edges("generate_query_or_respond", tools_condition, {"tools": "retrieve", END: END})
workflow.add_conditional_edges("retrieve", grade_documents)
workflow.add_edge("rewrite_question", "generate_query_or_respond")
workflow.add_edge("generate_answer", END)
graph = workflow.compile()
graph.get_graph().draw_mermaid() prints the structure below.
Rendering diagram…
The compiled graph, as LangGraph draws it. Dotted arrows are conditional edges; the only loop runs through rewrite_question, which the counter caps.

Step 5: run it and watch each node

python
def run(question):
for update in graph.stream({"messages": [HumanMessage(question)], "rewrites": 0},
stream_mode="updates"):
for node, out in update.items():
last = out["messages"][-1] if out and out.get("messages") else None
print(f"{node}: {getattr(last, 'tool_calls', None) or (last.text[:60] if last else '')}")
run("Can I expense wine with dinner on a trip?")
text
generate_query_or_respond: [{'name': 'retrieve_policy', 'args': {'query': 'wine dinner trip'}, ...}]
retrieve: [travel-meals] Meals on business travel are reimbursed up to $75
generate_answer: No. Alcohol is not reimbursable [travel-meals].
Expected output, about three model calls. Your queries, wording and path will vary; if the grader says no, you'll see rewrite_question and a second search.

Re-run: prove the cap works

To test the cap, make the grader always say no. You can do this by swapping model for one of LangChain's fake chat models that returns scripted replies. We ran it that way. The graph rewrote twice, searched three times, then answered, and rewrites ended at exactly MAX_REWRITES.

A plain greeting like "Hi" makes no tool call, so tools_condition sends it straight to the end. Test that case too. The fake models don't implement bind_tools, so the test needs a three-line subclass that returns itself from bind_tools.

What did the graph buy over lesson 21's loop? The control flow is now data you can draw, test node by node, and extend. Adding a human approval step or a checkpoint that survives a crash is a few lines in LangGraph, and a rewrite in a hand-made loop.

[28]

Put in order

Order one run where the first search fails

Drag, or focus an item and press Space, move with the arrow keys, then Space to drop.

  1. retrieve runs the search
  2. grade_documents returns rewrite_question
  3. generate_query_or_respond asks for retrieve_policy
  4. A second search runs
  5. rewrite_question increments the counter
  6. grade_documents returns generate_answer

Worth keeping

  1. A LangGraph graph is state, nodes that update it, and edges that pick the next node.
  2. Conditional edges are plain functions that return the next node's name.
  3. Keep loop counters in state and check them in the edge, so the cap is guaranteed.
  4. Test graphs with a fake model that scripts replies, including the worst case.

The graph runs, but when an answer goes wrong in production you'll want to see every node's input, output, latency and tokens. Next lab traces, evaluates and monitors it in LangSmith.

Leaderboard

Loading…

50 XP for each lesson you finish, 10 for each right answer. Names show only for learners who turn on Show me on leaderboards.

Sources for this lesson (2)

Each fact in the lesson is marked with the number of the page it comes from.

  1. [27]LangGraph docs: Build a custom RAG agent
  2. [28]LangGraph docs: Overview