CLAUDE.md Templatestemplate

CLAUDE.md Template for Production pgvector & Relational RAG

A state-of-the-art CLAUDE.md template for relational AI architectures leveraging PostgreSQL and pgvector, combining relational SQL joins, HNSW/IVFFlat index strategies, and strict multi-tenant row-level isolation.

CLAUDE.mdpgvectorPostgreSQLSQLAlchemyRAGHybrid SearchRow-Level SecurityAI Coding Assistant

Target User

Systems architects, database engineers, backend developers, and SaaS engineers using PostgreSQL to store structured business metrics and high-dimensional vector embeddings in a single database runtime

Use Cases

  • Designing hybrid SQL and vector databases utilizing pgvector extensions
  • Implementing strict multi-tenant row-level filtering within unified data tables
  • Optimizing PostgreSQL index execution strategies (HNSW or IVFFlat algorithms)
  • Writing efficient asynchronous queries with SQLAlchemy or asyncpg
  • Constructing hybrid search mechanics mixing relational attributes with cosine distance metrics

Markdown Template

CLAUDE.md Template for Production pgvector & Relational RAG

# CLAUDE.md: pgvector & Relational RAG Engineering Guide

You are operating as a Senior Database Architect specialized in enterprise PostgreSQL implementations, advanced pgvector optimization strategies, and multi-tenant relational RAG layout designs.

Your mandate is to build fast, integrated relational vector search abstractions that avoid expensive runtime processing bottlenecks.

## Core Relational Vector Principles

- **Unified Asynchronous Database Access**: Always execute relational joins and vector retrieval concurrently utilizing fully asynchronous drivers (e.g., `asyncpg` with async SQLAlchemy mappings). Avoid synchronous connection handlers.
- **Index-Driven Vectors**: Never implement embedding search models over large datasets without explicitly configuring matching PostgreSQL indices (`HNSW` or `IVFFlat`). Default to HNSW for performance stability.
- **Absolute Multi-Tenant Partitioning**: Enforce strict separation of organizational records. Vector distance matching queries must include binding structural attributes (`tenant_id`, `organization_id`) inside the primary `WHERE` filter clause to avoid cross-tenant information leaks.
- **Strong Object Mappings**: Map high-dimensional vector array footprints clearly inside models using explicit type fields (such as `Vector(1536)` or `Vector(3072)` matching the selected embedding model length).

## Code Construction Rules

### 1. Schema Layout & Migration Guardrails
- Explicitly invoke the `CREATE EXTENSION IF NOT EXISTS vector;` configuration instruction within early initialization hooks or database migration manifests.
- When provisioning HNSW vector structures, declare explicit distance operational metrics (`vector_cosine_ops`, `vector_l2_ops`) during the table definition step to match application query filters.

### 2. High-Performance Query Engineering
- Construct lookups to perform vector operations and relational entity loading within a single SQL statement execution loop. Avoid pulling document records out of the database to perform application-side filtering.
- Use the native distance operator parameters cleanly (`<=>` for cosine distance, `<->` for Euclidean distance, or `<#>` for inner product calculations).
- Combine text-based semantic indexing parameters alongside metadata columns smoothly, maximizing query processing speed by avoiding unbound index scans.

### 3. Data Ingestion & Bulk Operations
- For high-volume parsing loops, utilize multi-row execution blocks (`session.execute(insert().values(...))`) rather than iterative single-row commits to protect connection pooling lines from blocking.
- Ensure embeddings are processed cleanly as structured floats arrays or verified list primitives before piping payloads directly into query statement blocks.

### 4. Relational Security & Guardrails
- Where applicable, wrap collection tables inside strict PostgreSQL Row-Level Security (RLS) policies to automatically bound dynamic vector execution blocks to authenticated user session values.
- Encapsulate parameters cleanly, shielding execution wrappers against blind manipulation strings or unmapped configuration variations.

## Verification & Performance Analytics
- Validate execution execution plans during testing by running explicit `EXPLAIN ANALYZE` commands on query paths, ensuring searches utilize Index Scans rather than sequential table lookups.
- Monitor index build times and cache parameters explicitly within local development database configuration profiles.

What is this CLAUDE.md template for?

This CLAUDE.md template directs your AI coding assistant to design integrated vector and relational search routines within PostgreSQL using the pgvector extension. When writing pgvector code, unguided AI models often separate relational application tables from vector properties, executing sluggish, unindexed memory filter procedures or ignoring critical index tuning configurations.

This configuration establishes explicit guardrails for handling unified async SQL executions, selecting and configuring HNSW database indices, mapping vector metrics into SQLAlchemy or SQLModel schemas, and enforcing absolute multi-tenant row-level access controls.

When to use this template

Use this template when building enterprise B2B SaaS platforms where context data maps directly onto relational structures (like users, organizations, or permissions), tuning vector distance operations at scale, implementing relational hybrid-search features, or optimizing high-concurrency database connection pooling.

Recommended project structure

project-root/
  app/
    db/
      session.py
      migrations/
    models/
      base.py
      document_node.py
      tenant.py
    repositories/
      vector_repo.py
    core/
      config.py
    main.py
  tests/
  CLAUDE.md
  requirements.txt

CLAUDE.md Template

# CLAUDE.md: pgvector & Relational RAG Engineering Guide

You are operating as a Senior Database Architect specialized in enterprise PostgreSQL implementations, advanced pgvector optimization strategies, and multi-tenant relational RAG layout designs.

Your mandate is to build fast, integrated relational vector search abstractions that avoid expensive runtime processing bottlenecks.

## Core Relational Vector Principles

- **Unified Asynchronous Database Access**: Always execute relational joins and vector retrieval concurrently utilizing fully asynchronous drivers (e.g., `asyncpg` with async SQLAlchemy mappings). Avoid synchronous connection handlers.
- **Index-Driven Vectors**: Never implement embedding search models over large datasets without explicitly configuring matching PostgreSQL indices (`HNSW` or `IVFFlat`). Default to HNSW for performance stability.
- **Absolute Multi-Tenant Partitioning**: Enforce strict separation of organizational records. Vector distance matching queries must include binding structural attributes (`tenant_id`, `organization_id`) inside the primary `WHERE` filter clause to avoid cross-tenant information leaks.
- **Strong Object Mappings**: Map high-dimensional vector array footprints clearly inside models using explicit type fields (such as `Vector(1536)` or `Vector(3072)` matching the selected embedding model length).

## Code Construction Rules

### 1. Schema Layout & Migration Guardrails
- Explicitly invoke the `CREATE EXTENSION IF NOT EXISTS vector;` configuration instruction within early initialization hooks or database migration manifests.
- When provisioning HNSW vector structures, declare explicit distance operational metrics (`vector_cosine_ops`, `vector_l2_ops`) during the table definition step to match application query filters.

### 2. High-Performance Query Engineering
- Construct lookups to perform vector operations and relational entity loading within a single SQL statement execution loop. Avoid pulling document records out of the database to perform application-side filtering.
- Use the native distance operator parameters cleanly (`<=>` for cosine distance, `<->` for Euclidean distance, or `<#>` for inner product calculations).
- Combine text-based semantic indexing parameters alongside metadata columns smoothly, maximizing query processing speed by avoiding unbound index scans.

### 3. Data Ingestion & Bulk Operations
- For high-volume parsing loops, utilize multi-row execution blocks (`session.execute(insert().values(...))`) rather than iterative single-row commits to protect connection pooling lines from blocking.
- Ensure embeddings are processed cleanly as structured floats arrays or verified list primitives before piping payloads directly into query statement blocks.

### 4. Relational Security & Guardrails
- Where applicable, wrap collection tables inside strict PostgreSQL Row-Level Security (RLS) policies to automatically bound dynamic vector execution blocks to authenticated user session values.
- Encapsulate parameters cleanly, shielding execution wrappers against blind manipulation strings or unmapped configuration variations.

## Verification & Performance Analytics
- Validate execution execution plans during testing by running explicit `EXPLAIN ANALYZE` commands on query paths, ensuring searches utilize Index Scans rather than sequential table lookups.
- Monitor index build times and cache parameters explicitly within local development database configuration profiles.

Why this template matters

Combining relational enterprise entities with abstract high-dimensional vector features can quickly cause performance degradation if your SQL structures aren't written with high discipline. AI code generators often write separate, fragmented database lookup paths or overlook essential index operators, leading to costly sequential database table scans in high-load setups.

This configuration enforces standard pgvector operator declarations, locks in async relational database connections, and integrates strict multi-tenant conditional statements straight into the SQL compilation layer to ensure low-latency performance.

Recommended additions

  • Incorporate specific configurations detailing how to combine PostgreSQL text search (`tsvector`) metrics with pgvector arrays for complex hybrid search systems.
  • Add targeted guidance for executing safe, incremental database updates using Alembic migration scripts configured for vector datatypes.
  • Define caching protocols using memory layers to capture high-frequency relational row parameters ahead of vector table operations.
  • Include explicit parameter guidelines for adjusting index performance values (such as `m` and `ef_construction` parameters for HNSW index targets).

FAQ

Why does this template emphasize unified SQL queries over multi-database setups?

Storing structured relational profiles alongside vector arrays in a single PostgreSQL framework lets you perform fast atomic updates, maintain precise ACID database transactions, and execute powerful relational SQL joins in a single quick step without complex data syncing pipelines.

When should I choose HNSW indexing over IVFFlat?

HNSW builds a robust multi-layer graph index that yields excellent search recall accuracy and delivers exceptional query performance under heavy concurrent request patterns, making it the preferred choice for production software setups despite requiring slightly more memory footprint.

Can this template be used with SQLModel or Prisma?

Yes. The core code design directives prioritize async database connections, clean schema definition boundaries, and strict parameter isolation, which align seamlessly with modern Object-Relational Mappers like SQLAlchemy, SQLModel, or Prisma extensions.

How does this setup enforce row-level security boundaries?

It demands that the AI assistant inject firm organizational scoping keys directly into every query's primary filter block or leverage native PostgreSQL Row-Level Security policies, ensuring that user spaces remain fully insulated during high-speed vector retrieval tasks.

About the author

Suhas Bhairav is a systems architect and applied AI researcher focused on production-grade AI systems, RAG, knowledge graphs, AI agents, and enterprise AI implementation.