How to Integrate Anthropic for fintech with pgvector for AI agents

By Cyprian AaronsUpdated 2026-04-21
anthropic-for-fintechpgvectorai-agents

Combining Anthropic for fintech with pgvector gives you a clean pattern for building AI agents that can reason over regulated financial data without stuffing every document into the prompt. Anthropic handles the language side, while pgvector gives you semantic retrieval over policies, transaction notes, KYC records, support history, and internal runbooks.

The practical win is simple: your agent can answer questions with context pulled from a vector database instead of guessing. That means better grounding, lower token usage, and a much better fit for fintech workflows where traceability matters.

Prerequisites

  • Python 3.10+
  • A running PostgreSQL instance with the pgvector extension enabled
  • An Anthropic API key
  • A database user with permission to create tables and indexes
  • Installed Python packages:
    • anthropic
    • psycopg[binary]
    • pgvector
    • python-dotenv
pip install anthropic psycopg[binary] pgvector python-dotenv

Integration Steps

  1. Set up your environment variables

    Keep secrets out of code. Load your Anthropic key and Postgres connection string from .env.

import os
from dotenv import load_dotenv

load_dotenv()

ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
DATABASE_URL = os.getenv("DATABASE_URL")

if not ANTHROPIC_API_KEY or not DATABASE_URL:
    raise ValueError("Missing ANTHROPIC_API_KEY or DATABASE_URL")
  1. Create the pgvector table

    Use a vector column sized to the embedding model you plan to use. For this example, I’m using a 1536-dimensional vector, which matches common embedding setups.

import psycopg

conn = psycopg.connect(DATABASE_URL)
conn.execute("CREATE EXTENSION IF NOT EXISTS vector;")

conn.execute("""
CREATE TABLE IF NOT EXISTS fintech_docs (
    id SERIAL PRIMARY KEY,
    title TEXT NOT NULL,
    content TEXT NOT NULL,
    metadata JSONB DEFAULT '{}'::jsonb,
    embedding VECTOR(1536)
);
""")

conn.execute("""
CREATE INDEX IF NOT EXISTS fintech_docs_embedding_idx
ON fintech_docs
USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 100);
""")

conn.commit()
conn.close()
  1. Generate embeddings with Anthropic-compatible workflow

    Anthropic’s core API is for generation, not embeddings. In production, pair it with an embedding model service for vectors, then use Anthropic for reasoning and response generation. The agent flow still integrates cleanly: embed content, store in pgvector, retrieve top matches, then send them to Anthropic.

import os
import psycopg
from pgvector.psycopg import register_vector

# Example placeholder embedding function.
# Replace this with your embedding provider of choice.
def embed_text(text: str) -> list[float]:
    # Must return a 1536-dim float list for this schema.
    raise NotImplementedError("Plug in your embedding model here")

conn = psycopg.connect(DATABASE_URL)
register_vector(conn)

docs = [
    {
        "title": "Chargeback policy",
        "content": "Card disputes must be filed within 60 days of the statement date."
    },
    {
        "title": "KYC escalation",
        "content": "High-risk accounts require manual review before account activation."
    }
]

for doc in docs:
    vector = embed_text(doc["content"])
    conn.execute(
        """
        INSERT INTO fintech_docs (title, content, metadata, embedding)
        VALUES (%s, %s, %s::jsonb, %s)
        """,
        (doc["title"], doc["content"], '{"source":"policy"}', vector)
    )

conn.commit()
conn.close()
  1. Retrieve relevant context from pgvector

    This is the retrieval layer your agent will use before calling Anthropic. Query by cosine distance and pull back the most relevant chunks.

import psycopg
from pgvector.psycopg import register_vector

def search_similar(query_embedding: list[float], limit: int = 3):
    conn = psycopg.connect(DATABASE_URL)
    register_vector(conn)

    rows = conn.execute(
        """
        SELECT title, content,
               1 - (embedding <=> %s) AS similarity
        FROM fintech_docs
        ORDER BY embedding <=> %s
        LIMIT %s
        """,
        (query_embedding, query_embedding, limit)
    ).fetchall()

    conn.close()
    return rows
  1. Call Anthropic with retrieved context

    Use Anthropic.messages.create() to generate a grounded answer from the retrieved documents. Keep the prompt tight and include only what the agent needs.

from anthropic import Anthropic

client = Anthropic(api_key=ANTHROPIC_API_KEY)

def answer_fintech_question(question: str):
    query_embedding = embed_text(question)
    matches = search_similar(query_embedding)

    context_block = "\n\n".join(
        f"Title: {title}\nContent: {content}\nSimilarity: {similarity:.3f}"
        for title, content, similarity in matches
    )

    response = client.messages.create(
        model="claude-3-5-sonnet-20241022",
        max_tokens=400,
        temperature=0,
        messages=[
            {
                "role": "user",
                "content": f"""Answer the question using only the context below.

Context:
{context_block}

Question: {question}
"""
            }
        ]
    )

    return response.content[0].text

print(answer_fintech_question("What is the deadline for filing a chargeback?"))

Testing the Integration

Run a simple retrieval-and-answer test with one known policy document in pgvector. If everything is wired correctly, the agent should return an answer grounded in that document.

question = "How long do customers have to file a card dispute?"
result = answer_fintech_question(question)
print(result)

Expected output:

Customers must file card disputes within 60 days of the statement date.

If you get an empty or irrelevant answer:

  • Check that embeddings are being generated consistently
  • Confirm the VECTOR(1536) dimension matches your embedding output
  • Verify ivfflat index creation after enough rows exist
  • Make sure your prompt tells Claude to use only retrieved context

Real-World Use Cases

  • Policy-aware support agents
    Answer customer questions about chargebacks, fees, limits, and account rules using retrieved internal policy docs.

  • KYC/AML assistant workflows
    Pull relevant onboarding rules or escalation playbooks before Claude drafts next-step guidance for analysts.

  • Fraud ops copilots
    Retrieve prior incident notes, runbooks, and case summaries so agents can suggest consistent actions during investigations.


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