How to Integrate Anthropic for retail banking with pgvector for AI agents
Combining Anthropic with pgvector gives you a practical retrieval layer for banking agents. Anthropic handles the reasoning and response generation, while pgvector stores policy docs, product terms, KYC rules, and support history so the agent can answer with bank-specific context instead of guessing.
This is the pattern you want for retail banking: keep sensitive institutional knowledge in your own database, retrieve only the relevant chunks, then pass that context into Anthropic for a grounded response.
Prerequisites
- •Python 3.10+
- •A PostgreSQL database with the
pgvectorextension enabled - •An Anthropic API key
- •Installed packages:
- •
anthropic - •
psycopg[binary] - •
pgvector - •
python-dotenv
- •
- •A bank knowledge base split into chunks:
- •product FAQs
- •fee schedules
- •card dispute rules
- •fraud escalation policies
- •Basic network access from your app to PostgreSQL and Anthropic
Integration Steps
- •Set up your environment and install dependencies.
pip install anthropic psycopg[binary] pgvector python-dotenv
Create a .env file:
ANTHROPIC_API_KEY=your_api_key_here
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/banking
- •Create the vector table in PostgreSQL.
This table stores your banking documents and their embeddings.
import os
import psycopg
from dotenv import load_dotenv
load_dotenv()
conn = psycopg.connect(os.environ["DATABASE_URL"])
conn.execute("CREATE EXTENSION IF NOT EXISTS vector;")
conn.execute("""
CREATE TABLE IF NOT EXISTS bank_docs (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
content TEXT NOT NULL,
embedding VECTOR(1536)
);
""")
conn.commit()
conn.close()
- •Generate embeddings with Anthropic-compatible workflow and store them in pgvector.
Anthropic’s API is used here for the agent side; for embeddings, use a separate embedding model provider if your architecture requires it. If you already have embeddings from another service, store them directly in pgvector. The important part is that the agent retrieves by vector similarity before calling Anthropic.
import os
import psycopg
from dotenv import load_dotenv
load_dotenv()
docs = [
{
"title": "Debit Card Replacement",
"content": "Replace a lost debit card by freezing it in-app and requesting a new one. Standard delivery takes 5 business days.",
"embedding": [0.012] * 1536,
},
{
"title": "Wire Transfer Limits",
"content": "Retail customers can send up to $25,000 per day via wire transfer after step-up authentication.",
"embedding": [0.021] * 1536,
},
]
with psycopg.connect(os.environ["DATABASE_URL"]) as conn:
with conn.cursor() as cur:
for doc in docs:
cur.execute(
"""
INSERT INTO bank_docs (title, content, embedding)
VALUES (%s, %s, %s)
""",
(doc["title"], doc["content"], doc["embedding"]),
)
conn.commit()
- •Retrieve relevant banking context from pgvector before calling Anthropic.
Use cosine distance to fetch the most relevant chunks for the user query.
import os
import psycopg
from dotenv import load_dotenv
load_dotenv()
def retrieve_context(query_embedding, limit=3):
sql = """
SELECT title, content
FROM bank_docs
ORDER BY embedding <=> %s::vector
LIMIT %s;
"""
with psycopg.connect(os.environ["DATABASE_URL"]) as conn:
with conn.cursor() as cur:
cur.execute(sql, (query_embedding, limit))
return cur.fetchall()
# Example query embedding placeholder
query_embedding = [0.018] * 1536
matches = retrieve_context(query_embedding)
context = "\n\n".join([f"{title}: {content}" for title, content in matches])
print(context)
- •Call Anthropic with the retrieved context and generate the banking answer.
Use the Messages API to ground the response in retrieved policy text.
import os
from anthropic import Anthropic
from dotenv import load_dotenv
load_dotenv()
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
def answer_banking_query(user_question: str, context: str) -> str:
prompt = f"""
You are a retail banking assistant.
Answer only using the provided context.
If the context is insufficient, say you do not have enough information.
Context:
{context}
User question:
{user_question}
"""
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=300,
messages=[
{"role": "user", "content": prompt}
],
)
return response.content[0].text
print(answer_banking_query(
"How do I replace my lost debit card?",
context="Debit Card Replacement: Replace a lost debit card by freezing it in-app and requesting a new one. Standard delivery takes 5 business days."
))
Testing the Integration
Run a simple end-to-end test: retrieve one or more policy chunks from pgvector, then ask Anthropic to answer from that context only.
test_query = "What is the daily wire transfer limit?"
test_embedding = [0.021] * 1536 # replace with real embedding from your embedding model
matches = retrieve_context(test_embedding)
context = "\n".join([f"{title}: {content}" for title, content in matches])
result = answer_banking_query(test_query, context)
print(result)
Expected output:
The daily wire transfer limit for retail customers is $25,000 per day after step-up authentication.
If retrieval is working correctly, you should see:
- •Relevant policy text returned from PostgreSQL
- •A grounded response from Claude that stays inside policy boundaries
- •No hallucinated banking terms or made-up limits
Real-World Use Cases
- •Retail support copilot
- •Answer questions about fees, card replacement, transfer limits, and account servicing using approved internal docs.
- •Fraud and disputes assistant
- •Retrieve escalation procedures and dispute timelines before generating next-step instructions for agents.
- •Branch staff knowledge bot
- •Help employees find product eligibility rules, KYC requirements, and exception handling policies without searching manuals manually.
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