agent.pydevelopmentLangchainv1.0
LangChain: RAG Document Agent
Load a directory of docs, embed them, and expose a Q&A agent. Answers questions with citations.
Setup time: ~15 min
Model: GPT-4o
Cost: ~$0.10/day
Last updated: Mar 8, 2026
Template
agent.py
# Install: pip install langchain langchain-openai langchain-community chromadb
from langchain_community.document_loaders import DirectoryLoader, TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate
import os
# --- Config ---
DOCS_DIR = "./docs" # point at your docs directory
CHROMA_DIR = "./chroma_db" # where embeddings are stored
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
# --- Load and chunk documents ---
loader = DirectoryLoader(DOCS_DIR, glob="**/*.md", loader_cls=TextLoader)
docs = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
chunks = splitter.split_documents(docs)
# --- Embed and store ---
embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(chunks, embeddings, persist_directory=CHROMA_DIR)
retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
# --- Build QA chain ---
prompt = PromptTemplate(
template="""Answer the question using only the context below.
If the answer is not in the context, say "I don't know."
Cite the source file for each fact.
Context: {context}
Question: {question}
Answer:""",
input_variables=["context", "question"],
)
llm = ChatOpenAI(model="gpt-4o", temperature=0)
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
retriever=retriever,
chain_type_kwargs={"prompt": prompt},
return_source_documents=True,
)
# --- Run ---
if __name__ == "__main__":
while True:
query = input("Ask a question (or 'quit'): ")
if query.lower() == "quit":
break
result = qa_chain.invoke({"query": query})
print("\nAnswer:", result["result"])
print("Sources:", [d.metadata["source"] for d in result["source_documents"]])
print()Setup
- 1
Copy the agent.py content above.
- 2
Create a Python virtual environment and install dependencies.
- 3
Set your OPENAI_API_KEY environment variable.
- 4
Run: python agent.py
Run with LangChain
This is a Python script using LangChain. Set up a virtual environment and install dependencies.
# 1. Create a virtual environment
python -m venv venv && source venv/bin/activate
# 2. Install dependencies
pip install langchain langchain-openai langchain-community chromadb
# 3. Set your API key
export OPENAI_API_KEY="sk-..."
# 4. Save the agent.py from above and run it
python agent.pyVersion History
v1.0Initial releaseMar 8, 2026
Framework
LangchainRequirements
Python 3.10+
OpenAI API key
Estimated cost
~$0.10/day
on GPT-4o model
File type
agent.py
Version
v1.0
Updated Mar 8, 2026
You might also like
HEARTBEAT.md
GitHub PR Reviewer
Auto-review new PRs during business hours. Post summaries, flag issues.
~$0.50/day · ~5 min setup
HEARTBEAT.md
CI Monitor
Watch GitHub Actions. Alert on failures with the relevant logs.
~$0.20/day · ~3 min setup
HEARTBEAT.md
Deploy Tracker
Track deployments across repos. Daily changelog of what shipped.
~$0.15/day · ~5 min setup