contextual-chunk-headers
Skill8 starsChanged 7 months agoMITView on GitHub
Add higher-level context to chunks by prepending headers before embedding for better retrieval.
In this repository39 other files it gives its agents.
- AGENTS.mdAGENTS.md
- rag-skillsSkill
- choosing-a-chunking-frameworkSkill
- hierarchical-chunkingSkill
- semantic-chunkingSkill
- chunkingSkill
- sliding-window-chunkingSkill
- rag-for-code-documentationSkill
- rag-for-multimodal-contentSkill
- data-type-handlingSkill
- index-versioning-and-reindexingSkill
- production-rag-deploymentSkill
- deploymentSkill
- generation-evaluation-metricsSkill
- rag-evaluation-frameworksSkill
- retrieval-evaluation-metricsSkill
- evaluation-metricsSkill
- optimize-retrieval-latencySkill
- performance-optimizationSkill
- agentic-rag-routingSkill
- multi-agent-rag-orchestrationSkill
- rag-agentsSkill
- adaptive-retrievalSkill
- context-enrichment-windowSkill
- crag-corrective-ragSkill
- explainable-retrievalSkill
- graph-ragSkill
- hybrid-search-bm25-denseSkill
- hyde-hypothetical-document-embeddingsSkill
- hype-hypothetical-prompt-embeddingsSkill
- multi-pass-retrieval-with-rerankingSkill
- query-transformation-strategiesSkill
- raptor-hierarchical-retrievalSkill
- self-ragSkill
- retrieval-strategiesSkill
- choosing-vector-db-by-datatypeSkill
- qdrant-for-production-ragSkill
- qdrant-setup-ragSkill
- vector-databasesSkill
The file
We split this skill into the rules it gives an agent
11 rules. About 0 are common, 11 are distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈11
Distinctive · estimate
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 21
Misleading Isolation: Read alone, chunks can be misleading or incomplete
- Distinctive: line 27
Document-Level Context: Document title or summary as header context
- Distinctive: line 37
Why: Document-level context provides the most important higher-level information for understanding any chunk within that document.
- Distinctive: line 39
Use an LLM to generate a descriptive document title if one doesn't exist, or extract titles from document structure.
- Distinctive: line 60
When indexing each chunk, include the header as a prefix to the chunk content.
- Distinctive: line 63
Use the header-enhanced chunks for embedding and indexing.
- Distinctive: line 74
When returning retrieved content, include the original header for context.
- Distinctive: line 85
When chunks frequently refer to their subject via pronouns or implicit references
- Distinctive: line 87
When retrieval quality suffers from decontextualized chunks
- Distinctive: line 92
When chunk headers would be redundant or too long
- Distinctive: line 94
When storage overhead is a major concern (headers increase chunk size)
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Overview
Contextual chunk headers (CCH) enhance retrieval by prepending higher-level context (document title, section headers, summaries) to each chunk before embedding. This gives embeddings a more accurate representation of content and meaning, significantly improving retrieval quality and reducing irrelevant results.
Problem Statement
Individual chunks often lack sufficient context:
- Implicit References: Chunks use pronouns and implicit references that aren't clear in isolation
- Missing Context: Chunks only make sense in the context of their section or document
- Misleading Isolation: Read alone, chunks can be misleading or incomplete
- Poor Retrieval: Queries that reference subjects or themes aren't matched effectively
- LLM Misinterpretation: LLMs struggle to understand decontextualized chunks
Key Concepts
- Chunk Header: Higher-level context prepended to chunk content
- Document-Level Context: Document title or summary as header context
- Section-Level Context: Hierarchy of section and subsection titles
- Pre-Embedding Concatenation: Combining header and chunk before embedding
- Context Preservation: Carrying headers through to retrieval and presentation
Implementation Guide
Step 1: Generate Document-Level Context
Extract or generate document titles and summaries for use in chunk headers.
Why: Document-level context provides the most important higher-level information for understanding any chunk within that document.
Use an LLM to generate a descriptive document title if one doesn't exist, or extract titles from document structure.
Step 2: Extract Section Hierarchy
Parse document structure to capture section and subsection titles.
Why: Section hierarchy provides granular context that helps retrieve information about specific topics and themes within the document.
Parse the document structure (markdown headings, HTML tags, etc.) to build a hierarchical representation of sections.
Step 3: Create Chunk Headers
Combine document and section context into comprehensive headers.
Why: Combined context provides both broad document understanding and specific section information, giving embeddings the best possible representation.
For each chunk, create a header combining document title and the section path that contains the chunk.
Step 4: Prepend Headers to Chunks
Combine headers with chunk content before embedding.
Why: Embeddings represent the combined header+chunk content, capturing both local meaning and global context in the same vector space.
When indexing each chunk, include the header as a prefix to the chunk content.
Step 5: Embed with Headers
Use the header-enhanced chunks for embedding and indexing.
Why: Embedding with headers ensures that retrieval considers both local content and broader context, leading to more relevant matches.
Index the enhanced chunks using your preferred embedding model and vector database.
Step 6: Present Results with Headers
Include headers when presenting retrieved chunks to users or LLMs.
Why: Presenting headers provides necessary context for understanding retrieved chunks, reducing misinterpretation and improving answer quality.
When returning retrieved content, include the original header for context.
Step 7: Build Complete CCH Pipeline
Combine all components into a cohesive chunking and retrieval system.
Why: A complete pipeline ensures that contextual headers are consistently applied through chunking, embedding, retrieval, and presentation.
For implementation patterns, see LangChain RecursiveCharacterTextSplitter, LlamaIndex node parser usage, Anthropic Contextual Retrieval, and dsRAG AutoContext.
When to Use This Skill
- Building RAG systems with structured documents (articles, reports, documentation)
- When chunks frequently refer to their subject via pronouns or implicit references
- For documents with clear section hierarchy or structure
- When retrieval quality suffers from decontextualized chunks
- For applications where LLMs need to understand chunk context
When NOT to Use This Skill
- For unstructured documents without clear hierarchy or titles
- When chunk headers would be redundant or too long
- For very short documents where the entire text is already coherent
- When storage overhead is a major concern (headers increase chunk size)
- For domains where document structure is meaningless (code snippets, logs)
Related Skills
- Semantic Chunking - Context-aware chunking approach
- Hierarchical Chunking - Structure-based chunking
- Context Enrichment Window - Post-retrieval context
Metrics & Success Criteria
- Retrieval Precision: Increased rate of correct retrieval for subject-based queries
- Context Completeness: Retrieved chunks provide sufficient context for understanding
- Header Effectiveness: Headers improve embedding representation quality
- Answer Quality: Reduced hallucinations from decontextualized chunks
- Storage Overhead: Acceptable increase in chunk size due to headers
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

