How to Integrate Anthropic for insurance with pgvector for AI agents
Insurance agents need more than a chat model. They need retrieval over policy docs, claims history, endorsements, and underwriting rules, then a model that can turn that context into a grounded answer or action.
That’s where Anthropic for insurance and pgvector fit together. Anthropic handles the reasoning and response generation, while pgvector gives your agent low-latency semantic search over internal insurance knowledge stored in Postgres.
Prerequisites
- •Python 3.10+
- •PostgreSQL 14+ with the
pgvectorextension installed - •An Anthropic API key
- •A database with insurance documents you want to retrieve from
- •Python packages:
- •
anthropic - •
psycopg2-binary - •
pgvector - •
python-dotenv
- •
Install them:
pip install anthropic psycopg2-binary pgvector python-dotenv
Create the extension in Postgres:
CREATE EXTENSION IF NOT EXISTS vector;
Integration Steps
- •Set up your database schema for embeddings.
Use a table that stores the document text plus a vector column. For Anthropic embeddings, use whatever embedding model your stack standardizes on; the important part is that the vector dimension matches your embedding output.
import os
import psycopg2
conn = psycopg2.connect(
host=os.getenv("PGHOST"),
dbname=os.getenv("PGDATABASE"),
user=os.getenv("PGUSER"),
password=os.getenv("PGPASSWORD"),
port=os.getenv("PGPORT", "5432"),
)
with conn, conn.cursor() as cur:
cur.execute("""
CREATE TABLE IF NOT EXISTS insurance_docs (
id SERIAL PRIMARY KEY,
doc_type TEXT NOT NULL,
content TEXT NOT NULL,
embedding vector(1536)
);
""")
- •Generate embeddings for insurance content and store them in pgvector.
In production, you’ll usually batch this job from policy PDFs, claim notes, or underwriting guidelines. The code below shows the pattern: get an embedding, then insert it into Postgres.
import os
from anthropic import Anthropic
import psycopg2
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
def embed_text(text: str) -> list[float]:
# Replace with your embedding service if your Anthropic setup exposes one.
# The integration pattern stays the same: produce a float vector and store it in pgvector.
raise NotImplementedError("Wire this to your embedding pipeline")
docs = [
("policy", "Water damage is covered if caused by sudden and accidental discharge."),
("claims", "Claims over $10,000 require supervisor review."),
]
conn = psycopg2.connect(os.environ["DATABASE_URL"])
with conn, conn.cursor() as cur:
for doc_type, content in docs:
embedding = embed_text(content)
cur.execute(
"""
INSERT INTO insurance_docs (doc_type, content, embedding)
VALUES (%s, %s, %s)
""",
(doc_type, content, embedding),
)
- •Retrieve the most relevant policy context with pgvector similarity search.
This is the core of the RAG flow. You embed the user query, compare it to stored vectors using <->, and return the top matches.
import os
import psycopg2
def search_docs(query_embedding: list[float], limit: int = 5):
conn = psycopg2.connect(os.environ["DATABASE_URL"])
with conn, conn.cursor() as cur:
cur.execute(
"""
SELECT id, doc_type, content
FROM insurance_docs
ORDER BY embedding <-> %s::vector
LIMIT %s;
""",
(query_embedding, limit),
)
return cur.fetchall()
- •Pass retrieved context into Anthropic to generate an insurance-safe answer.
Use Anthropic’s Messages API to ground responses in retrieved evidence. Keep the prompt strict: answer only from context when possible and flag uncertainty when documents do not support a claim.
import os
from anthropic import Anthropic
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
def answer_question(question: str, context_chunks: list[str]) -> str:
context = "\n\n".join(context_chunks)
message = client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=500,
temperature=0,
messages=[
{
"role": "user",
"content": f"""
You are an insurance assistant.
Use only the provided policy context to answer.
If the context is insufficient, say what is missing.
Context:
{context}
Question:
{question}
"""
}
],
)
return message.content[0].text
- •Wire retrieval + generation into one agent function.
This is what your AI agent actually calls at runtime. Query comes in, nearest chunks come back from pgvector, then Anthropic produces the final response.
def embed_query(query: str) -> list[float]:
# Same note as above: plug in your embedding pipeline here.
raise NotImplementedError
def rag_answer(question: str) -> str:
query_embedding = embed_query(question)
rows = search_docs(query_embedding)
context_chunks = [
f"[{doc_type}] {content}"
for _, doc_type, content in rows
]
return answer_question(question, context_chunks)
print(rag_answer("Is sudden water damage covered under this policy?"))
Testing the Integration
Run a simple end-to-end check against one known policy clause and one query that should match it.
test_query = "Does accidental water discharge count as covered damage?"
query_embedding = embed_query(test_query)
matches = search_docs(query_embedding, limit=3)
print("Top match:", matches[0][1], matches[0][2])
response = answer_question(
test_query,
[f"{doc_type}: {content}" for _, doc_type, content in matches]
)
print("\nAgent response:\n", response)
Expected output:
Top match: policy Water damage is covered if caused by sudden and accidental discharge.
Agent response:
Yes. Based on the retrieved policy language, sudden and accidental discharge is covered...
Real-World Use Cases
- •
Claims triage assistant
Retrieve claim notes and policy clauses before asking Anthropic to summarize coverage gaps or next actions. - •
Underwriting copilot
Search historical submissions and underwriting guidelines with pgvector, then have Anthropic draft risk summaries for reviewers. - •
Customer service policy bot
Ground answers in approved policy text so agents can explain deductibles, exclusions, and coverage limits without hallucinating.
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