How to Integrate Anthropic for healthcare with pgvector for AI agents

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

Combining Anthropic for healthcare with pgvector gives you a practical pattern for building clinical AI agents that can retrieve patient-adjacent context, policy docs, care pathways, and prior case notes before generating a response. The value is simple: use pgvector as your retrieval layer, then let Anthropic handle reasoning, summarization, and clinician-facing outputs with the right guardrails.

Prerequisites

  • Python 3.10+
  • A running PostgreSQL 14+ instance
  • The pgvector extension installed in Postgres
  • An Anthropic API key with access to the healthcare-capable model you plan to use
  • A database schema for storing embeddings and metadata
  • pip packages:
    • anthropic
    • psycopg[binary]
    • pgvector
    • python-dotenv

Integration Steps

  1. Install dependencies and enable pgvector
pip install anthropic psycopg[binary] pgvector python-dotenv

Then create the extension in your database:

CREATE EXTENSION IF NOT EXISTS vector;

If you are using Docker or managed Postgres, make sure the extension is available before you start writing vectors.

  1. Create a table for healthcare documents and embeddings

Use one table for source text plus the embedding vector. Keep metadata separate so you can filter by document type, specialty, or tenant.

import os
import psycopg
from pgvector.psycopg import register_vector

DB_URL = os.getenv("DATABASE_URL")

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

That index assumes a 1536-dimensional embedding. Match it to the embedding model you choose.

  1. Generate embeddings with Anthropic and store them in Postgres

Anthropic’s chat models do the reasoning part. For embeddings, many teams pair Anthropic with a dedicated embedding model from their stack. If your architecture already has an embedding service, plug it into the same pipeline; if not, keep this step abstracted behind a function.

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

def embed_text(text: str) -> list[float]:
    # Replace with your embedding provider.
    # Keep the interface stable so retrieval stays decoupled from generation.
    raise NotImplementedError("Connect your embedding model here")

docs = [
    {
        "doc_type": "policy",
        "title": "Diabetes Follow-up Protocol",
        "content": "Patients with uncontrolled diabetes should be scheduled for follow-up within 14 days...",
        "metadata": {"specialty": "endocrinology"}
    },
    {
        "doc_type": "note",
        "title": "Care Coordination Summary",
        "content": "Patient reports improved adherence after medication counseling...",
        "metadata": {"specialty": "primary_care"}
    }
]

with psycopg.connect(os.getenv("DATABASE_URL")) as conn:
    register_vector(conn)
    with conn.cursor() as cur:
        for doc in docs:
            vector = embed_text(doc["content"])
            cur.execute(
                """
                INSERT INTO healthcare_docs (doc_type, title, content, metadata, embedding)
                VALUES (%s, %s, %s, %s::jsonb, %s)
                """,
                (doc["doc_type"], doc["title"], doc["content"], json.dumps(doc["metadata"]), vector),
            )
        conn.commit()

This is the core pattern: store raw text plus vector once, then query by similarity at runtime.

  1. Retrieve relevant context from pgvector and send it to Anthropic

Use cosine distance search to pull the top matches. Then pass those results into Anthropic’s Messages API so the model answers using retrieved context instead of guessing.

import os
import psycopg
from anthropic import Anthropic
from pgvector.psycopg import register_vector

client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

def embed_query(text: str) -> list[float]:
    # Same embedding provider used during ingestion.
    raise NotImplementedError("Connect your embedding model here")

question = "What follow-up should we recommend for an uncontrolled diabetes patient?"

query_vector = embed_query(question)

with psycopg.connect(os.getenv("DATABASE_URL")) as conn:
    register_vector(conn)
    with conn.cursor() as cur:
        cur.execute(
            """
            SELECT title, content
            FROM healthcare_docs
            ORDER BY embedding <=> %s
            LIMIT 3;
            """,
            (query_vector,),
        )
        rows = cur.fetchall()

context = "\n\n".join([f"Title: {title}\nContent: {content}" for title, content in rows])

response = client.messages.create(
    model="claude-3-5-sonnet-latest",
    max_tokens=400,
    temperature=0,
    messages=[
        {
            "role": "user",
            "content": f"""Use only the context below to answer.

Context:
{context}

Question:
{question}
"""
        }
    ],
)

print(response.content[0].text)

That messages.create(...) call is where Anthropic turns retrieved clinical context into a grounded response.

  1. Add guardrails for healthcare workflows

For production healthcare agents, do not send raw PHI unless your compliance posture supports it. Redact identifiers before indexing when possible, and keep audit logs for every retrieval and generation request.

import re

PHI_PATTERNS = [
    r"\b\d{3}-\d{2}-\d{4}\b",   # SSN-like patterns
    r"\b\d{10}\b",              # phone-like patterns without formatting
]

def redact_phi(text: str) -> str:
    redacted = text
    for pattern in PHI_PATTERNS:
        redacted = re.sub(pattern, "[REDACTED]", redacted)
    return redacted

safe_content = redact_phi("Patient John Doe called from 5551234567 about lab results.")
print(safe_content)

In real systems, put this before both embedding and prompt construction.

Testing the Integration

Run a retrieval-plus-generation test against a known document. If everything is wired correctly, pgvector should return the most relevant row and Anthropic should summarize it without drifting off-topic.

test_question = "When should we schedule follow-up for uncontrolled diabetes?"
test_vector = embed_query(test_question)

with psycopg.connect(os.getenv("DATABASE_URL")) as conn:
    register_vector(conn)
    with conn.cursor() as cur:
        cur.execute(
            """
            SELECT title, content
            FROM healthcare_docs
            ORDER BY embedding <=> %s
            LIMIT 1;
            """,
            (test_vector,),
        )
        top_doc = cur.fetchone()

print("Top match:", top_doc[0])

result = client.messages.create(
    model="claude-3-5-sonnet-latest",
    max_tokens=200,
    temperature=0,
    messages=[
        {
            "role": "user",
            "content": f"Answer using this document only:\n\n{top_doc[1]}\n\nQuestion: {test_question}"
        }
    ],
)

print(result.content[0].text)

Expected output:

Top match: Diabetes Follow-up Protocol

Patients with uncontrolled diabetes should be scheduled for follow-up within 14 days...

Real-World Use Cases

  • Clinical policy assistant

    • Retrieve hospital protocols from pgvector and have Anthropic generate concise guidance for nurses or care coordinators.
  • Prior authorization copilot

    • Index payer rules, coverage criteria, and internal playbooks so agents can draft authorization packets faster.
  • Patient support triage

    • Search symptom guidance or care pathways first, then use Anthropic to produce safe next-step recommendations routed to staff review.

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