Skip to content
LangGraph

RAG Pipeline with LangGraph

Build a Retrieval-Augmented Generation pipeline that searches documents with vector embeddings and answers questions with citations — using LangGraph.

RAGvector storeembeddingsdocuments

Working Code

LangGraph
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent
@tool
def search_documents(query: str, top_k: int = 5) -> str:
"""Search uploaded documents for relevant content."""
results = vector_store.similarity_search(query, k=top_k)
return "\n\n".join(
f"[{i+1}] {r.metadata.get('source', 'unknown')}: {r.page_content}"
for i, r in enumerate(results)
)
agent = create_react_agent(
ChatOpenAI(model="gpt-4o"),
tools=[search_documents],
prompt="You are a document Q&A assistant. Search documents before answering. Cite sources using [1], [2] format.",
)
result = await agent.ainvoke({
"messages": [("user", "What does the refund policy say about digital products?")]
})
print(result["messages"][-1].content)

Step by Step

1

Install dependencies

Install LangGraph and the required tools for this use case.

2

Define your tools

Create the domain-specific tool functions your agent will use to interact with external services.

3

Create the agent and run

Initialize the LangGraph agent with your tools, set the system prompt, and execute a query.

Ready to build with LangGraph?

Generate a production-ready project with LangGraph pre-configured — FastAPI + Next.js, auth, streaming, and more.

Get Started

Ready to build your first production AI agent?

Open-source tools, battle-tested patterns, zero boilerplate. Configure your stack and ship in minutes — not months.