How to Integrate Anthropic for banking with pgvector for startups

By Cyprian AaronsUpdated 2026-04-21
anthropic-for-bankingpgvectorstartups

Combining Anthropic for banking with pgvector gives you a practical pattern for building AI agents that can answer customer questions, retrieve policy or transaction context, and stay grounded in your own data. For startups, this is the difference between a generic chatbot and a bank-ready assistant that can search relevant records, summarize them, and produce controlled responses.

Prerequisites

  • Python 3.10+
  • A PostgreSQL instance with the pgvector extension enabled
  • An Anthropic API key
  • psycopg or psycopg2 installed for PostgreSQL access
  • anthropic Python SDK installed
  • A table design for storing embeddings plus metadata like customer ID, document type, and timestamps
  • Access controls in place if you’re indexing sensitive banking data

Install the packages:

pip install anthropic psycopg[binary] pgvector

Integration Steps

  1. Set up PostgreSQL with pgvector

    You need a vector column to store embeddings generated from banking documents, notes, or case records.

import psycopg
from pgvector.psycopg import register_vector

conn = psycopg.connect("postgresql://postgres:password@localhost:5432/banking")
register_vector(conn)

with conn.cursor() as cur:
    cur.execute("CREATE EXTENSION IF NOT EXISTS vector;")
    cur.execute("""
        CREATE TABLE IF NOT EXISTS bank_knowledge (
            id BIGSERIAL PRIMARY KEY,
            customer_id TEXT NOT NULL,
            content TEXT NOT NULL,
            embedding vector(1536),
            created_at TIMESTAMPTZ DEFAULT now()
        );
    """)
    conn.commit()
  1. Generate embeddings from banking content using Anthropic-compatible workflow

    Anthropic’s core SDK is for text generation, so in production you typically pair it with an embedding model service. If you already have embeddings from another internal service, this step still fits the same storage pattern.

import os
import anthropic

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

def chunk_text(text: str) -> list[str]:
    return [text[i:i+1000] for i in range(0, len(text), 1000)]

def generate_summary(text: str) -> str:
    msg = client.messages.create(
        model="claude-3-5-sonnet-20241022",
        max_tokens=300,
        messages=[
            {
                "role": "user",
                "content": f"Summarize this banking note for retrieval indexing:\n\n{text}"
            }
        ]
    )
    return msg.content[0].text
  1. Insert embeddings into pgvector

    Use your embedding provider of choice to create vectors, then store them alongside the source text. The important part is the vector(...) column and similarity search later.

from pgvector import Vector

def fake_embedding(text: str) -> list[float]:
    # Replace with your real embedding service.
    return [0.01] * 1536

docs = [
    ("cust_001", "Customer requested a chargeback for card transaction on 2024-03-11."),
    ("cust_001", "Mortgage application pending income verification."),
]

with conn.cursor() as cur:
    for customer_id, content in docs:
        embedding = fake_embedding(content)
        cur.execute(
            """
            INSERT INTO bank_knowledge (customer_id, content, embedding)
            VALUES (%s, %s, %s)
            """,
            (customer_id, content, Vector(embedding))
        )
    conn.commit()
  1. Query pgvector and send retrieved context to Anthropic

    This is the core agent flow: retrieve top matches from PostgreSQL, then pass them into Claude as grounded context.

def search_similar(customer_id: str, query_embedding: list[float], limit: int = 3):
    with conn.cursor() as cur:
        cur.execute(
            """
            SELECT content
            FROM bank_knowledge
            WHERE customer_id = %s
            ORDER BY embedding <-> %s::vector
            LIMIT %s
            """,
            (customer_id, Vector(query_embedding), limit)
        )
        return [row[0] for row in cur.fetchall()]

query = "Why was the chargeback opened?"
query_embedding = fake_embedding(query)
context_docs = search_similar("cust_001", query_embedding)

prompt = f"""
Use only the context below to answer the banking question.

Context:
{chr(10).join(f"- {doc}" for doc in context_docs)}

Question: {query}
"""

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=250,
    messages=[{"role": "user", "content": prompt}]
)

print(response.content[0].text)
  1. Wrap it in an agent function

    In production, your app should expose one function that handles retrieval and generation together.

def answer_banking_question(customer_id: str, question: str) -> str:
    q_embedding = fake_embedding(question)
    docs = search_similar(customer_id, q_embedding)

    prompt = f"""
You are a banking assistant.
Answer only from the provided records.

Records:
{chr(10).join(f"- {d}" for d in docs)}

Question: {question}
"""

    msg = client.messages.create(
        model="claude-3-5-sonnet-20241022",
        max_tokens=200,
        messages=[{"role": "user", "content": prompt}]
    )
    return msg.content[0].text

print(answer_banking_question("cust_001", "What is the current status of my mortgage application?"))

Testing the Integration

Run a simple end-to-end check: insert one record, retrieve it by similarity, and ask Claude to summarize it.

test_question = "Tell me why this customer contacted support."
result = answer_banking_question("cust_001", test_question)
print(result)

Expected output:

The customer contacted support regarding a chargeback related to a card transaction on 2024-03-11.

If you get back unrelated text or hallucinated details:

  • Verify your similarity search is returning the right rows
  • Confirm embeddings are consistent in dimension and model family
  • Make sure your prompt restricts Claude to retrieved context only

Real-World Use Cases

  • Customer support copilot
    Retrieve account notes, recent disputes, and policy snippets from pgvector, then use Anthropic to draft accurate replies.

  • Loan ops assistant
    Index application documents and underwriting notes so agents can ask questions like “what’s missing from this file?” and get grounded answers.

  • Compliance review helper
    Store regulatory guidance and prior case decisions in pgvector, then have Claude summarize relevant evidence for analysts.


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