How to Integrate Anthropic for wealth management with pgvector for startups
Combining Anthropic for wealth management with pgvector gives you a practical pattern for advisor copilots, portfolio Q&A, and client document retrieval. Anthropic handles the reasoning and response generation, while pgvector gives you fast semantic search over filings, IPS docs, meeting notes, and product research.
Prerequisites
- •Python 3.10+
- •PostgreSQL 14+ with the
pgvectorextension installed - •An Anthropic API key
- •
psycopgorpsycopg2for PostgreSQL access - •
anthropicPython SDK installed - •A database schema ready for storing embeddings and metadata
- •A startup-grade document source: PDFs, CSVs, CRM notes, or investment memos
Install the packages:
pip install anthropic psycopg[binary] pgvector sqlalchemy
Create the extension in your database:
CREATE EXTENSION IF NOT EXISTS vector;
Integration Steps
- •
Set up your PostgreSQL table with a vector column
Store chunks of wealth-management content alongside embeddings. Use a fixed embedding dimension that matches the model you choose.
import os
import psycopg
from pgvector.psycopg import register_vector
conn = psycopg.connect(os.environ["DATABASE_URL"])
register_vector(conn)
with conn.cursor() as cur:
cur.execute("""
CREATE TABLE IF NOT EXISTS wealth_docs (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
content TEXT NOT NULL,
embedding VECTOR(1536)
)
""")
conn.commit()
- •
Generate embeddings with Anthropic-compatible text processing
Anthropic’s core API is for generation, so use it to normalize or chunk wealth content before embedding. In production, pair this with your embedding model of choice; the important part is that Anthropic can cleanly extract structured text from messy inputs.
import anthropic
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
def normalize_wealth_note(raw_text: str) -> str:
message = client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=500,
messages=[
{
"role": "user",
"content": f"""
Clean this wealth management note into concise bullet points.
Preserve tickers, account numbers placeholders, risk notes, and action items.
NOTE:
{raw_text}
"""
}
]
)
return message.content[0].text.strip()
- •
Create embeddings and insert rows into pgvector
If you already have an embedding service in your stack, store those vectors here. The database side is standard pgvector: insert the vector directly and query with distance operators later.
import numpy as np
def fake_embedding(text: str) -> list[float]:
# Replace with your embedding provider.
# This placeholder keeps the integration shape clear.
rng = np.random.default_rng(abs(hash(text)) % (2**32))
return rng.random(1536).tolist()
doc_title = "Q4 Client Review"
raw_content = "Client wants lower volatility. Increase fixed income exposure and review tax-loss harvesting."
clean_content = normalize_wealth_note(raw_content)
embedding = fake_embedding(clean_content)
with conn.cursor() as cur:
cur.execute(
"INSERT INTO wealth_docs (title, content, embedding) VALUES (%s, %s, %s)",
(doc_title, clean_content, embedding)
)
conn.commit()
- •
Query similar documents from pgvector
Use cosine distance to retrieve the most relevant notes before sending context to Anthropic.
def search_similar_docs(query_text: str, limit: int = 5):
query_embedding = fake_embedding(query_text)
with conn.cursor() as cur:
cur.execute(
"""
SELECT title, content
FROM wealth_docs
ORDER BY embedding <=> %s
LIMIT %s
""",
(query_embedding, limit)
)
return cur.fetchall()
matches = search_similar_docs("Need low-volatility portfolio adjustments for a cautious client")
for title, content in matches:
print(title)
print(content)
- •
Send retrieved context to Anthropic for final answer generation
This is the actual agent loop: retrieve from pgvector first, then ask Claude to synthesize an advisor-ready response.
def answer_wealth_question(question: str) -> str:
docs = search_similar_docs(question)
context_block = "\n\n".join(
[f"Title: {title}\nContent: {content}" for title, content in docs]
)
response = client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=700,
messages=[
{
"role": "user",
"content": f"""
You are assisting a wealth management workflow.
Use only the provided context to answer.
Question:
{question}
Context:
{context_block}
Return:
- Short answer
- Key risks
- Suggested next action
"""
}
]
)
return response.content[0].text.strip()
Testing the Integration
Run a simple end-to-end check: store one note, retrieve it by semantic similarity, then generate an answer.
result = answer_wealth_question("What should we do for a conservative client who wants less volatility?")
print(result)
Expected output:
Short answer:
Shift toward higher-quality fixed income and reduce equity concentration.
Key risks:
Interest rate sensitivity may increase if duration is extended too far.
Tax implications should be reviewed before rebalancing.
Suggested next action:
Review current allocations against IPS constraints and confirm any tax-loss harvesting opportunities.
Real-World Use Cases
- •
Advisor copilot
- •Retrieve prior meeting notes, policy docs, and suitability constraints from pgvector.
- •Use Anthropic to draft client-facing summaries and internal next steps.
- •
Portfolio research assistant
- •Index research memos, market commentary, and house views in Postgres.
- •Ask natural-language questions like “Which clients are exposed to duration risk?”
- •
Client onboarding workflow
- •Store KYC documents and onboarding transcripts as vectors.
- •Generate missing-field checklists and compliance summaries with Claude.
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