How to Integrate Anthropic for wealth management with pgvector for production AI
Combining Anthropic for wealth management with pgvector gives you a practical pattern for building AI agents that can answer client questions from internal knowledge, market research, and policy docs without stuffing everything into the prompt. The model handles reasoning and response generation, while pgvector gives you low-latency semantic retrieval over portfolios, product docs, suitability policies, and advisor notes.
For wealth management teams, that means better client servicing, faster advisor support, and less brittle prompt engineering. You keep the sensitive knowledge in your database, retrieve only what matters, and let Anthropic turn that context into a controlled answer.
Prerequisites
- •Python 3.10+
- •A PostgreSQL 14+ database
- •
pgvectorinstalled and enabled in Postgres - •An Anthropic API key
- •A working Python environment with network access to your database and Anthropic API
- •These Python packages:
- •
anthropic - •
psycopg[binary] - •
pgvector - •
python-dotenv
- •
Install them:
pip install anthropic psycopg[binary] pgvector python-dotenv
Enable the extension in Postgres:
CREATE EXTENSION IF NOT EXISTS vector;
Integration Steps
- •
Set up your environment and clients
Keep credentials in environment variables. Create one Anthropic client and one PostgreSQL connection using psycopg.
import os
from dotenv import load_dotenv
import anthropic
import psycopg
load_dotenv()
ANTHROPIC_API_KEY = os.environ["ANTHROPIC_API_KEY"]
DATABASE_URL = os.environ["DATABASE_URL"]
client = anthropic.Anthropic(api_key=ANTHROPIC_API_KEY)
conn = psycopg.connect(DATABASE_URL)
conn.autocommit = True
print("Connected to Anthropic and Postgres")
- •
Create a pgvector table for wealth management knowledge
Store chunks of policy docs, advisor playbooks, fund summaries, or client-facing FAQs as embeddings. Use a fixed embedding dimension that matches the model you choose for embedding generation.
from pgvector.psycopg import register_vector
register_vector(conn)
with conn.cursor() as cur:
cur.execute("""
CREATE TABLE IF NOT EXISTS wealth_docs (
id BIGSERIAL PRIMARY KEY,
source ტექST NOT NULL,
content TEXT NOT NULL,
embedding VECTOR(1536)
)
""")
cur.execute("""
CREATE INDEX IF NOT EXISTS wealth_docs_embedding_idx
ON wealth_docs USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 100)
""")
print("Table ready")
- •
Generate embeddings with Anthropic-compatible workflow and store them
Anthropic’s core API is for language generation, so in production you typically pair it with an embedding model from your stack. If your system already has embeddings from another service, this step is where they get written into pgvector. The important part is the storage/query pattern around Anthropic-generated responses.
import random
def embed_text(text: str) -> list[float]:
# Replace with your production embedding service.
# This stub keeps the example focused on pgvector integration.
random.seed(hash(text) % 2**32)
return [random.random() for _ in range(1536)]
docs = [
("advisor_playbook", "For retirement accounts, confirm risk tolerance before recommending allocation changes."),
("policy_manual", "Do not provide tax advice unless the client has signed off on advisory scope."),
("fund_summary", "The balanced income model portfolio targets moderate volatility with monthly rebalancing."),
]
with conn.cursor() as cur:
for source, content in docs:
emb = embed_text(content)
cur.execute(
"INSERT INTO wealth_docs (source, content, embedding) VALUES (%s, %s, %s)",
(source, content, emb),
)
print("Seeded documents")
- •
Retrieve relevant context from pgvector and send it to Anthropic
This is the core integration: semantic search in Postgres first, then response generation through Anthropic’s Messages API.
def retrieve_context(query: str, k: int = 3) -> list[str]:
query_emb = embed_text(query)
with conn.cursor() as cur:
cur.execute(
"""
SELECT content
FROM wealth_docs
ORDER BY embedding <=> %s::vector
LIMIT %s
""",
(query_emb, k),
)
rows = cur.fetchall()
return [row[0] for row in rows]
user_query = "Can I rebalance a retirement portfolio if the client is close to retirement?"
context_docs = retrieve_context(user_query)
context_block = "\n\n".join(f"- {doc}" for doc in context_docs)
response = client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=300,
temperature=0,
system="You are a wealth management assistant. Use only the provided context.",
messages=[
{
"role": "user",
"content": f"""Answer this question using the retrieved context.
Question: {user_query}
Context:
{context_block}
"""
}
],
)
print(response.content[0].text)
- •
Wrap it as a production function
In a real agent system, this becomes a reusable retrieval-and-generation pipeline behind an API endpoint or worker.
def answer_wealth_question(question: str) -> str:
docs = retrieve_context(question)
prompt = "\n\n".join(f"Context {i+1}: {doc}" for i, doc in enumerate(docs))
msg = client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=250,
temperature=0,
system="You are an internal assistant for wealth management operations.",
messages=[
{
"role": "user",
"content": f"{question}\n\n{prompt}"
}
],
)
return msg.content[0].text
answer = answer_wealth_question("What should I check before changing a client's allocation?")
print(answer)
Testing the Integration
Run a simple end-to-end query after seeding at least a few documents.
test_question = "What is the rule around tax advice?"
result = answer_wealth_question(test_question)
print("QUESTION:", test_question)
print("ANSWER:", result)
Expected output:
QUESTION: What is the rule around tax advice?
ANSWER: Do not provide tax advice unless it falls within the signed advisory scope...
If you get relevant answers grounded in your stored documents instead of generic model output, the retrieval layer is working.
Real-World Use Cases
- •
Advisor copilot
- •Retrieve portfolio policy notes, suitability rules, and product summaries from pgvector.
- •Use Anthropic to draft compliant advisor responses or meeting prep notes.
- •
Client service assistant
- •Answer questions about account procedures, contribution limits, or rebalancing rules using internal knowledge.
- •Keep responses tied to approved documentation instead of free-form generation.
- •
Research summarization pipeline
- •Store market commentary and house research in pgvector.
- •Ask Anthropic to summarize impacts on model portfolios or flag conflicts across sources.
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