llm-rag-systems
Skill1 starsChanged 2 months agoMITView on GitHub
Build and debug retrieval-augmented generation - chunking that preserves meaning, embedding and dimensionality tradeoffs, hybrid BM25 plus vector search, reranking, query rewriting, citation grounding, and diagnosing whether a bad answer was a retrieval miss or a generation miss. Use when a RAG or chat-with-docs feature returns wrong, outdated, or ungrounded answers, when asked about chunk size, embeddings, vector search, pgvector, RAGAS, "it says the doc has it but the bot doesn't", or "the answer is hallucinated".
In this repository126 other files it gives its agents.
- eu-data-backend-job-hunterSkill
- job-application-matcherSkill
- llm-agents-and-tool-useSkill
- llm-application-architectureSkill
- llm-cost-and-latency-optimizationSkill
- llm-evaluationSkill
- llm-fine-tuning-and-adaptersSkill
- llm-privacy-and-safetySkill
- llm-prompt-engineeringSkill
- structured-output-and-jsonSkill
- graphql-api-designSkill
- pagination-and-filteringSkill
- rest-api-designSkill
- webhooks-and-integrationsSkill
- caching-architecture-multi-layerSkill
- resilience-patternsSkill
- service-boundaries-and-monolithsSkill
- system-design-interviewSkill
- go-concurrencySkill
- go-error-handlingSkill
- java-spring-patternsSkill
- nodejs-server-performanceSkill
- python-async-patternsSkill
- python-data-modelingSkill
- python-packaging-and-toolingSkill
- python-performanceSkill
- rest-api-implementationSkill
- rust-error-handlingSkill
- rust-ownership-patternsSkill
- typescript-type-safetySkill
- ci-caching-and-speedupSkill
- ci-pipeline-designSkill
- deployment-strategiesSkill
- github-actions-hardeningSkill
- release-versioning-and-changelogsSkill
- reproducible-buildsSkill
- aws-iam-and-securitySkill
- aws-serverless-architectureSkill
- azure-fundamentalsSkill
- cloud-cost-optimizationSkill
- cloud-networking-and-vpcSkill
- gcp-cloud-architectureSkill
- code-review-skillsSkill
- design-review-and-refactoring-architectureSkill
- refactoring-techniqueSkill
- understanding-unknown-codebaseSkill
- buildkit-and-cacheSkill
- container-image-debuggingSkill
- container-runtime-securitySkill
- docker-compose-workflowsSkill
- dockerfile-authoringSkill
- docker-image-optimizationSkill
- database-connection-managementSkill
- database-transactionsSkill
- distributed-databasesSkill
- nosql-data-modelingSkill
- postgres-query-tuningSkill
- redis-and-cachingSkill
- schema-migrationsSkill
- data-modeling-for-analyticsSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
11 rules. About 0 are common, 8 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈8
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 17
Run this before changing anything.
- Very specific: line 28
Read references/retrieval-failure-taxonomy.md for the full failure-mode taxonomy with symptoms and fixes.
- Distinctive: line 44
Do not skip step 1 or step 5.
- Distinctive: line 53
Default recommendation, in order:
- Very specific: line 58
Recursive character splitting with a generous budget: Use a real separator ladder (\n\n, \n, .
- Very specific: line 73
Prepend context to every chunk: before embedding. For a support corpus, embedding "Acme Cloud — Billing FAQ\n\nTo reset your card..." beats …
- Distinctive: line 138
Decompose: only for genuinely multi-hop questions, and only when you can fan the sub-queries out in parallel.
- Distinctive: line 162
Include a closing instruction: after the context: "If the answer is not in the context, say so and name what is missing."
- Distinctive: line 170
Every claim in the answer maps to a specific retrieved chunk, cited by id.
- Distinctive: line 201
Format/schema validity: if the output is structured.
- Distinctive: line 232
Metadata filtering can destroy ANN recall: if it happens post-search, and can be very slow if it happens pre-search without an index.
≈ “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
LLM RAG Systems
RAG is a retrieval system with a language model bolted on at the end. Almost every "the AI got it wrong" bug is in the retrieval half, and you cannot find out which half failed without measuring the two halves separately.
Triage first: retrieval miss or generation miss?
Run this before changing anything. Take 20 questions where you already know the right answer, and check whether the gold document was in the top-k.
- Not in top-k → retrieval problem. Chunking, embeddings, query, filters, or reranking. No amount of prompt work fixes this.
- In top-k, answer still wrong → generation problem. The context was present and the model ignored it, was drowned out, or the question was ambiguous. Now prompt, context ordering, and citation forcing apply.
- Answer is right but ungrounded → the model answered from parametric memory. Force citation and check that every claim maps to a retrieved span.
Read references/retrieval-failure-taxonomy.md for the full failure-mode
taxonomy with symptoms and fixes. It is the core diagnostic table; come back to
it whenever quality is wrong rather than merely mediocre.
Pipeline
question
-> query transformation (rewrite / decompose / expand with history)
-> filters (tenant, ACL, date)
-> hybrid retrieval: BM25 + dense vector, merged
-> (optional) reranker over top-N
-> context assembly: dedupe, reorder, budget to tokens
-> generation with citations and an abstain pathDo not skip step 1 or step 5. The two most common RAG failures are a bad query and a bad context window.
Chunking
Fixed-size chunks with overlap are the default because they are trivial to implement, not because they are good. They cut mid-sentence, mid-table, and mid-argument, and an embedding of half a paragraph is close to meaningless.
Default recommendation, in order:
- Structure-aware chunking. Split on real boundaries: markdown headings, HTML sections, PDF pages, code symbols, API endpoints, dialogue turns. Then apply a token budget on top. This is the house default.
- Recursive character splitting with a generous budget. Use a real
separator ladder (
\n\n,\n,., space) rather than a hard cut. ~500-800 tokens with ~10-15% overlap. - Sentence/paragraph packing with a parent-child index. Embed small units for precision, return the larger parent section so the model has the full argument.
Rules:
- Chunk size is a precision/recall dial. Small chunks retrieve precisely and lose context; large chunks carry context and dilute the embedding. Tune it against a retrieval metric, not a vibe.
- Overlap is insurance against boundary cuts, not a substitute for good boundaries. 10-15% is plenty; heavy overlap inflates index size and duplicates results in context.
- Prepend context to every chunk before embedding. For a support corpus,
embedding
"Acme Cloud — Billing FAQ\n\nTo reset your card..."beats embedding the paragraph alone, because short chunks have almost no signal on their own. Metadata headers are cheap and reliably improve recall. - One table per chunk. A chunk containing a table plus surrounding prose retrieves badly and confuses the model. Convert the table to text and describe it in the header.
- Store the chunk's source coordinates (doc id, heading path, page/line) at index time. You cannot produce citations you never recorded, and you cannot re-chunk later without a reindex.
- Never re-chunk casually. It invalidates the whole index. Re-chunking is a migration with a rebuild, an eval run, and a cutover.
Embeddings and dimensionality
- Match embed model to query language and domain, and re-embed the whole corpus when you change it. Mixing embedding models in one index gives you nonsense comparisons.
- Dimensionality is a storage/latency knob, not a quality knob in itself. Smaller embeddings cut index size and search cost; quality is decided by the model. Compare quality at a couple of sizes rather than assuming smaller is worse.
- Normalise when using cosine/IP inner-product indexes. Mixing a normalised vector with a distance metric silently degrades results.
- MTEB is a starting point, not an answer. Embedding leaderboards transfer badly to your corpus. Run a 100-query internal relevance check: embed each query, retrieve, and have a human or judge mark relevant/not. That 100-query set is your real benchmark and it will contradict the leaderboard more often than you expect.
- Document-side vs query-side prefixes: some embedding families expect an instruction on the query only. Using them backwards degrades quality.
Hybrid search and reranking
Dense vectors are semantic; they are weak at exact tokens — part numbers, error
codes, ERR_AUTH_401, surnames, negations, versions. BM25 is the opposite.
Real queries contain both, so run both and merge.
- BM25: any keyword engine (Postgres full-text, Elasticsearch, OpenSearch,
SQLite FTS). Tunable
k1andb; defaults are fine. Merge with Reciprocal Rank Fusion (RRF) rather than normalising scores and adding them — RRF is rank-based, so it does not need score calibration between two incomparable systems:
rrf(d) = sum over rankers of 1 / (k + rank_r(d)) # k ~ 60- Rerank the top ~50-200 with a cross-encoder (a model that reads the query and the passage together). This is the single highest-leverage accuracy win in most RAG systems and the most commonly skipped. Rerank cost is per document, so it applies to a shortlist, not the corpus.
- Cut the shortlist to what fits the context budget. Retrieving 50 chunks and passing 50 chunks to the model is worse than retrieving 50 and passing 6: recall@50 and precision@6 are different numbers, and the model is scored on the second.
Query transformation
Users ask follow-ups. "and what about the EU one?" embeds nothing useful.
- Condense a question with conversation history into a standalone query before retrieval.
- Rewrite vague questions into concrete ones ("the latest" →
2026 policy, and add a date filter). - Decompose only for genuinely multi-hop questions, and only when you can fan the sub-queries out in parallel. Sequential decomposition multiplies latency and is usually worse than a single well-formed query.
- Multi-query / HyDE (generate a hypothetical answer, embed that) help when recall is the bottleneck. They cost one extra generation per query — measure before adopting.
- Route first: a classifier that decides "chitchat / this needs the docs / this needs the database" saves a retrieval call on most traffic.
Context assembly
This is where a correct retrieval gets thrown away.
- Budget to tokens, not to chunk count. Model context is finite; leave room for the answer and the instructions.
- Dedupe by document and near-duplicate text. BM25 and dense retrieve the same paragraph twice constantly.
- Put the strongest chunk where you said you would. Models weight the beginning and the end of context most; the middle gets the least attention ("lost in the middle"). If you are cutting, cut the middle, not the end.
- Label every chunk with its source and instruct the model to cite. This is what makes hallucination detectable instead of merely suspected.
- Interleave, don't concatenate by source, when you have several documents, so one verbose document does not own the window.
- Include a closing instruction after the context: "If the answer is not in the context, say so and name what is missing."
Citation grounding
Citations are the cheapest hallucination detector you have, and users will check them. Requirements:
- Every claim in the answer maps to a specific retrieved chunk, cited by id.
- The cited chunk actually supports the claim — verify this, do not assume it. Models cite correctly-formatted but irrelevant chunks.
- Post-hoc: strip or annotate unsupported sentences, or refuse the answer if any sentence is unsupported. This is a cheap deterministic filter on top of a generative model, and it works.
- Show the source text in the UI. Citations the user cannot open are decoration.
Evaluating a RAG system
Measure retrieval and generation separately. RAG-as-a-whole metrics hide which half broke.
Retrieval metrics (deterministic, cheap, and the ones to optimise first):
- Recall@k against a labelled gold doc/chunk per question.
- MRR / nDCG@k for ranked quality — recall@5 is insensitive to whether the gold chunk was 1st or 5th, which matters a lot to the final answer.
- Context precision: what fraction of retrieved chunks were useful.
- Build the labelled set from real questions first (log mining), then add synthetic ones for coverage. 100-200 labelled questions is enough to steer.
Generation metrics:
- Groundedness: are claims supported by the given context?
- Answer correctness vs the known right answer.
- Citation accuracy and completeness — a metric most teams skip and users notice.
- Abstention accuracy: does it refuse when it should, and answer when it should? Measure both; a system that always refuses scores 0 on usefulness.
- Format/schema validity if the output is structured.
Deterministic checks first: exact match / F1 for short factual answers,
regex and schema validation, citation-id existence. Model-graded second
(skills/ai/llm-evaluation/SKILL.md). Judge-based metrics need calibration
against human labels or they are just vibes with extra steps.
Operational reality
- The index is a derived artefact. Source of truth is the source documents. Rebuild must be a one-command, idempotent, incremental job with a full-rebuild path; test it.
- Freshness is a product decision. Embedding on write, a periodic incremental sync, or a nightly full rebuild. For anything users edit, on-write is the only one they will trust.
- Deleting a document must delete its chunks. Orphaned chunks in the index are how a "we deleted that doc" ticket becomes a security incident.
- Access control belongs in retrieval. Filter by tenant/ACL before the model sees anything, in the query itself. Post-filtering leaks: the top-k may contain rows the user cannot read and the model will quote them.
- Vector search is a candidate generator, not a database. Postgres is fine up to a few million vectors with a tuned index; past that, a dedicated engine. Benchmark your own filter selectivity — HNSW/IVF behaviour depends on it.
Gotchas
- Similarity scores are not probabilities and are not comparable across queries. Never threshold on a raw score without calibrating on your data.
- Your embedding model has a max input length. Long chunks get truncated silently, and the tail is the part that answered the question.
- Metadata filtering can destroy ANN recall if it happens post-search, and can be very slow if it happens pre-search without an index. Test with your real filter cardinality.
- Hybrid needs both sides. Adding BM25 to a dense-only index and assuming improvement is how you end up with slower queries and no gain.
- A reranker is a model in your latency path. Measure it end to end.
- Chunk-level dedupe by hash, not by embedding similarity, if you need it to be exact and cheap.
- Don't eval on questions you wrote from reading the docs. They are easier and more lexical than real user phrasing.
Checklist
- 100+ labelled questions with a gold document/chunk
- Recall@k and MRR measured before any optimisation
- Chunking follows document structure; source coordinates stored
- Hybrid retrieval with RRF merge, reranker over the shortlist
- Context deduped, ordered, and budgeted to tokens
- Citations required and verified; abstain path tested
- ACL filtering applied inside the retrieval query
- Reindex path is idempotent and tested
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.

