This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: chromadb-v06-hnsw-filter-tenant-sharding

ChromaDB 0.6 Multi-Tenant Isolation and HNSW Filter Optimization

Enforces tenant-scoped collection partitioning and HNSW index calibration in ChromaDB 0.6+. Eliminates cross-tenant data leakage risks and prevents catastrophic vector recall collapse caused by global soft-filtering over monolithic collections.

Prohibited Anti-Pattern: Global Multi-Tenant Monolith with Post-Query Metadata Filtering

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: ChromaDB 0.6 Multi-Tenant Isolation and HNSW Filter Optimization - Enforces tenant-scoped collection partitioning and HNSW index calibration in ChromaDB 0.6+. Eliminates cross-tenant data leakage risks and prevents catastrophic vector recall collapse caused by global soft-filtering over monolithic collections.
globs: src/**/*.{ts,tsx}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/chromadb-v06-hnsw-filter-tenant-sharding"
---

# ChromaDB 0.6 Multi-Tenant Isolation and HNSW Filter Optimization

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Global Multi-Tenant Monolith with Post-Query Metadata Filtering

## System Constraints
You are a Principal Backend and Data Architect enforcing strict ChromaDB 0.6+ vector database invariants. You must prevent global shared-collection multi-tenancy with runtime metadata filtering, which degrades HNSW graph traversal and risks data leakage. Enforce physical collection or database-level tenant isolation, explicit HNSW hyperparameter configuration, and strictly bounded boolean metadata filters.

## Strict Guidelines
- Engineers MUST ALWAYS enforce tenant isolation at the ChromaDB tenant/database or distinct collection level rather than relying on runtime `where: { tenant_id: ... }` metadata filters.
- Engineers MUST NOT execute metadata filters using unbounded negative operators (`$ne`, `$nin`) over HNSW collections as they trigger high-latency linear scans and degrade graph traversal recall.
- Code MUST explicitly specify HNSW index configuration parameters (`hnsw:space`, `hnsw:construction_ef`, `hnsw:M`, and `hnsw:search_ef`) upon collection creation.
- Services MUST dynamically scale `hnsw:search_ef` proportionally to query filter selectivity when applying restrictive metadata constraints to prevent approximate nearest neighbor recall collapse.
- Components NEVER execute vector similarity searches without runtime schema validation of the metadata filter object using an invariant parser (e.g., Zod).

## Bad vs Good Implementations
### Tenant Isolation and HNSW Configuration in Vector Collection Queries

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Shared multi-tenant collections rely on soft metadata filtering at query time. For sparse tenant subsets, the HNSW graph traversal fails to find valid entry nodes, resulting in low recall or fallbacks to slow brute-force scans. Additionally, any missing or malformed where clause completely leaks other tenants' embeddings.
```typescript
import { ChromaClient } from 'chromadb';

const client = new ChromaClient();

// Anti-pattern: Monolithic collection with tenant ID soft-filtering and uncalibrated HNSW
export async function searchTenantEmbeddings(
  tenantId: string,
  queryEmbedding: number[],
  docType: string
) {
  const collection = await client.getOrCreateCollection({
    name: 'global_enterprise_documents',
  });

  // Catastrophic: Runtime metadata filtering over a shared multi-tenant graph causes
  // severe recall drop, high latency, and high blast radius for data contamination.
  return await collection.query({
    queryEmbeddings: [queryEmbedding],
    nResults: 10,
    where: {
      $and: [
        { tenant_id: { $eq: tenantId } },
        { type: { $eq: docType } },
      ],
    },
  });
}
```

#### Production Standard (Correct)
> **Standard**: Enforces deterministic per-tenant collection namespacing to isolate vector index structures physically. Calibrates HNSW hyperparameters explicitly (`hnsw:space`, `hnsw:M`, `hnsw:search_ef`) to sustain graph traversal recall, and strictly validates metadata query payloads with Zod.
```typescript
import { ChromaClient, IncludeEnum } from 'chromadb';
import { z } from 'zod';

const client = new ChromaClient();

const FilterSchema = z.object({
  type: z.enum(['report', 'invoice', 'memo']),
  status: z.literal('active'),
});

export async function searchTenantEmbeddings(
  tenantId: string,
  queryEmbedding: number[],
  rawFilter: z.infer<typeof FilterSchema>
) {
  const validatedFilter = FilterSchema.parse(rawFilter);
  
  // Production Invariant: Physical collection isolation per tenant
  // Sanitized collection naming prevents cross-tenant pollution
  const safeTenant = tenantId.replace(/[^a-zA-Z0-9_-]/g, '');
  const collectionName = `tenant_${safeTenant}_documents`;

  const collection = await client.getOrCreateCollection({
    name: collectionName,
    metadata: {
      'hnsw:space': 'cosine',
      'hnsw:construction_ef': 128,
      'hnsw:M': 16,
      'hnsw:search_ef': 64, // Explicit search budget calibrated for filtered traversal
    },
  });

  return await collection.query({
    queryEmbeddings: [queryEmbedding],
    nResults: 10,
    where: {
      $and: [
        { type: { $eq: validatedFilter.type } },
        { status: { $eq: validatedFilter.status } },
      ],
    },
    include: [IncludeEnum.Documents, IncludeEnum.Metadatas, IncludeEnum.Distances],
  });
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: ChromaDB 0.6 Multi-Tenant Isolation and HNSW Filter Optimization

ChromaDB 0.6 Multi-Tenant Isolation and HNSW Filter Optimization defines an industrial-grade behavioral standard for backend engineering. Enforces tenant-scoped collection partitioning and HNSW index calibration in ChromaDB 0.6+. Eliminates cross-tenant data leakage risks and prevents catastrophic vector recall collapse caused by global soft-filtering over monolithic collections. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Global Multi-Tenant Monolith with Post-Query Metadata Filtering Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftGlobal Multi-Tenant Monolith with Post-Query Metadata FilteringCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS enforce tenant isolation at the ChromaDB tenant/database or distinct collection level rather than relying on runtime `where

Engineers MUST ALWAYS enforce tenant isolation at the ChromaDB tenant/database or distinct collection level rather than relying on runtime `where: { tenant_id: ... }` metadata filters.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT execute metadata filters using unbounded negative operators (`$ne`, `$nin`) over HNSW collections as they trigger high-latency linear scans and degrade graph traversal recall.

Engineers MUST NOT execute metadata filters using unbounded negative operators (`$ne`, `$nin`) over HNSW collections as they trigger high-latency linear scans and degrade graph traversal recall.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Code MUST explicitly specify HNSW index configuration parameters (`hnsw

Code MUST explicitly specify HNSW index configuration parameters (`hnsw:space`, `hnsw:construction_ef`, `hnsw:M`, and `hnsw:search_ef`) upon collection creation.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Services MUST dynamically scale `hnsw

Services MUST dynamically scale `hnsw:search_ef` proportionally to query filter selectivity when applying restrictive metadata constraints to prevent approximate nearest neighbor recall collapse.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Components NEVER execute vector similarity searches without runtime schema validation of the metadata filter object using an invariant parser (e.g., Zod).

Components NEVER execute vector similarity searches without runtime schema validation of the metadata filter object using an invariant parser (e.g., Zod).

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is ChromaDB 0.6 Multi-Tenant Isolation and HNSW Filter Optimization critical for AI coding assistants?

Enforces tenant-scoped collection partitioning and HNSW index calibration in ChromaDB 0.6+. Eliminates cross-tenant data leakage risks and prevents catastrophic vector recall collapse caused by global soft-filtering over monolithic collections. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Global Multi-Tenant Monolith with Post-Query Metadata Filtering) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

ChromaDB