How to Integrate Anthropic for fintech with pgvector for multi-agent systems

By Cyprian AaronsUpdated 2026-04-21
anthropic-for-fintechpgvectormulti-agent-systems

Combining Anthropic for fintech with pgvector gives you a practical pattern for multi-agent systems that need both reasoning and retrieval. The model handles policy-aware analysis, summarization, and tool orchestration, while pgvector stores embeddings for transaction notes, KYC documents, support tickets, and internal runbooks that agents can query at runtime.

This is the setup you want when one agent needs to classify an incoming payment dispute, another needs to retrieve similar cases, and a supervisor agent needs to produce a compliant response grounded in your internal knowledge base.

Prerequisites

  • Python 3.10+
  • PostgreSQL 15+ with the pgvector extension installed
  • An Anthropic API key
  • Access to an Anthropic model that supports your workload
  • A PostgreSQL database URL
  • pip packages:
    • anthropic
    • psycopg[binary]
    • pgvector
    • python-dotenv
  • A table schema ready for vector storage
  • Basic familiarity with async Python if you plan to run multiple agents concurrently

Install the dependencies:

pip install anthropic psycopg[binary] pgvector python-dotenv

Enable the extension in Postgres:

CREATE EXTENSION IF NOT EXISTS vector;

Integration Steps

1) Create the vector table for agent memory

For multi-agent systems, keep shared memory in Postgres so every agent can retrieve the same case history. Use a vector column plus metadata for tenant, document type, and source.

import os
import psycopg
from pgvector.psycopg import register_vector

DB_URL = os.environ["DATABASE_URL"]

with psycopg.connect(DB_URL) as conn:
    register_vector(conn)
    with conn.cursor() as cur:
        cur.execute("""
            CREATE TABLE IF NOT EXISTS agent_memory (
                id BIGSERIAL PRIMARY KEY,
                tenant_id TEXT NOT NULL,
                doc_type TEXT NOT NULL,
                content TEXT NOT NULL,
                embedding VECTOR(1536),
                metadata JSONB DEFAULT '{}'::jsonb,
                created_at TIMESTAMPTZ DEFAULT now()
            );
        """)
        cur.execute("""
            CREATE INDEX IF NOT EXISTS agent_memory_embedding_idx
            ON agent_memory USING ivfflat (embedding vector_cosine_ops)
            WITH (lists = 100);
        """)
    conn.commit()

2) Generate embeddings with Anthropic-compatible text inputs

Anthropic’s core SDK is used here for reasoning and generation. For embeddings, many fintech stacks pair Anthropic with a dedicated embedding model service; if your architecture already has an internal embedding service, drop it into this function. The key is that the same text normalization pipeline feeds both retrieval and generation.

import os
from anthropic import Anthropic

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

def normalize_text(text: str) -> str:
    return " ".join(text.strip().split())

def embed_text(text: str) -> list[float]:
    # Replace this with your embedding provider.
    # Keep the interface stable so pgvector storage stays isolated.
    normalized = normalize_text(text)
    raise NotImplementedError(f"Embed: {normalized[:80]}")

If you already have an embedding provider, keep the contract simple: input text in, fixed-length float array out. That array is what gets written into VECTOR(n).

3) Store documents and cases in pgvector

Insert customer support transcripts, policy docs, or suspicious activity notes into agent_memory. Multi-agent systems work better when retrieval is scoped by tenant and doc type.

import json
import psycopg
from pgvector.psycopg import register_vector

def save_memory(conn, tenant_id: str, doc_type: str, content: str, embedding: list[float], metadata: dict):
    register_vector(conn)
    with conn.cursor() as cur:
        cur.execute(
            """
            INSERT INTO agent_memory (tenant_id, doc_type, content, embedding, metadata)
            VALUES (%s, %s, %s, %s, %s)
            """,
            (tenant_id, doc_type, content, embedding, json.dumps(metadata)),
        )
    conn.commit()

with psycopg.connect(DB_URL) as conn:
    save_memory(
        conn=conn,
        tenant_id="bank-001",
        doc_type="chargeback_case",
        content="Customer disputes card charge from merchant MCC 5732. Similar case resolved by refund after fraud check.",
        embedding=[0.01] * 1536,
        metadata={"case_id": "cb_1042", "priority": "high"},
    )

4) Retrieve similar context for each agent

Use pgvector similarity search to fetch relevant cases before calling Anthropic. In a multi-agent setup, one agent can retrieve evidence while another drafts the response.

import psycopg
from pgvector.psycopg import register_vector

def find_similar_cases(conn, query_embedding: list[float], tenant_id: str, limit: int = 5):
    register_vector(conn)
    with conn.cursor() as cur:
        cur.execute(
            """
            SELECT id, doc_type, content, metadata,
                   1 - (embedding <=> %s::vector) AS similarity
            FROM agent_memory
            WHERE tenant_id = %s
            ORDER BY embedding <=> %s::vector
            LIMIT %s;
            """,
            (query_embedding, tenant_id, query_embedding, limit),
        )
        return cur.fetchall()

5) Call Anthropic with retrieved context to produce a grounded answer

Now pass retrieved records into Anthropic’s Messages API. This is where the system becomes useful: retrieval constrains the model to your internal case history instead of free-form guessing.

from anthropic import Anthropic

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

def draft_response(user_query: str, retrieved_cases: list[tuple]) -> str:
    context_blocks = []
    for row in retrieved_cases:
        _id, doc_type, content, metadata, similarity = row
        context_blocks.append(
            f"[{doc_type} | similarity={similarity:.3f} | meta={metadata}]\n{content}"
        )

    prompt = f"""
You are a fintech operations assistant.
Answer using only the provided case history when possible.
If evidence is insufficient, say what additional review is needed.

User query:
{user_query}

Retrieved context:
{chr(10).join(context_blocks)}
""".strip()

    message = client.messages.create(
        model="claude-3-5-sonnet-latest",
        max_tokens=400,
        temperature=0.2,
        messages=[
            {"role": "user", "content": prompt}
        ],
    )
    return message.content[0].text

Testing the Integration

Run a minimal end-to-end check by inserting one record and querying it back through similarity search plus Anthropic generation.

import os
import psycopg
from pgvector.psycopg import register_vector

DB_URL = os.environ["DATABASE_URL"]

query_embedding = [0.01] * 1536

with psycopg.connect(DB_URL) as conn:
    register_vector(conn)
    results = find_similar_cases(conn, query_embedding=query_embedding, tenant_id="bank-001", limit=1)

print("Retrieved:", len(results))
print("Top match:", results[0][2])

response = draft_response(
    "What should we do with this disputed card charge?",
    results,
)

print("Model response:", response[:200])

Expected output:

Retrieved: 1
Top match: Customer disputes card charge from merchant MCC 5732...
Model response: Based on the prior chargeback case...

Real-World Use Cases

  • Chargeback triage

    • One agent classifies dispute type.
    • Another retrieves similar historical resolutions from pgvector.
    • Anthropic drafts a compliant next-step recommendation for ops staff.
  • KYC/AML investigation assistants

    • Store analyst notes and alert summaries in pgvector.
    • Retrieve related alerts across entities or time windows.
    • Use Anthropic to summarize risk patterns and propose escalation language.
  • Policy-grounded customer support

    • Agents fetch product terms and exception handling rules from Postgres.
    • Anthropic generates responses aligned to internal policy.
    • A supervisor agent checks whether the answer cites enough supporting evidence before sending it out.

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