agentleFS
Goodnight77Goodnight77/rag-skills/skills/chunking/contextual-chunk-headers/SKILL.md

contextual-chunk-headers

Skill8 starsChanged 7 months agoMITView on GitHub

Add higher-level context to chunks by prepending headers before embedding for better retrieval.

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
  1. Distinctive:

    Misleading Isolation: Read alone, chunks can be misleading or incomplete

    line 21
  2. Distinctive:

    Document-Level Context: Document title or summary as header context

    line 27
  3. Distinctive:

    Why: Document-level context provides the most important higher-level information for understanding any chunk within that document.

    line 37
  4. Distinctive:

    Use an LLM to generate a descriptive document title if one doesn't exist, or extract titles from document structure.

    line 39
  5. Distinctive:

    When indexing each chunk, include the header as a prefix to the chunk content.

    line 60
  6. Distinctive:

    Use the header-enhanced chunks for embedding and indexing.

    line 63
  7. Distinctive:

    When returning retrieved content, include the original header for context.

    line 74
  8. Distinctive:

    When chunks frequently refer to their subject via pronouns or implicit references

    line 85
  9. Distinctive:

    When retrieval quality suffers from decontextualized chunks

    line 87
  10. Distinctive:

    When chunk headers would be redundant or too long

    line 92
  11. Distinctive:

    When storage overhead is a major concern (headers increase chunk size)

    line 94

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.