This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: rag-semantic-chunking-reranking

Enterprise RAG Pipeline: Semantic Chunking & Cross-Encoder Reranking

Production architecture for Retrieval-Augmented Generation (RAG). Replaces arbitrary character splitting with semantic Markdown header hierarchy chunking, hybrid vector + BM25 keyword search with Reciprocal Rank Fusion (RRF), and Cross-Encoder reranking (Cohere / BGE) to eliminate LLM hallucinations.

Prohibited Anti-Pattern: AI uses fixed chunk_size=500 character splitting cutting code blocks and sentences in half, passes raw top-k vector similarity scores, and prompts LLMs with fragmented noise.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Enterprise RAG Pipeline: Semantic Chunking & Cross-Encoder Reranking - Production architecture for Retrieval-Augmented Generation (RAG). Replaces arbitrary character splitting with semantic Markdown header hierarchy chunking, hybrid vector + BM25 keyword search with Reciprocal Rank Fusion (RRF), and Cross-Encoder reranking (Cohere / BGE) to eliminate LLM hallucinations.
globs: **/rag/**/*.py, **/retriever/**/*.py, **/pipeline/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/rag-semantic-chunking-reranking"
---

# Enterprise RAG Pipeline: Semantic Chunking & Cross-Encoder Reranking

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: AI uses fixed chunk_size=500 character splitting cutting code blocks and sentences in half, passes raw top-k vector similarity scores, and prompts LLMs with fragmented noise.

## System Constraints
You are an Enterprise RAG and Information Retrieval Specialist. When designing RAG retrieval pipelines, NEVER use fixed character-based chunking that breaks syntactic units. Always implement semantic/structural chunking, hybrid dense + sparse retrieval, and mandatory Cross-Encoder reranking before feeding context into LLMs.

## Strict Guidelines
- Use structural/markdown-aware splitters (e.g. MarkdownHeaderTextSplitter) preserving code blocks and complete table rows intact.
- Combine dense vector embeddings (e.g. text-embedding-3-large) with sparse lexical search (BM25) via Reciprocal Rank Fusion (RRF).
- Always apply a Cross-Encoder reranker (Cohere Rerank v3 or BGE-Reranker-v2-m3) on the top 25 candidate chunks, filtering down to top 5 high-relevance chunks.
- Prepend parent document breadcrumbs and metadata (title, section, last_updated) into the chunk content to eliminate ambiguous pronoun references.
- Enforce relevance score thresholds (e.g. rerank_score >= 0.70); if no chunks meet threshold, explicitly trigger fallback 'insufficient context' handling.

## Bad vs Good Implementations
### Production RAG Retrieval with Semantic Chunking and Cross-Encoder Reranking

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Fixed character splitting slices sentences and code blocks mid-statement. Pure vector similarity searches frequently retrieve semantically related but factually irrelevant chunks, causing hallucination in generated answers.
```python
# FATAL: Fixed character splitting with raw top-k vector lookup
from langchain_text_splitters import CharacterTextSplitter

# Broken: splits sentences and code blocks in half
splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
chunks = splitter.split_text(raw_document)

# Raw vector lookup without reranker - top matches are frequently irrelevant semantic duplicates
results = vector_store.similarity_search(query, k=5)
prompt = f"Context: {results}\nQuestion: {query}"
```

#### Production Standard (Correct)
> **Standard**: Preserves structural context using markdown header hierarchies, retrieves candidate breadth via hybrid search, and scores deep relevance using a Cross-Encoder to guarantee only factually pertinent chunks reach the LLM.
```python
from langchain_text_splitters import MarkdownHeaderTextSplitter, RecursiveCharacterTextSplitter
from sentence_transformers import CrossEncoder

# 1. Structural Markdown Hierarchy Splitting
headers_to_split_on = [("#", "h1"), ("##", "h2"), ("###", "h3")]
markdown_splitter = MarkdownHeaderTextSplitter(headers_to_split_on=headers_to_split_on)
header_splits = markdown_splitter.split_text(raw_document)

text_splitter = RecursiveCharacterTextSplitter(chunk_size=1200, chunk_overlap=150)
chunks = text_splitter.split_documents(header_splits)

# 2. Hybrid Retrieval: Retrieve Top-20 candidates from Vector + BM25
candidates = hybrid_retriever.get_relevant_documents(query, limit=20)

# 3. Cross-Encoder Second-Stage Reranker
reranker = CrossEncoder("BAAI/bge-reranker-large")
pairs = [[query, doc.page_content] for doc in candidates]
scores = reranker.predict(pairs)

# Rank and filter strictly by confidence threshold
ranked = sorted(zip(candidates, scores), key=lambda x: x[1], reverse=True)
filtered_context = [doc.page_content for doc, score in ranked[:5] if score > 0.65]
```

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: Enterprise RAG Pipeline: Semantic Chunking & Cross-Encoder Reranking

Enterprise RAG Pipeline: Semantic Chunking & Cross-Encoder Reranking defines an industrial-grade behavioral standard for backend engineering. Production architecture for Retrieval-Augmented Generation (RAG). Replaces arbitrary character splitting with semantic Markdown header hierarchy chunking, hybrid vector + BM25 keyword search with Reciprocal Rank Fusion (RRF), and Cross-Encoder reranking (Cohere / BGE) to eliminate LLM hallucinations. 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: AI uses fixed chunk_size=500 character splitting cutting code blocks and sentences in half, passes raw top-k vector similarity scores, and prompts LLMs with fragmented noise. 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 DriftAI uses fixed chunk_size=500 character splitting cutting code blocks and sentences in half, passes raw top-k vector similarity scores, and prompts LLMs with fragmented noise.Critical
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

Use structural/markdown-aware splitters (e.g. MarkdownHeaderTextSplitter) preserving code blocks and complete table rows intact.

Use structural/markdown-aware splitters (e.g. MarkdownHeaderTextSplitter) preserving code blocks and complete table rows intact.

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

Combine dense vector embeddings (e.g. text-embedding-3-large) with sparse lexical search (BM25) via Reciprocal Rank Fusion (RRF).

Combine dense vector embeddings (e.g. text-embedding-3-large) with sparse lexical search (BM25) via Reciprocal Rank Fusion (RRF).

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

Always apply a Cross-Encoder reranker (Cohere Rerank v3 or BGE-Reranker-v2-m3) on the top 25 candidate chunks, filtering down to top 5 high-relevance chunks.

Always apply a Cross-Encoder reranker (Cohere Rerank v3 or BGE-Reranker-v2-m3) on the top 25 candidate chunks, filtering down to top 5 high-relevance chunks.

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

Prepend parent document breadcrumbs and metadata (title, section, last_updated) into the chunk content to eliminate ambiguous pronoun references.

Prepend parent document breadcrumbs and metadata (title, section, last_updated) into the chunk content to eliminate ambiguous pronoun references.

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

Enforce relevance score thresholds (e.g. rerank_score >= 0.70)

Enforce relevance score thresholds (e.g. rerank_score >= 0.70); if no chunks meet threshold, explicitly trigger fallback 'insufficient context' handling.

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 Enterprise RAG Pipeline: Semantic Chunking & Cross-Encoder Reranking critical for AI coding assistants?

Production architecture for Retrieval-Augmented Generation (RAG). Replaces arbitrary character splitting with semantic Markdown header hierarchy chunking, hybrid vector + BM25 keyword search with Reciprocal Rank Fusion (RRF), and Cross-Encoder reranking (Cohere / BGE) to eliminate LLM hallucinations. 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 (AI uses fixed chunk_size=500 character splitting cutting code blocks and sentences in half, passes raw top-k vector similarity scores, and prompts LLMs with fragmented noise.) 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

Enterprise