This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: chromadb-0-6-hnsw-metadata-filtering-tenancy

ChromaDB 0.6 HNSW Metadata Filtering and Multi-Tenant Isolation Standard

Enforces deterministic multi-tenant isolation, HNSW index parameter calibration, and structured boolean metadata filtering in ChromaDB 0.6+. Prevents catastrophic recall collapse resulting from sparse in-graph metadata filtering on monolithic vector collections and eliminates cross-tenant data leakage risks.

Prohibited Anti-Pattern: Sparse In-Graph Metadata Post-Filtering on Monolithic Collections Causing HNSW Recall Collapse and Tenant Bleed

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: ChromaDB 0.6 HNSW Metadata Filtering and Multi-Tenant Isolation Standard - Enforces deterministic multi-tenant isolation, HNSW index parameter calibration, and structured boolean metadata filtering in ChromaDB 0.6+. Prevents catastrophic recall collapse resulting from sparse in-graph metadata filtering on monolithic vector collections and eliminates cross-tenant data leakage risks.
globs: src/vector/**/*.ts, src/services/embedding/**/*.ts, src/db/chroma/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/chromadb-0-6-hnsw-metadata-filtering-tenancy"
---

# ChromaDB 0.6 HNSW Metadata Filtering and Multi-Tenant Isolation Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Sparse In-Graph Metadata Post-Filtering on Monolithic Collections Causing HNSW Recall Collapse and Tenant Bleed

## System Constraints
You are a Principal Backend and Vector Search Architect enforcing ChromaDB 0.6 production patterns. You must mandate strict tenant isolation boundaries using native database/tenant namespaces or isolated collection segments, enforce typed and indexed metadata operators ($and, $or, $eq, $in), and ensure HNSW graph parameters (search_ef, construction_ef, M) are dynamically tuned to prevent query-pruning recall collapse during filtered vector queries.

## Strict Guidelines
- Engineers MUST ALWAYS isolate distinct tenants using ChromaDB native multi-tenancy constructs (tenant and database namespaces) or dedicated per-tenant collections rather than relying solely on post-filtering queries against a shared monolithic collection.
- Engineers MUST configure explicit HNSW collection metadata during collection creation, specifically specifying 'hnsw:space' (cosine, l2, or ip), 'hnsw:construction_ef', and 'hnsw:M'.
- Engineers MUST dynamic-scale 'hnsw:search_ef' or oversample 'nResults' during filtered queries; when query filters match less than 20% of the collection's vectors, default HNSW search depth will prune valid paths and result in recall collapse (empty or suboptimal results).
- Engineers MUST NOT construct metadata filter clauses using untyped raw string interpolation or flat object keys without explicit operator expressions ($eq, $in, $and).
- Engineers MUST validate that all metadata values passed to ChromaDB are primitive types (string, number, boolean) and MUST NEVER pass nested JSON objects, arrays, or undefined fields into metadata payloads.
- Queries filtering across multiple metadata dimensions MUST ALWAYS encapsulate conditions in explicit boolean composite operators ($and, $or) matching the ChromaDB 0.6 query specification.

## Bad vs Good Implementations
### Multi-Tenant Query with Metadata Filtering and HNSW Depth Adjustment

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Querying a monolithic collection with sparse tenant metadata causes HNSW early graph pruning, returning fewer than `limit` results or empty sets (recall collapse). Furthermore, missing explicit multi-tenant namespaces creates critical isolation and performance risks in ChromaDB.
```typescript
import { ChromaClient } from 'chromadb';

const client = new ChromaClient();

export async function searchTenantDocuments(
  tenantId: string,
  queryEmbedding: number[],
  documentType: string,
  limit: number
) {
  // ANTI-PATTERN: Shared monolithic collection relying solely on metadata filtering for tenant boundaries.
  // Default HNSW search_ef will prune prematurely when tenantId is sparse, leading to 0 results.
  // Unsanitized and implicit where-clause syntax.
  const collection = await client.getOrCreateCollection({ name: 'global-knowledgebase' });

  return await collection.query({
    queryEmbeddings: [queryEmbedding],
    nResults: limit,
    where: {
      tenant_id: tenantId,
      type: documentType,
    },
  });
}
```

#### Production Standard (Correct)
> **Standard**: Uses ChromaDB 0.6 native tenant/database routing, configures HNSW parameters with adequate search_ef depth to counteract filter-induced graph pruning, and enforces strongly typed, explicit boolean filter syntax.
```typescript
import { ChromaClient, IncludeEnum, Where } from 'chromadb';

// Create tenant-isolated client instance supported in Chroma 0.6+
export function getChromaClientForTenant(tenantId: string, databaseName = 'default'): ChromaClient {
  return new ChromaClient({
    path: process.env.CHROMA_SERVER_URL ?? 'http://localhost:8000',
    tenant: tenantId,
    database: databaseName,
  });
}

export async function searchTenantDocuments(
  tenantClient: ChromaClient,
  collectionName: string,
  queryEmbedding: number[],
  documentType: string,
  limit: number
) {
  // Invariant 1: Collection configured with explicit HNSW parameters
  const collection = await tenantClient.getOrCreateCollection({
    name: collectionName,
    metadata: {
      'hnsw:space': 'cosine',
      'hnsw:construction_ef': 128,
      'hnsw:M': 16,
      'hnsw:search_ef': Math.max(100, limit * 4), // Prevent recall collapse on filtered traversal
    },
  });

  // Invariant 2: Explicit boolean operator typing ($and, $eq)
  const filterClause: Where = {
    $and: [
      { doc_type: { $eq: documentType } },
      { is_archived: { $eq: false } },
    ],
  };

  return await collection.query({
    queryEmbeddings: [queryEmbedding],
    nResults: limit,
    where: filterClause,
    include: [IncludeEnum.Metadatas, IncludeEnum.Documents, 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 HNSW Metadata Filtering and Multi-Tenant Isolation Standard

ChromaDB 0.6 HNSW Metadata Filtering and Multi-Tenant Isolation Standard defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic multi-tenant isolation, HNSW index parameter calibration, and structured boolean metadata filtering in ChromaDB 0.6+. Prevents catastrophic recall collapse resulting from sparse in-graph metadata filtering on monolithic vector collections and eliminates cross-tenant data leakage risks. 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: Sparse In-Graph Metadata Post-Filtering on Monolithic Collections Causing HNSW Recall Collapse and Tenant Bleed 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 DriftSparse In-Graph Metadata Post-Filtering on Monolithic Collections Causing HNSW Recall Collapse and Tenant BleedCritical
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 isolate distinct tenants using ChromaDB native multi-tenancy constructs (tenant and database namespaces) or dedicated per-tenant collections rather than relying solely on post-filtering queries against a shared monolithic collection.

Engineers MUST ALWAYS isolate distinct tenants using ChromaDB native multi-tenancy constructs (tenant and database namespaces) or dedicated per-tenant collections rather than relying solely on post-filtering queries against a shared monolithic collection.

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 configure explicit HNSW collection metadata during collection creation, specifically specifying 'hnsw

Engineers MUST configure explicit HNSW collection metadata during collection creation, specifically specifying 'hnsw:space' (cosine, l2, or ip), 'hnsw:construction_ef', and 'hnsw:M'.

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

Engineers MUST dynamic-scale 'hnsw

Engineers MUST dynamic-scale 'hnsw:search_ef' or oversample 'nResults' during filtered queries; when query filters match less than 20% of the collection's vectors, default HNSW search depth will prune valid paths and result in recall collapse (empty or suboptimal results).

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

Engineers MUST NOT construct metadata filter clauses using untyped raw string interpolation or flat object keys without explicit operator expressions ($eq, $in, $and).

Engineers MUST NOT construct metadata filter clauses using untyped raw string interpolation or flat object keys without explicit operator expressions ($eq, $in, $and).

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

Engineers MUST validate that all metadata values passed to ChromaDB are primitive types (string, number, boolean) and MUST NEVER pass nested JSON objects, arrays, or undefined fields into metadata payloads.

Engineers MUST validate that all metadata values passed to ChromaDB are primitive types (string, number, boolean) and MUST NEVER pass nested JSON objects, arrays, or undefined fields into metadata payloads.

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.
6

Queries filtering across multiple metadata dimensions MUST ALWAYS encapsulate conditions in explicit boolean composite operators ($and, $or) matching the ChromaDB 0.6 query specification.

Queries filtering across multiple metadata dimensions MUST ALWAYS encapsulate conditions in explicit boolean composite operators ($and, $or) matching the ChromaDB 0.6 query specification.

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 HNSW Metadata Filtering and Multi-Tenant Isolation Standard critical for AI coding assistants?

Enforces deterministic multi-tenant isolation, HNSW index parameter calibration, and structured boolean metadata filtering in ChromaDB 0.6+. Prevents catastrophic recall collapse resulting from sparse in-graph metadata filtering on monolithic vector collections and eliminates cross-tenant data leakage risks. 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 (Sparse In-Graph Metadata Post-Filtering on Monolithic Collections Causing HNSW Recall Collapse and Tenant Bleed) 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