How to Integrate Anthropic for retail banking with pgvector for production AI
Combining Anthropic with pgvector gives you a practical pattern for retail banking agents: the model handles customer-facing reasoning, while pgvector gives you fast retrieval over policies, product docs, call transcripts, and compliance notes. That means your agent can answer questions like “What’s the overdraft fee for this account?” or “Summarize the KYC requirements for a new SME customer” using grounded internal knowledge instead of guessing.
For retail banking, that matters because accuracy and traceability beat generic chat quality. Anthropic generates the response; pgvector supplies the relevant context from your bank’s own data.
Prerequisites
- •Python 3.10+
- •PostgreSQL 14+ with the
pgvectorextension installed - •An Anthropic API key
- •A PostgreSQL user/database with permission to create tables and extensions
- •These Python packages:
- •
anthropic - •
psycopg[binary] - •
pgvector - •
sqlalchemy
- •
- •A source of bank documents to embed:
- •product FAQs
- •fee schedules
- •policy docs
- •support transcripts
Install dependencies:
pip install anthropic psycopg[binary] pgvector sqlalchemy
Integration Steps
- •Set up pgvector in PostgreSQL and create a table for embeddings.
import os
import psycopg
conn = psycopg.connect(
"postgresql://bank_user:bank_pass@localhost:5432/banking_ai"
)
with conn.cursor() as cur:
cur.execute("CREATE EXTENSION IF NOT EXISTS vector;")
cur.execute("""
CREATE TABLE IF NOT EXISTS bank_docs (
id SERIAL PRIMARY KEY,
doc_type TEXT NOT NULL,
content TEXT NOT NULL,
embedding vector(1536)
);
""")
conn.commit()
print("pgvector table ready")
- •Generate embeddings with Anthropic-compatible text chunks and store them in PostgreSQL.
Anthropic’s API is used here for generation later; for embeddings, use your own embedding model. In production, keep retrieval and generation separate. This example uses a placeholder embedding function so the integration pattern is clear.
import os
import math
from typing import List
def embed_text(text: str) -> List[float]:
# Replace with your embedding provider.
# Must return a vector matching your pgvector dimension.
return [math.sin(i) * 0.01 for i in range(1536)]
docs = [
("fee_schedule", "Retail checking accounts charge a $12 monthly maintenance fee unless minimum balance is maintained."),
("kyc_policy", "Customer onboarding requires government-issued ID, proof of address, and sanctions screening."),
]
with conn.cursor() as cur:
for doc_type, content in docs:
embedding = embed_text(content)
cur.execute(
"INSERT INTO bank_docs (doc_type, content, embedding) VALUES (%s, %s, %s)",
(doc_type, content, embedding),
)
conn.commit()
print("documents indexed")
- •Query pgvector for the most relevant bank context.
Use cosine distance to retrieve top matches before calling Anthropic. This keeps the model grounded in policy and product data.
query = "What documents do I need to open a retail checking account?"
query_embedding = embed_text(query)
with conn.cursor() as cur:
cur.execute(
"""
SELECT doc_type, content
FROM bank_docs
ORDER BY embedding <=> %s::vector
LIMIT 3;
""",
(query_embedding,),
)
rows = cur.fetchall()
context = "\n".join([f"[{row[0]}] {row[1]}" for row in rows])
print(context)
- •Call Anthropic with retrieved context to generate the final answer.
Use the Messages API. Pass the retrieved context into the system or user message so the model answers from bank-approved material.
from anthropic import Anthropic
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
response = client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=300,
temperature=0,
system="You are a retail banking assistant. Use only the provided context. If missing, say you don't know.",
messages=[
{
"role": "user",
"content": f"""
Customer question: {query}
Relevant bank context:
{context}
Answer clearly and concisely.
"""
}
],
)
print(response.content[0].text)
- •Wrap retrieval + generation into one production function.
This is the pattern you want in your agent service: retrieve top-k chunks from pgvector, then call Anthropic with those chunks attached.
def answer_banking_question(question: str) -> str:
q_emb = embed_text(question)
with conn.cursor() as cur:
cur.execute(
"""
SELECT content
FROM bank_docs
ORDER BY embedding <=> %s::vector
LIMIT 3;
""",
(q_emb,),
)
docs = [r[0] for r in cur.fetchall()]
retrieved_context = "\n".join(docs)
resp = client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=250,
temperature=0,
system="Answer using only retrieved banking policy context.",
messages=[{"role": "user", "content": f"{question}\n\nContext:\n{retrieved_context}"}],
)
return resp.content[0].text
print(answer_banking_question("Can I waive the monthly fee on this account?"))
Testing the Integration
Run a simple query that should map cleanly to one of your stored policies.
result = answer_banking_question("What ID do I need to open a new retail account?")
print(result)
Expected output:
You need a government-issued ID, proof of address, and sanctions screening before account opening.
If you get an empty or vague response:
- •check that embeddings are being stored at the same dimension as your
vector(n)column - •confirm your similarity operator matches your use case (
<=>for cosine distance) - •verify that retrieved context actually contains relevant policy text
- •keep
temperature=0for deterministic banking responses
Real-World Use Cases
- •
Customer support assistant
- •Answer product questions using approved fee schedules, eligibility rules, and onboarding requirements.
- •
Branch staff copilot
- •Retrieve internal procedures during customer interactions and draft compliant responses faster.
- •
Policy Q&A over regulated content
- •Search KYC/AML rules, complaint handling procedures, or lending criteria with grounded answers from bank-owned documents.
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