My father slipped on wet tiles while watering plants. Left shoulder dislocation. Emergency room. The doctor said he needed treatment, possibly surgery.
Hospital estimate: ₹30,000-50,000.
We had HDFC insurance for 4 years. But standing there, I had no idea what was covered.
The 84-page policy PDF was useless at that moment.
So I built an AI system that could answer our questions. It took one night. It saved us ₹30,000.
Here's exactly how I did it.
What Is a RAG Agent?
RAG = Retrieval-Augmented Generation
Think of it as ChatGPT that:
Only reads YOUR documents
Never makes up answers
Shows you which page it found information on
Perfect for insurance policies where accuracy matters.
How It Helped Us
Question 1
"MY Father's shoulder is dislocated. Is it covered?"
Agent Answer:
✅ YES - Covered under Accidental Injury (Section 4.2)
- No waiting period for accidents
- Manipal Hospital is a network hospital - use cashless
- Apply for pre-authorization within 24 hours Result: Got cashless approval in 18 hours. No upfront payment needed.
Question 2
"Is an MRI scan also covered, or need to pay for it?"
Agent Answer:
✅ YES - Covered under Diagnostic Procedures (Section 5.1)
- MRI needs pre-authorization
- Already approved with treatment
- Direct billing available Result: MRI done cashless. Saved ₹18,000 out-of-pocket.
Question 3
"Physiotherapy sessions are covered?"
Agent Answer:
✅ YES - 10 sessions covered (Section 6.4)
- Up to ₹800 per session
- Must be prescribed by a doctor
- Can be done at empaneled centers Result: We had NO idea this was covered. Used all 10 sessions. Saved ₹8,000.
The Complete Code
Here's the exact system I built. Copy-paste ready.
Prerequisites
# Install Python packages
pip install langchain langchain-community langchain-openai faiss-cpu pypdf python-dotenv
Get OpenAI API key from platform.openai.com
Step 1: Setup (.env file)
OPENAI_API_KEY=your_key_here
Step 2: Load Policy PDF
# load_policy.py
from langchain_community.document_loaders import PyPDFLoader
def load_policy(pdf_path):
"""Load insurance policy PDF"""
loader = PyPDFLoader(pdf_path)
documents = loader.load()
print(f"✅ Loaded {len(documents)} pages")
return documents
### Usage
policy_docs = load_policy("star_health_policy.pdf")
Step 3: Create Smart Chunks
# chunking.py
from langchain.text_splitter import RecursiveCharacterTextSplitter
def create_chunks(documents):
"""Split into searchable pieces"""
splitter = RecursiveCharacterTextSplitter(
chunk_size=500,
chunk_overlap=100
)
chunks = splitter.split_documents(documents)
print(f"✅ Created {len(chunks)} chunks")
return chunks
# Usage
chunks = create_chunks(policy_docs)
Step 4: Create Vector Database
# vectorstore.py
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
def create_vectorstore(chunks):
"""Create searchable database"""
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.from_documents(chunks, embeddings)
vectorstore.save_local("policy_index")
print("✅ Vector store created")
return vectorstore
# Load existing
def load_vectorstore():
embeddings = OpenAIEmbeddings()
return FAISS.load_local(
"policy_index",
embeddings,
allow_dangerous_deserialization=True
)
# Usage
vectorstore = create_vectorstore(chunks)
Step 5: Create the RAG System
# rag_system.py
from langchain_openai import ChatOpenAI
from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate
def create_rag_system(vectorstore):
"""Create the question-answering system"""
# Define prompt
prompt = PromptTemplate(
input_variables=["context", "question"],
template="""You are an insurance policy expert.
Answer using ONLY the context below. If you don't know, say so.
Context:
{context}
Question:
{question}
Answer clearly and cite sections."""
)
# Create LLM
llm = ChatOpenAI(
model="gpt-4-turbo-preview",
temperature=0
)
# Create retriever
retriever = vectorstore.as_retriever(
search_kwargs={"k": 5}
)
# Build chain
chain = RetrievalQA.from_chain_type(
llm=llm,
retriever=retriever,
return_source_documents=True,
chain_type_kwargs={"prompt": prompt}
)
return chain
# Usage
rag_chain = create_rag_system(vectorstore)
Step 6: Ask Questions
# query.py
def ask_question(chain, question):
"""Ask and get answer with sources"""
result = chain.invoke({"query": question})
print(f"\n❓ Question: {question}")
print(f"\n✅ Answer: {result['result']}")
print(f"\n📚 Sources: {len(result['source_documents'])} sections")
for i, doc in enumerate(result['source_documents'], 1):
print(f"\n{i}. Page {doc.metadata.get('page', '?')}")
print(f" {doc.page_content[:200]}...")
return result
# Usage
ask_question(rag_chain, "Is emergency treatment covered for accidents?")
Complete Script (main.py)
# main.py
import os
from dotenv import load_dotenv
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_community.vectorstores import FAISS
from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate
# Load API key
load_dotenv()
def setup_rag_system(pdf_path):
"""Complete setup in one function"""
print("📄 Loading policy...")
loader = PyPDFLoader(pdf_path)
documents = loader.load()
print("✂️ Creating chunks...")
splitter = RecursiveCharacterTextSplitter(
chunk_size=500,
chunk_overlap=100
)
chunks = splitter.split_documents(documents)
print("🧠 Creating embeddings...")
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.from_documents(chunks, embeddings)
print("🔗 Building RAG chain...")
prompt = PromptTemplate(
input_variables=["context", "question"],
template="""You are an insurance policy expert.
Answer using ONLY the context below. If you don't know, say so.
Context:
{context}
Question:
{question}
Answer clearly and cite policy sections."""
)
llm = ChatOpenAI(model="gpt-4-turbo-preview", temperature=0)
retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
chain = RetrievalQA.from_chain_type(
llm=llm,
retriever=retriever,
return_source_documents=True,
chain_type_kwargs={"prompt": prompt}
)
print("✅ System ready!\n")
return chain
def ask(chain, question):
"""Ask a question"""
result = chain.invoke({"query": question})
print(f"\n❓ {question}")
print(f"✅ {result['result']}\n")
return result
# Run
if __name__ == "__main__":
# Setup
chain = setup_rag_system("your_policy.pdf")
# Ask questions
ask(chain, "Is shoulder dislocation covered?")
ask(chain, "What about physiotherapy sessions?")
ask(chain, "Are there any waiting periods?")
Run It
python main.py
Real Output Example
$ python main.py
📄 Loading policy...
✂️ Creating chunks...
🧠 Creating embeddings...
🔗 Building RAG chain...
✅ System ready!
❓ Is shoulder dislocation covered?
✅ Yes, shoulder dislocation from accidental injury is covered under
Section 4.2 (Accidental Injury Coverage). No waiting period applies
for accident-related injuries. Pre-authorization required within 24
hours for cashless treatment.
❓ What about physiotherapy sessions?
✅ Physiotherapy is covered under Section 6.4 for post-treatment
rehabilitation. Maximum 10 sessions covered at ₹800 per session.
Must be prescribed by treating doctor.
❓ Are there any waiting periods?
✅ Yes, standard waiting periods apply: 30 days for specific ailments,
24 months for pre-existing conditions. However, these are WAIVED for
accidental injuries.
Limitations
What It Does:
✅ Answers factual coverage questions
✅ Explains waiting periods
✅ Finds relevant clauses
✅ Handles Hindi + English questions
What It Doesn't Do:
❌ Legal advice
❌ Guarantee claim approval
❌ Medical diagnosis
❌ Replace insurance company
Always verify critical decisions with your insurer.
Real Impact
After sharing this, 200+ families asked for help with their policies.
Common discoveries:
Room rent sub-limits nobody knew about
Physiotherapy coverage never claimed
Waiting periods wrongly applied
Cashless hospitals not used
Total saved by our community: ₹23+ lakhs
Next Steps For You
Get your policy PDF
Get OpenAI API key (or use free Ollama)
Run the code above
Ask questions about YOUR policy
For Developers
Add more insurers (ICICI, HDFC, Care)
Build mobile app
Add Hindi voice interface
Create comparison tool
For Startups
This is a real problem. Millions of families need this. Build it right, help millions.
**
FAQs**
Q: Do I need coding knowledge?
A: Basic Python. If you can copy-paste, you can run this.
Q: How long does setup take?
A: 30 minutes first time. 2 minutes after that.
Q: Is my policy data safe?
A: Runs on your computer. Your policy never leaves your machine (except embeddings to OpenAI).
Q: Can I use this for other documents?
A: Yes! Works for any PDF - legal docs, manuals, research papers.
Q: What if my policy updates?
A: Rerun the setup script. Takes 2 minutes.
Success Story
After building this:
Father's treatment went smoothly
No confusion, no panic
Saved ₹30,000 by understanding coverage
Discovered benefits we didn't know existed
Helped 200+ other families
Most importantly: Peace of mind during a medical emergency.
Final Thoughts
Insurance shouldn't be a mystery box you open during emergencies.
You pay premiums. You deserve to understand what you bought.
This RAG system gives you that understanding—in seconds, in your language, with sources cited.
Build it. Use it. Share it.
Your family will thank you when it matters most.
Top comments (0)