How to Integrate FastAPI for insurance with LangChain for RAG
FastAPI for insurance gives you a clean API surface for policy, claims, and underwriting workflows. LangChain gives you the retrieval and orchestration layer to turn those endpoints into a RAG-powered agent that can answer questions from policy docs, claims history, and internal procedures without hardcoding business logic.
Prerequisites
- •Python 3.10+
- •A FastAPI for insurance service running with documented endpoints for:
- •policy lookup
- •claims lookup
- •document retrieval
- •A LangChain-compatible LLM provider key set in your environment
- •
pipinstalled packages:- •
fastapi - •
uvicorn - •
langchain - •
langchain-openaior your preferred chat model package - •
httpx
- •
- •Access to your insurance API base URL and auth token
- •A vector store or document index if you want true RAG over policy PDFs, claim notes, or underwriting manuals
Integration Steps
- •
Expose your insurance data through FastAPI endpoints
Start by making sure your insurance system has stable HTTP endpoints that LangChain can call as tools. Keep the responses small and structured; agents work better with JSON than with raw HTML or free text.
from fastapi import FastAPI, Header, HTTPException from pydantic import BaseModel app = FastAPI(title="Insurance API") class PolicyQuery(BaseModel): policy_number: str @app.post("/policies/lookup") async def lookup_policy(payload: PolicyQuery, authorization: str = Header(default="")): if not authorization.startswith("Bearer "): raise HTTPException(status_code=401, detail="Unauthorized") # Replace with real DB call return { "policy_number": payload.policy_number, "status": "active", "product": "homeowners", "effective_date": "2024-01-01", "deductible": 1000, } - •
Wrap the FastAPI endpoint as a LangChain tool
Use LangChain’s
@tooldecorator so the agent can call the insurance API when it needs live policy data. This keeps the LLM from guessing facts that belong in your system of record.import os import httpx from langchain_core.tools import tool INSURANCE_API_BASE_URL = os.getenv("INSURANCE_API_BASE_URL", "http://localhost:8000") INSURANCE_API_TOKEN = os.getenv("INSURANCE_API_TOKEN", "dev-token") @tool async def get_policy_details(policy_number: str) -> dict: """Fetch policy details from the insurance API.""" async with httpx.AsyncClient(timeout=10.0) as client: response = await client.post( f"{INSURANCE_API_BASE_URL}/policies/lookup", json={"policy_number": policy_number}, headers={"Authorization": f"Bearer {INSURANCE_API_TOKEN}"}, ) response.raise_for_status() return response.json() - •
Add RAG over insurance documents with LangChain
For claims manuals, underwriting guidelines, or coverage wording, use a retriever. The agent should retrieve relevant chunks first, then answer using those chunks plus live API data.
from langchain_openai import OpenAIEmbeddings from langchain_community.vectorstores import FAISS from langchain_text_splitters import RecursiveCharacterTextSplitter from langchain_core.documents import Document docs = [ Document(page_content="Water damage is covered only if sudden and accidental."), Document(page_content="Claims above $10,000 require supervisor review."), Document(page_content="Homeowners policies exclude flood unless endorsed."), ] splitter = RecursiveCharacterTextSplitter(chunk_size=300, chunk_overlap=50) chunks = splitter.split_documents(docs) embeddings = OpenAIEmbeddings(model="text-embedding-3-small") vectorstore = FAISS.from_documents(chunks, embeddings) retriever = vectorstore.as_retriever(search_kwargs={"k": 2}) - •
Compose the agent with tools + retriever
Build a chain that can decide when to query the API and when to retrieve policy language. In production, this is where you keep prompts tight and force structured outputs.
from langchain_openai import ChatOpenAI from langchain_core.prompts import ChatPromptTemplate from langchain.agents import create_tool_calling_agent, AgentExecutor llm = ChatOpenAI(model="gpt-4o-mini", temperature=0) prompt = ChatPromptTemplate.from_messages([ ("system", "You are an insurance assistant. Use tools for live policy data and retrieval for coverage rules."), ("human", "{input}"), ("placeholder", "{agent_scratchpad}"), ]) tools = [get_policy_details] agent = create_tool_calling_agent(llm=llm, tools=tools, prompt=prompt) executor = AgentExecutor(agent=agent, tools=tools, verbose=True) - •
Combine retrieved context with live API output
The practical pattern is: retrieve relevant guidance first, fetch policy state second, then generate the final answer. That keeps answers grounded in both documentation and current system data.
async def answer_claim_question(question: str, policy_number: str): docs = retriever.invoke(question) policy_data = await get_policy_details.ainvoke(policy_number) context = "\n\n".join([d.page_content for d in docs]) messages = [ ("system", "Answer using only provided context and policy data."), ("human", f"""
Question: {question}
Policy data: {policy_data}
Retrieved guidance: {context} """), ]
return await llm.ainvoke(messages)
## Testing the Integration
Use a simple smoke test that hits both the FastAPI endpoint and the LangChain flow. If this works locally, you’ve proven the plumbing between system-of-record data and retrieval-based reasoning.
```python
import asyncio
async def main():
result = await answer_claim_question(
question="Is water damage covered for this homeowners policy?",
policy_number="POL-12345",
)
print(result.content)
asyncio.run(main())
Expected output:
Water damage is covered only if it was sudden and accidental. The policy is active under POL-12345 with a $1,000 deductible.
Real-World Use Cases
- •
Claims triage assistant
- •Pulls claim status from FastAPI endpoints.
- •Retrieves claims handling rules from manuals.
- •Drafts next-step recommendations for adjusters.
- •
Policy servicing copilot
- •Answers coverage questions using retrieved policy language.
- •Checks live policy status before responding.
- •Reduces back-and-forth between service teams and core systems.
- •
Underwriting support agent
- •Queries applicant or account data through FastAPI.
- •Retrieves underwriting guidelines through LangChain RAG.
- •Flags missing documents or rule violations before submission.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
By Cyprian Aarons, AI Consultant at Topiax.
Want the complete 8-step roadmap?
Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.
Get the Starter Kit