How to Integrate Anthropic for payments with pgvector for multi-agent systems
Combining Anthropic for payments with pgvector gives you a clean pattern for agent systems that need both decision-making and memory. Anthropic handles the payment-facing reasoning layer, while pgvector stores semantic context so multiple agents can retrieve prior cases, policy notes, and transaction history before acting.
That matters when you’re building payment workflows with review, fraud checks, or dispute handling. One agent can classify the request, another can fetch similar historical cases from vector search, and a third can generate the final action or response using Anthropic.
Prerequisites
- •Python 3.10+
- •A running PostgreSQL instance with the
pgvectorextension enabled - •
psycopg2-binaryorpsycopginstalled - •
anthropicPython SDK installed - •API key for Anthropic set in your environment
- •Database credentials for PostgreSQL
- •A table schema ready for storing embeddings and payment case metadata
Install the core packages:
pip install anthropic psycopg2-binary pgvector openai
If your Postgres instance does not have pgvector, enable it first:
CREATE EXTENSION IF NOT EXISTS vector;
Integration Steps
- •Set up Anthropic and PostgreSQL clients
Start by initializing both clients in the same service. In production, keep API keys in environment variables and use connection pooling for Postgres.
import os
import anthropic
import psycopg2
ANTHROPIC_API_KEY = os.environ["ANTHROPIC_API_KEY"]
DB_URL = os.environ["DATABASE_URL"]
client = anthropic.Anthropic(api_key=ANTHROPIC_API_KEY)
conn = psycopg2.connect(DB_URL)
conn.autocommit = True
cur = conn.cursor()
- •Create a pgvector table for agent memory
Store payment cases, support notes, or policy snippets as embeddings. Use a fixed embedding dimension that matches your embedding model.
cur.execute("""
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE IF NOT EXISTS payment_agent_memory (
id BIGSERIAL PRIMARY KEY,
case_id TEXT NOT NULL,
role TEXT NOT NULL,
content TEXT NOT NULL,
embedding vector(1536)
);
""")
For embeddings, use a dedicated embedding model. Anthropic’s Claude models are for reasoning and generation; for vector storage you typically use an embedding model from your stack, then pass retrieved context into Claude.
- •Embed and store payment-related context
A multi-agent system works better when each agent writes its observations into shared memory. Here’s a simple pattern using an embedding client plus pgvector storage.
from openai import OpenAI
embed_client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
def embed_text(text: str):
resp = embed_client.embeddings.create(
model="text-embedding-3-small",
input=text
)
return resp.data[0].embedding
def save_memory(case_id: str, role: str, content: str):
embedding = embed_text(content)
cur.execute(
"""
INSERT INTO payment_agent_memory (case_id, role, content, embedding)
VALUES (%s, %s, %s, %s)
""",
(case_id, role, content, embedding)
)
- •Retrieve similar cases before calling Anthropic
This is where pgvector earns its keep. Before an agent makes a payment decision or drafts a response, pull semantically similar cases from memory.
def retrieve_similar_cases(query: str, limit: int = 5):
query_embedding = embed_text(query)
cur.execute(
"""
SELECT case_id, role, content
FROM payment_agent_memory
ORDER BY embedding <-> %s::vector
LIMIT %s
""",
(query_embedding, limit)
)
return cur.fetchall()
Now feed that context into Anthropic’s Messages API.
def generate_payment_decision(user_request: str):
similar_cases = retrieve_similar_cases(user_request)
context_lines = [
f"[{case_id} | {role}] {content}"
for case_id, role, content in similar_cases
]
prompt = f"""
You are a payments operations agent.
Use the retrieved cases to decide how to handle this request.
Request:
{user_request}
Relevant history:
{chr(10).join(context_lines)}
"""
response = client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=400,
messages=[
{"role": "user", "content": prompt}
]
)
return response.content[0].text
- •Wire multiple agents together through shared vector memory
In a real system, one agent classifies the request while another handles resolution. Both read and write to the same pgvector-backed store.
def classify_payment_case(case_text: str):
response = client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=100,
messages=[
{"role": "user", "content": f"Classify this payment case:\n{case_text}"}
]
)
return response.content[0].text
def resolve_payment_case(case_id: str, case_text: str):
classification = classify_payment_case(case_text)
save_memory(case_id, "classifier", classification)
decision = generate_payment_decision(case_text)
save_memory(case_id, "resolver", decision)
return {
"classification": classification,
"decision": decision,
}
Testing the Integration
Run a quick end-to-end test with one sample payment dispute. This verifies embedding storage, similarity retrieval, and Anthropic generation all work together.
test_case_id = "pay_10021"
test_case_text = """
Customer says card was charged twice for the same subscription renewal.
They want one charge reversed and confirmation by email.
"""
save_memory(test_case_id, "input", test_case_text)
result = resolve_payment_case(test_case_id, test_case_text)
print("Classification:", result["classification"])
print("Decision:", result["decision"])
Expected output:
Classification: Duplicate charge dispute; likely refund required after verification.
Decision: Based on similar cases...
- Verify both transactions match merchant reference IDs.
- If duplicate is confirmed, reverse one charge.
- Send customer confirmation email with timeline.
Real-World Use Cases
- •
Payment dispute triage
- •One agent classifies disputes.
- •Another retrieves similar prior disputes from pgvector.
- •Anthropic generates the final customer-facing resolution.
- •
Fraud review assistant
- •Store suspicious patterns as vectors.
- •Retrieve comparable fraud signals across agents.
- •Use Claude to explain why a transaction should be held or released.
- •
Collections and recovery workflows
- •Agents share notes on delinquent accounts through pgvector.
- •Claude drafts compliant outreach messages based on retrieved history.
- •Different agents handle prioritization, messaging, and escalation without losing context.
The main pattern is simple: use pgvector as shared memory and Anthropic as the reasoning layer. That gives you multi-agent behavior without forcing every agent to carry full conversation history or reprocess old cases on every request.
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