agentleFS
HaiTrieu0902HaiTrieu0902/agent-skills-kit/skills/rag-patterns/SKILL.md

rag-patterns

Skill0 starsChanged 8 months agoMITView on GitHub

Retrieval-Augmented Generation design — when to use it, chunking, embeddings, hybrid search, reranking, grounding, and evaluation. Use when building Q&A over documents, knowledge assistants, or any LLM feature grounded in private data.

The file

We split this skill into the rules it gives an agent

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

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    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:

    Keep metadata (source, section, date) on every chunk — needed for filtering and citations.

    line 36
  2. Distinctive:

    Keep the most relevant chunks first.

    line 55

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 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

RAG Patterns

Ground model answers in your data instead of its memory.

Overview

RAG retrieves relevant context at query time and feeds it to the model, so answers reflect your corpus and can cite sources. The hard part is retrieval quality — the model can only be as good as what you give it.

Do You Even Need RAG?

NeedApproach
Answer over a private/changing corpusRAG
Whole corpus fits in the context window and is small/stableLong-context (skip retrieval)
Change behavior/format/tone, not knowledgeFine-tune / prompt, not RAG
Real-time facts (prices, status)Tool/API call, not RAG

The Pipeline

ingest → chunk → embed → store → retrieve → (rerank) → assemble context → generate → cite

Chunking

  • Split on semantic boundaries (headings, paragraphs), not fixed byte counts.
  • Typical chunk: a few hundred tokens with small overlap; tune to your content.
  • Keep metadata (source, section, date) on every chunk — needed for filtering and citations.

Retrieval

TechniqueUse for
Vector (semantic) searchMeaning/paraphrase matches
Keyword (BM25) searchExact terms, codes, names, acronyms
Hybrid (vector + keyword)Default — combines both, usually best recall
Reranking (cross-encoder)Reorder top-N for precision before sending to the model
Metadata filtersScope by tenant, date, permission — apply before/with search

Improve recall with query rewriting (expand/clarify the user question) and multi-query retrieval when questions are short or ambiguous.

Generation & Grounding

  • Instruct the model to answer only from the provided context and to say so when the context is insufficient.
  • Require citations back to chunk metadata so answers are verifiable.
  • Keep the most relevant chunks first; don't overstuff context (recall ≠ relevance).

Evaluation

LayerQuestionMetric
RetrievalDid we fetch the right chunks?recall@k, hit rate
GenerationIs the answer supported by context?faithfulness / groundedness
End-to-endIs the answer correct & useful?answer relevance (often LLM-as-judge)

(Use the llm-evaluation skill for the harness.)

Failure Modes

SymptomLikely cause
Right docs exist but not retrievedBad chunking / vector-only search → add keyword/hybrid + rerank
Retrieves docs but answer is wrongContext overstuffed or not instructed to ground → trim, demand citations
Confident wrong answersNo "insufficient context" path → add an explicit out
Stale answersRe-embedding not triggered on source change → wire ingestion to updates

Anti-Patterns

❌✅
Fixed-size byte chunksSemantic chunks + metadata
Vector search onlyHybrid + reranking
No citationsCite chunk sources
No retrieval evalsMeasure recall before blaming the model

Most RAG quality problems are retrieval problems. Fix retrieval first.

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.