agentleFS
Goodnight77Goodnight77/rag-skills/skills/retrieval-strategies/context-enrichment-window/SKILL.md

context-enrichment-window

Skill8 starsChanged 7 months agoMITView on GitHub

Add surrounding chunks to retrieved results to provide more coherent and complete context.

The file

We split this skill into the rules it gives an agent

9 rules. About 0 are common, 9 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈9

    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:

    Add index, start position, and end position as metadata fields for each chunk in your vector database.

    line 46
  2. Distinctive:

    When concatenating chunks, account for overlap by including each character only once.

    line 60
  3. Distinctive:

    Why: Not all chunks need the same context size.

    line 65
  4. Distinctive:

    Use larger windows for shorter chunks and smaller windows for longer chunks.

    line 67
  5. Distinctive:

    When documents have natural flow or story structure

    line 78
  6. Distinctive:

    When retrieved chunks are frequently mid-sentence or mid-thought

    line 80
  7. Distinctive:

    When using extremely large windows that negate chunking benefits

    line 85
  8. Distinctive:

    When chunk overlap is already very large (50%+)

    line 87
  9. Distinctive:

    Coverage: No significant information lost in chunk boundaries

    line 98

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

Context enrichment window expands retrieved chunks by including neighboring text from the original document. This provides more coherent and contextually rich results, mitigating the tendency of vector search to return isolated text fragments.

Problem Statement

Vector-based retrieval often returns isolated fragments:

  • Mid-Sentence Cuts: Chunks may start or end in the middle of thoughts
  • Missing Context: Relevant information may be in adjacent chunks
  • Incoherence: Isolated fragments lack narrative flow
  • Incomplete Information: Critical context for understanding may be missing
  • Fragmented Answers: LLMs struggle to generate coherent responses from disjointed pieces

Key Concepts

  • Surrounding Context: Text immediately before and after a retrieved chunk
  • Window Size: Number of chunks to include on each side
  • Chunk Indexing: Maintaining original document order and positions
  • Context Expansion: Appending surrounding chunks to retrieved results
  • Overlap Handling: Managing overlapping regions between consecutive chunks

Implementation Guide

Step 1: Implement Chunk Indexing

Store chunk indices and document positions during the indexing phase.

Why: To retrieve surrounding chunks, you need to know where each chunk sits in the original document. Indexing maintains this positional information.

Assign a sequential index to each chunk during the chunking process and store the index as metadata.

Step 2: Store Chunks with Positional Metadata

Include chunk indices and position information in the vector database.

Why: Vector databases need to store positional metadata alongside content to enable context enrichment after retrieval.

Add index, start position, and end position as metadata fields for each chunk in your vector database.

Step 3: Implement Context Window Retrieval

For each retrieved chunk, fetch surrounding chunks by index.

Why: Adding surrounding context provides the narrative flow and information needed to understand the retrieved fragment properly.

Retrieve chunks at indices ±N around each retrieved chunk, where N is the window size.

Step 4: Implement Overlap Handling

Properly combine overlapping chunks without duplication.

Why: Chunk overlap means surrounding chunks share text with the target chunk. Proper combination avoids duplication and maintains coherence.

When concatenating chunks, account for overlap by including each character only once.

Step 5: Implement Adaptive Window Sizing

Adjust window size based on chunk characteristics.

Why: Not all chunks need the same context size. Adaptive sizing balances completeness with efficiency.

Use larger windows for shorter chunks and smaller windows for longer chunks.

Step 6: Build Complete Context Enrichment Pipeline

Combine all components into a cohesive retrieval system.

Why: A complete pipeline with configurable parameters allows for optimization and experimentation with different window strategies.

For implementation patterns, see LlamaIndex SentenceWindowNodeParser, LangChain ContextualCompressionRetriever, and the RecursiveCharacterTextSplitter for chunking strategies.

When to Use This Skill

  • Building RAG systems where narrative coherence matters
  • When documents have natural flow or story structure
  • For Q&A systems that need complete context for answers
  • When retrieved chunks are frequently mid-sentence or mid-thought
  • For domains where understanding depends on surrounding information

When NOT to Use This Skill

  • For collections where chunks are already self-contained (FAQ entries)
  • When using extremely large windows that negate chunking benefits
  • For systems where retrieval must be as fast as possible (enrichment adds overhead)
  • When chunk overlap is already very large (50%+)
  • For keyword-only retrieval where position doesn't matter

Related Skills

  • Semantic Chunking - Better initial chunking
  • Hierarchical Chunking - Structure-based approach
  • Sliding Window Chunking - Overlap-based chunking

Metrics & Success Criteria

  • Context Coherence: Retrieved content represents complete, self-contained information
  • Answer Quality: Improved coherence in generated responses
  • Coverage: No significant information lost in chunk boundaries
  • Token Efficiency: Enriched chunks balanced between too small and too large
  • Latency: Acceptable overhead for enrichment (typically < 100ms)

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.