Design a RAG pipeline in LangGraph as a state machine with nodes for retrieval, relevance grading, query rewriting, and generation, using conditional edges to decide whether to continue, retry, or fall back to web search.
LangGraph transforms RAG pipelines from linear sequences into intelligent state machines. The core graph nodes include: retrieve (fetch documents from vector store), grade (evaluate document relevance using LLM grader), rewrite (rephrase query when retrieval fails), generate (produce final answer). Conditional edges route based on grading results: if documents are relevant, proceed to generation; if not, trigger query rewrite and retry; if multiple retries fail, fall back to web search. This creates a self-correcting system that automatically recovers from retrieval failures without manual intervention.
Retrieve node: Fetches documents from vector store based on query
Grade node: Evaluates relevance using LLM grader; filters irrelevant documents
Rewrite node: Reformulates query when retrieval fails; maintains retry counter
Generate node: Produces final answer from relevant documents
Web search node: Fallback when local retrieval repeatedly fails