context-enrichment-window
Skill8 starsChanged 7 months agoMITView on GitHub
Add surrounding chunks to retrieved results to provide more coherent and complete context.
In this repository39 other files it gives its agents.
- AGENTS.mdAGENTS.md
- rag-skillsSkill
- choosing-a-chunking-frameworkSkill
- contextual-chunk-headersSkill
- 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
- 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
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
- Distinctive: line 46
Add index, start position, and end position as metadata fields for each chunk in your vector database.
- Distinctive: line 60
When concatenating chunks, account for overlap by including each character only once.
- Distinctive: line 65
Why: Not all chunks need the same context size.
- Distinctive: line 67
Use larger windows for shorter chunks and smaller windows for longer chunks.
- Distinctive: line 78
When documents have natural flow or story structure
- Distinctive: line 80
When retrieved chunks are frequently mid-sentence or mid-thought
- Distinctive: line 85
When using extremely large windows that negate chunking benefits
- Distinctive: line 87
When chunk overlap is already very large (50%+)
- Distinctive: line 98
Coverage: No significant information lost in chunk boundaries
≈ “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
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)
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.

