How to Integrate Anthropic for wealth management with pgvector for RAG

By Cyprian AaronsUpdated 2026-04-21
anthropic-for-wealth-managementpgvectorrag

Combining Anthropic for wealth management with pgvector gives you a practical RAG stack for client-facing financial workflows. You can ground model responses in portfolio notes, product docs, suitability policies, and market research stored in PostgreSQL, then have Anthropic generate compliant, contextual answers for advisors and relationship managers.

This is the pattern I’d use for wealth platforms that need retrieval over private data without pushing everything into a separate vector database.

Prerequisites

  • Python 3.10+
  • PostgreSQL 14+ with the pgvector extension installed
  • An Anthropic API key
  • A database user with permission to create tables and extensions
  • Python packages:
    • anthropic
    • psycopg[binary]
    • pgvector
    • python-dotenv

Install them:

pip install anthropic psycopg[binary] pgvector python-dotenv

Create the extension in PostgreSQL:

CREATE EXTENSION IF NOT EXISTS vector;

Set environment variables:

export ANTHROPIC_API_KEY="your_key"
export DATABASE_URL="postgresql://user:password@localhost:5432/wealth_rag"

Integration Steps

  1. Create a vector-enabled table in PostgreSQL

Use a fixed embedding dimension that matches your embedding model. For production, keep the schema explicit and index the vector column.

import os
import psycopg
from pgvector.psycopg import register_vector

DATABASE_URL = os.environ["DATABASE_URL"]

with psycopg.connect(DATABASE_URL) as conn:
    register_vector(conn)
    with conn.cursor() as cur:
        cur.execute("""
            CREATE TABLE IF NOT EXISTS wealth_documents (
                id SERIAL PRIMARY KEY,
                source ტექxt,
                content TEXT NOT NULL,
                embedding VECTOR(1536)
            )
        """)
        cur.execute("""
            CREATE INDEX IF NOT EXISTS wealth_documents_embedding_idx
            ON wealth_documents
            USING ivfflat (embedding vector_cosine_ops)
            WITH (lists = 100)
        """)
        conn.commit()
  1. Generate embeddings with Anthropic-compatible workflow

Anthropic’s core API is for generation, so in practice you pair it with an embedding model from your stack and keep Anthropic for reasoning and response generation. If you already have embeddings from another provider, store them here; the retrieval layer stays the same.

import os
import json
import psycopg
from pgvector.psycopg import register_vector

DATABASE_URL = os.environ["DATABASE_URL"]

def store_document(content: str, embedding: list[float], source: str):
    with psycopg.connect(DATABASE_URL) as conn:
        register_vector(conn)
        with conn.cursor() as cur:
            cur.execute(
                """
                INSERT INTO wealth_documents (source, content, embedding)
                VALUES (%s, %s, %s)
                """,
                (source, content, embedding),
            )
        conn.commit()

# Example embedding placeholder from your embedding service.
sample_embedding = [0.01] * 1536
store_document(
    content="Client IPS requires capital preservation and income-focused allocation.",
    embedding=sample_embedding,
    source="ips_001.pdf",
)
  1. Retrieve relevant context from pgvector

This is the RAG part. Query by cosine distance and return the top matches.

import os
import psycopg
from pgvector.psycopg import register_vector

DATABASE_URL = os.environ["DATABASE_URL"]

def retrieve_context(query_embedding: list[float], k: int = 5) -> list[str]:
    with psycopg.connect(DATABASE_URL) as conn:
        register_vector(conn)
        with conn.cursor() as cur:
            cur.execute(
                """
                SELECT content
                FROM wealth_documents
                ORDER BY embedding <=> %s
                LIMIT %s
                """,
                (query_embedding, k),
            )
            rows = cur.fetchall()
    return [row[0] for row in rows]

query_embedding = [0.02] * 1536
context_chunks = retrieve_context(query_embedding)
print(context_chunks)
  1. Call Anthropic with retrieved context

Use Anthropic’s Messages API to ground the answer in retrieved passages. Keep the prompt tight and include instructions for compliance-sensitive output.

import os
from anthropic import Anthropic

client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

def answer_with_context(question: str, contexts: list[str]) -> str:
    context_block = "\n\n".join(f"- {c}" for c in contexts)

    response = client.messages.create(
        model="claude-3-5-sonnet-20241022",
        max_tokens=500,
        temperature=0,
        messages=[
            {
                "role": "user",
                "content": f"""
You are assisting a wealth management advisor.
Use only the provided context to answer.
If the context is insufficient, say so clearly.

Context:
{context_block}

Question:
{question}
"""
            }
        ],
    )
    return response.content[0].text

print(answer_with_context("What allocation style does the IPS require?", context_chunks))
  1. Wrap retrieval + generation into one agent function

This is what you expose to your app or workflow engine.

def rag_answer(question: str, query_embedding: list[float]) -> str:
    contexts = retrieve_context(query_embedding=query_embedding, k=4)
    return answer_with_context(question=question, contexts=contexts)

result = rag_answer(
    "What investment objective is stated in the client IPS?",
    query_embedding=[0.02] * 1536,
)
print(result)

Testing the Integration

Run a smoke test against one known document and verify retrieval plus generation.

test_query_embedding = [0.01] * 1536

contexts = retrieve_context(test_query_embedding, k=1)
print("Retrieved:", contexts[0])

answer = answer_with_context(
    "Summarize the client's investment constraint.",
    contexts,
)
print("Answer:", answer)

Expected output:

Retrieved: Client IPS requires capital preservation and income-focused allocation.
Answer: The client's investment constraint emphasizes capital preservation and an income-focused allocation.

If retrieval returns irrelevant text, check these first:

  • Your embedding dimension matches the table definition.
  • The query embedding comes from the same model family as stored vectors.
  • The pgvector index exists and is using the correct operator class.

Real-World Use Cases

  • Advisor copilots that answer questions from IPS documents, product sheets, fee schedules, and internal policy manuals.
  • Client meeting prep agents that summarize portfolio changes, risk constraints, and open action items from CRM notes plus research docs.
  • Compliance assistants that flag whether a proposed recommendation conflicts with documented suitability rules or house policy.

Keep learning

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

Related Guides