All 29 lessons
Foundations: what RAG is and a working pipeline
Better retrieval: chunking and search
Answers you can trust, measure and debug
Production in a company
Agentic RAG
Frameworks: LangChain, LangGraph and LangSmith
- 24. The RAG toolbox: what each library does and why you import it
- 25. The framework landscape: LangChain, LangGraph, LlamaIndex, Haystack
- 26. Lab: Ask Brightline in LangChain
- 27. Lab: agentic RAG as a LangGraph graph
- 28. Lab: tracing, evaluating and monitoring with LangSmith
- 29. Wrap-up: your production agentic RAG checklist
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.
Step 1: the tool and the state
import osfrom typing import Literalfrom langchain.tools import toolfrom langchain.messages import HumanMessagefrom langchain_anthropic import ChatAnthropicfrom langgraph.graph import MessagesState@tooldef retrieve_policy(query: str) -> str:"""Search the Brightline policy handbook."""found = vector_store.similarity_search(query, k=2) # lesson 26's storereturn "\n\n".join(f"[{d.metadata['source']}] {d.page_content}" for d in found)MAX_REWRITES = 2class State(MessagesState): # the chat messages, plus our own counterrewrites: intmodel = ChatAnthropic(model=os.environ["CLAUDE_MODEL"])
print(retrieve_policy.invoke({"query": "VPN error 0x80"})[:70])
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.
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.
from pydantic import BaseModel, Fieldclass 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 rewritinggrade = 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
from langgraph.graph import StateGraph, START, ENDfrom langgraph.prebuilt import ToolNode, tools_conditionworkflow = 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()
Step 5: run it and watch each node
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 Noneprint(f"{node}: {getattr(last, 'tool_calls', None) or (last.text[:60] if last else '')}")run("Can I expense wine with dinner on a trip?")
generate_query_or_respond: [{'name': 'retrieve_policy', 'args': {'query': 'wine dinner trip'}, ...}]retrieve: [travel-meals] Meals on business travel are reimbursed up to $75generate_answer: No. Alcohol is not reimbursable [travel-meals].
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.
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.
- retrieve runs the search
- grade_documents returns rewrite_question
- generate_query_or_respond asks for retrieve_policy
- A second search runs
- rewrite_question increments the counter
- grade_documents returns generate_answer
Worth keeping
- A LangGraph graph is state, nodes that update it, and edges that pick the next node.
- Conditional edges are plain functions that return the next node's name.
- Keep loop counters in state and check them in the edge, so the cap is guaranteed.
- 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.