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

ChromaDB 0.6+ Multi-Tenant Namespaces and HNSW Metadata Filter Optimization

ChromaDB 0.6+ mandates strict tenant and database namespace isolation at the client and collection tier instead of monolithic collection filtering. Executing high-selectivity tenant filters against an unpartitioned shared HNSW graph causes query recall collapse, filter thrashing, and potential data leakage across tenant boundaries.

Prohibited Anti-Pattern: Monolithic Shared Collection with Soft Tenant Filtering and Unindexed HNSW Traversal

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: ChromaDB 0.6+ Multi-Tenant Namespaces and HNSW Metadata Filter Optimization - ChromaDB 0.6+ mandates strict tenant and database namespace isolation at the client and collection tier instead of monolithic collection filtering. Executing high-selectivity tenant filters against an unpartitioned shared HNSW graph causes query recall collapse, filter thrashing, and potential data leakage across tenant boundaries.
globs: src/vector/**/*.{ts,js}, src/services/**/*.{ts,js}, src/lib/vector/**/*.{ts,js}, src/infrastructure/chroma/**/*.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/chromadb-hnsw-tenant-isolated-filtering"
---

# ChromaDB 0.6+ Multi-Tenant Namespaces and HNSW Metadata Filter Optimization

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Monolithic Shared Collection with Soft Tenant Filtering and Unindexed HNSW Traversal

## System Constraints
You are a Principal Engineer enforcing ChromaDB 0.6+ architectural invariants. Enforce strict tenant/database namespace isolation, explicit HNSW index hyperparameter tuning (`hnsw:space`, `hnsw:construction_ef`, `hnsw:search_ef`, `hnsw:M`), and deterministic structured metadata filter trees with `$and`, `$or`, and `$in` operators. Reject monolithic multi-tenant vector pooling and bare unstructured queries.

## Strict Guidelines
- Engineers MUST ALWAYS isolate distinct tenants using ChromaDB 0.6+ native tenant and database namespaces (`tenant` and `database` configuration on client creation or retrieval) rather than single-collection metadata tags.
- Engineers MUST NOT rely on top-level `{ tenant_id: tenantId }` metadata filters inside an unpartitioned global HNSW collection to achieve data boundary compliance.
- Code MUST explicitly define HNSW collection configuration parameters (`hnsw:space`, `hnsw:construction_ef`, `hnsw:search_ef`, `hnsw:M`) upon collection initialization to balance recall against memory and index convergence rates.
- Services MUST structure complex metadata queries using explicit boolean operators (`$and`, `$or`, `$in`, `$nin`, `$eq`, `$ne`, `$gt`, `$gte`, `$lt`, `$lte`) and validate that target fields are indexed primitive types (string, number, boolean).
- Components NEVER execute unbounded vector similarity queries; `nResults` must be strictly validated, capped, and accompanied by explicit distance threshold filtering.

## Bad vs Good Implementations
### Tenant Isolation and HNSW Query Optimization in ChromaDB 0.6+

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Creates a single shared collection across all tenants, relying on soft metadata filtering. In an HNSW index, high-selectivity filters on large unpartitioned collections cause the search graph to prune prematurely or fall back to high-latency brute force scans, while introducing significant data isolation compliance risks.
```typescript
import { ChromaClient } from 'chromadb';

const client = new ChromaClient();

// ANTI-PATTERN: Monolithic collection sharing with soft metadata tenant filtering
export async function searchDocuments(tenantId: string, queryEmbedding: number[], filterCategory: string) {
  const collection = await client.getOrCreateCollection({
    name: 'global-embeddings-collection',
  });

  // Inefficient HNSW traversal: filtering across tenants inside a massive flat graph
  // Causes severe recall collapse and risk of multi-tenant cross-talk
  return await collection.query({
    queryEmbeddings: [queryEmbedding],
    nResults: 10,
    where: {
      tenantId: tenantId,
      category: filterCategory,
    },
  });
}
```

#### Production Standard (Correct)
> **Standard**: Isolates the tenant at the client/database level using ChromaDB 0.6 native namespace primitives, isolating the physical/logical HNSW index segment. It configures explicit HNSW index parameters (`hnsw:space`, `hnsw:search_ef`) and formats query filters with valid boolean operator trees (`$and`, `$eq`).
```typescript
import { ChromaClient, DefaultEmbeddingFunction } from 'chromadb';

interface QueryOptions {
  tenantId: string;
  databaseName?: string;
  queryEmbedding: number[];
  filterCategory: string;
  limit?: number;
}

// PRODUCTION FIX: Scoped tenant/database isolation with deterministic HNSW parameters
export async function searchDocuments({
  tenantId,
  databaseName = 'default_database',
  queryEmbedding,
  filterCategory,
  limit = 10,
}: QueryOptions) {
  const boundedLimit = Math.min(Math.max(1, limit), 100);

  // ChromaDB 0.6+ native tenant and database boundaries
  const client = new ChromaClient({
    path: process.env.CHROMA_SERVER_URL || 'http://localhost:8000',
    tenant: tenantId,
    database: databaseName,
  });

  const collection = await client.getOrCreateCollection({
    name: 'knowledge-base',
    metadata: {
      'hnsw:space': 'cosine',
      'hnsw:construction_ef': 128,
      'hnsw:search_ef': 64,
      'hnsw:M': 16,
    },
    embeddingFunction: new DefaultEmbeddingFunction(),
  });

  return await collection.query({
    queryEmbeddings: [queryEmbedding],
    nResults: boundedLimit,
    where: {
      $and: [
        { category: { $eq: filterCategory } },
        { isArchived: { $eq: false } },
      ],
    },
  });
}
```

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 Namespaces and HNSW Metadata Filter Optimization

ChromaDB 0.6+ Multi-Tenant Namespaces and HNSW Metadata Filter Optimization defines an industrial-grade behavioral standard for backend engineering. ChromaDB 0.6+ mandates strict tenant and database namespace isolation at the client and collection tier instead of monolithic collection filtering. Executing high-selectivity tenant filters against an unpartitioned shared HNSW graph causes query recall collapse, filter thrashing, and potential data leakage across tenant boundaries. 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: Monolithic Shared Collection with Soft Tenant Filtering and Unindexed HNSW Traversal 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 DriftMonolithic Shared Collection with Soft Tenant Filtering and Unindexed HNSW TraversalCritical
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 0.6+ native tenant and database namespaces (`tenant` and `database` configuration on client creation or retrieval) rather than single-collection metadata tags.

Engineers MUST ALWAYS isolate distinct tenants using ChromaDB 0.6+ native tenant and database namespaces (`tenant` and `database` configuration on client creation or retrieval) rather than single-collection metadata tags.

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 rely on top-level `{ tenant_id

Engineers MUST NOT rely on top-level `{ tenant_id: tenantId }` metadata filters inside an unpartitioned global HNSW collection to achieve data boundary compliance.

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 define HNSW collection configuration parameters (`hnsw

Code MUST explicitly define HNSW collection configuration parameters (`hnsw:space`, `hnsw:construction_ef`, `hnsw:search_ef`, `hnsw:M`) upon collection initialization to balance recall against memory and index convergence rates.

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 structure complex metadata queries using explicit boolean operators (`$and`, `$or`, `$in`, `$nin`, `$eq`, `$ne`, `$gt`, `$gte`, `$lt`, `$lte`) and validate that target fields are indexed primitive types (string, number, boolean).

Services MUST structure complex metadata queries using explicit boolean operators (`$and`, `$or`, `$in`, `$nin`, `$eq`, `$ne`, `$gt`, `$gte`, `$lt`, `$lte`) and validate that target fields are indexed primitive types (string, number, boolean).

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 unbounded vector similarity queries

Components NEVER execute unbounded vector similarity queries; `nResults` must be strictly validated, capped, and accompanied by explicit distance threshold filtering.

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 Namespaces and HNSW Metadata Filter Optimization critical for AI coding assistants?

ChromaDB 0.6+ mandates strict tenant and database namespace isolation at the client and collection tier instead of monolithic collection filtering. Executing high-selectivity tenant filters against an unpartitioned shared HNSW graph causes query recall collapse, filter thrashing, and potential data leakage across tenant boundaries. 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 (Monolithic Shared Collection with Soft Tenant Filtering and Unindexed HNSW Traversal) 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