How to Integrate Anthropic for fintech with pgvector for RAG
Combining Anthropic for fintech with pgvector gives you a practical RAG stack for regulated workflows: retrieve the right policy, transaction context, or customer record from Postgres, then have Claude draft a controlled response, summary, or decision support note. This is useful when you need grounded answers over internal data without shipping that data into a separate vector service.
The pattern is simple: pgvector handles similarity search inside Postgres, and Anthropic handles reasoning and generation on top of the retrieved context. That keeps your data path auditable and your application easier to operate in fintech environments.
Prerequisites
- •Python 3.10+
- •PostgreSQL 14+ with the
pgvectorextension installed - •An Anthropic API key
- •A Postgres database URL with write access
- •Python packages:
- •
anthropic - •
psycopg[binary] - •
pgvector - •
sentence-transformersor another embedding provider
- •
- •A basic schema for storing documents and embeddings
- •Network access from your app to both Anthropic and Postgres
Integration Steps
- •Set up Postgres with pgvector and create a documents table.
import os
import psycopg
DB_URL = os.environ["DATABASE_URL"]
with psycopg.connect(DB_URL) as conn:
with conn.cursor() as cur:
cur.execute("CREATE EXTENSION IF NOT EXISTS vector;")
cur.execute("""
CREATE TABLE IF NOT EXISTS knowledge_base (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
content TEXT NOT NULL,
embedding VECTOR(384)
);
""")
conn.commit()
print("pgvector schema ready")
Use the vector dimension that matches your embedding model. If you change embedding models later, migrate the column size too.
- •Generate embeddings and store them in pgvector.
import os
import psycopg
from sentence_transformers import SentenceTransformer
DB_URL = os.environ["DATABASE_URL"]
model = SentenceTransformer("all-MiniLM-L6-v2")
docs = [
{
"title": "KYC policy",
"content": "Customer onboarding requires identity verification, sanctions screening, and source of funds checks."
},
{
"title": "Fraud escalation",
"content": "Transactions above threshold with unusual geo-location should be escalated to the fraud team."
},
]
with psycopg.connect(DB_URL) as conn:
with conn.cursor() as cur:
for doc in docs:
embedding = model.encode(doc["content"]).tolist()
cur.execute(
"""
INSERT INTO knowledge_base (title, content, embedding)
VALUES (%s, %s, %s)
""",
(doc["title"], doc["content"], embedding),
)
conn.commit()
print("documents embedded and stored")
This keeps retrieval local to Postgres. In production, batch inserts and use a real ingestion pipeline rather than inserting one row at a time.
- •Query pgvector for the most relevant context.
import os
import psycopg
from sentence_transformers import SentenceTransformer
DB_URL = os.environ["DATABASE_URL"]
model = SentenceTransformer("all-MiniLM-L6-v2")
question = "What checks are required before onboarding a new customer?"
query_embedding = model.encode(question).tolist()
with psycopg.connect(DB_URL) as conn:
with conn.cursor() as cur:
cur.execute(
"""
SELECT title, content
FROM knowledge_base
ORDER BY embedding <-> %s::vector
LIMIT 3;
""",
(query_embedding,),
)
rows = cur.fetchall()
context = "\n\n".join([f"{title}: {content}" for title, content in rows])
print(context)
The <-> operator performs nearest-neighbor distance search with pgvector. For larger datasets, add an IVFFlat or HNSW index once your ingestion stabilizes.
- •Call Anthropic with retrieved context to produce a grounded answer.
import os
from anthropic import Anthropic
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
prompt = f"""
You are a fintech assistant. Answer only using the provided context.
If the context is insufficient, say so clearly.
Context:
{context}
Question:
{question}
"""
response = client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=300,
temperature=0,
messages=[
{"role": "user", "content": prompt}
],
)
print(response.content[0].text)
This is the core RAG loop: retrieve from pgvector, generate with Anthropic. Keep temperature low for compliance-facing workflows where deterministic output matters.
- •Wrap retrieval and generation into one reusable function.
import os
import psycopg
from anthropic import Anthropic
from sentence_transformers import SentenceTransformer
DB_URL = os.environ["DATABASE_URL"]
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
embedder = SentenceTransformer("all-MiniLM-L6-v2")
def rag_answer(question: str) -> str:
q_embedding = embedder.encode(question).tolist()
with psycopg.connect(DB_URL) as conn:
with conn.cursor() as cur:
cur.execute(
"""
SELECT title, content
FROM knowledge_base
ORDER BY embedding <-> %s::vector
LIMIT 3;
""",
(q_embedding,),
)
rows = cur.fetchall()
context = "\n\n".join([f"{title}: {content}" for title, content in rows])
response = client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=250,
temperature=0,
messages=[{
"role": "user",
"content": f"Use only this context:\n{context}\n\nQuestion: {question}"
}],
)
return response.content[0].text
print(rag_answer("What is required before onboarding a customer?"))
That function is what you wire into an agent tool or API endpoint. In production, add logging for retrieved document IDs so every answer can be traced back to source material.
Testing the Integration
Run a simple end-to-end check against known policy text.
answer = rag_answer("What should happen when a transaction looks suspicious?")
print(answer)
Expected output:
Transactions above threshold with unusual geo-location should be escalated to the fraud team.
If you get an empty or vague answer, check these first:
- •The embeddings column dimension matches your model output
- •The similarity query returns relevant rows before calling Anthropic
- •Your prompt explicitly tells Claude to use only retrieved context
Real-World Use Cases
- •Customer support copilots that answer questions about KYC rules, account restrictions, or payment exceptions using internal policy docs.
- •Fraud operations assistants that summarize suspicious activity by retrieving case notes from Postgres and drafting analyst-ready explanations.
- •Compliance review tools that pull relevant controls or procedures from a controlled corpus and generate first-pass responses for human review.
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