How to Integrate LangChain for insurance with PostgreSQL for startups
Combining LangChain for insurance with PostgreSQL gives you a clean way to build AI agents that can answer policy questions, retrieve claim history, and keep every interaction grounded in structured data. For startups, this matters because you get fast prototyping on top of a durable system of record instead of stuffing customer context into prompts and hoping for the best.
Prerequisites
- •Python 3.10+
- •A PostgreSQL database running locally or in the cloud
- •A database user with
CREATE,SELECT,INSERT, andUPDATEpermissions - •A working LangChain setup for your insurance agent workflow
- •
pipinstalled - •Environment variables ready:
- •
DATABASE_URL - •
OPENAI_API_KEYor your model provider key
- •
- •These Python packages:
- •
langchain - •
langchain-openai - •
langchain-community - •
psycopg2-binary - •
sqlalchemy
- •
Integration Steps
- •Install dependencies and set up the database connection
Start by installing the packages your agent will use to talk to both the LLM and PostgreSQL.
pip install langchain langchain-openai langchain-community psycopg2-binary sqlalchemy
Set your connection string:
export DATABASE_URL="postgresql+psycopg2://postgres:password@localhost:5432/insurance_agent"
export OPENAI_API_KEY="your-key"
Then create a SQLAlchemy engine. This is the connection object LangChain will use through its SQL utilities.
import os
from sqlalchemy import create_engine
database_url = os.environ["DATABASE_URL"]
engine = create_engine(database_url)
with engine.connect() as conn:
result = conn.execute("SELECT 1")
print(result.fetchone())
- •Create insurance tables for policies and claims
For an insurance agent, you need structured tables that store policy metadata and claim records. Keep them simple at first so your agent can query them reliably.
from sqlalchemy import text
schema_sql = """
CREATE TABLE IF NOT EXISTS policies (
id SERIAL PRIMARY KEY,
policy_number VARCHAR(50) UNIQUE NOT NULL,
customer_name VARCHAR(255) NOT NULL,
product_type VARCHAR(100) NOT NULL,
status VARCHAR(50) NOT NULL,
premium NUMERIC(12,2) NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS claims (
id SERIAL PRIMARY KEY,
claim_number VARCHAR(50) UNIQUE NOT NULL,
policy_number VARCHAR(50) NOT NULL REFERENCES policies(policy_number),
claim_status VARCHAR(50) NOT NULL,
amount NUMERIC(12,2) NOT NULL,
description TEXT,
created_at TIMESTAMP DEFAULT NOW()
);
"""
with engine.begin() as conn:
for statement in schema_sql.strip().split(";"):
if statement.strip():
conn.execute(text(statement))
Seed a couple of rows so the agent has something real to query.
seed_sql = """
INSERT INTO policies (policy_number, customer_name, product_type, status, premium)
VALUES
('POL-1001', 'Amina Patel', 'Health', 'Active', 1200.00),
('POL-1002', 'Brian Okafor', 'Auto', 'Active', 840.00)
ON CONFLICT (policy_number) DO NOTHING;
INSERT INTO claims (claim_number, policy_number, claim_status, amount, description)
VALUES
('CLM-9001', 'POL-1001', 'Open', 250.00, 'Outpatient consultation'),
('CLM-9002', 'POL-1002', 'Approved', 1800.00, 'Accident repair')
ON CONFLICT (claim_number) DO NOTHING;
"""
with engine.begin() as conn:
for statement in seed_sql.strip().split(";"):
if statement.strip():
conn.execute(text(statement))
- •Wire LangChain to PostgreSQL using SQLDatabase
LangChain’s SQLDatabase wrapper gives your agent read access to PostgreSQL schema and data. This is the core bridge between natural language and your operational store.
from langchain_community.utilities import SQLDatabase
db = SQLDatabase.from_uri(database_url)
print(db.get_usable_table_names())
print(db.get_table_info(["policies", "claims"]))
At this point you have a database object LangChain can reason over. For insurance workflows, that means the model can inspect table structure before generating queries.
- •Build an agent that can query policy and claim data
Use a chat model plus LangChain’s SQL toolkit so the assistant can translate user questions into safe SQL queries.
from langchain_openai import ChatOpenAI
from langchain_community.agent_toolkits import create_sql_agent
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
agent = create_sql_agent(
llm=llm,
db=db,
verbose=True,
)
response = agent.invoke(
{"input": "What is the status of policy POL-1001 and does it have any open claims?"}
)
print(response["output"])
If you want tighter control for an insurance startup, keep prompts specific to domain tasks like:
- •policy lookup
- •claim status checks
- •premium validation
- •fraud triage summaries
That keeps the agent from wandering into irrelevant SQL or inventing business logic.
- •Add structured retrieval for agent memory or case context
PostgreSQL is not just for transactional data. You can also store conversation state, underwriting notes, or case summaries that your agent retrieves before answering.
from sqlalchemy.orm import declarative_base, Session
from sqlalchemy import Column, Integer, String, Text
Base = declarative_base()
class CaseNote(Base):
__tablename__ = "case_notes"
id = Column(Integer, primary_key=True)
case_id = Column(String(50), nullable=False)
note_type = Column(String(50), nullable=False)
content = Column(Text, nullable=False)
Base.metadata.create_all(engine)
with Session(engine) as session:
session.add(
CaseNote(
case_id="CLM-9001",
note_type="adjuster_note",
content="Customer submitted invoice; awaiting medical review."
)
)
session.commit()
Now your app can fetch case notes before invoking the LLM. That gives you better answers than relying on prompt history alone.
Testing the Integration
Run a direct verification against PostgreSQL and then let LangChain answer a real insurance question.
from sqlalchemy import text
with engine.connect() as conn:
rows = conn.execute(
text("SELECT policy_number, status FROM policies WHERE policy_number = :pn"),
{"pn": "POL-1001"},
).fetchall()
print(rows)
result = agent.invoke({"input": "Show me all claims for policy POL-1001."})
print(result["output"])
Expected output:
[('POL-1001', 'Active')]
The claims for policy POL-1001 are:
- CLM-9001: Open, amount 250.00
If you get empty results or bad SQL:
- •confirm
DATABASE_URLpoints to the right database - •check table names match exactly
- •verify your LLM key is loaded correctly
- •inspect
verbose=Truelogs to see generated queries
Real-World Use Cases
- •Policy servicing assistant: let customers ask about coverage status, renewal dates, premiums, and endorsements while pulling live data from PostgreSQL.
- •Claims triage copilot: summarize open claims, surface missing documents, and flag suspicious patterns using structured claim records.
- •Internal ops assistant: help support teams search cases faster by combining conversation context stored in PostgreSQL with LangChain-driven natural language querying.
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