rag-and-memory
Skill216 starsChanged 6 months agoMITView on GitHub
Patterns for Retrieval-Augmented Generation (RAG) and agent memory systems. Retrieves only relevant context, prevents context bloat, and maintains coherent state across sessions.
We split this skill into the rules it gives an agent
10 rules. About 0 are common, 10 are distinctive, and 0 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈10
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 18
When agent context windows are being exceeded
- Distinctive: line 19
When agents need to remember information across sessions
- Distinctive: line 20
When building Q&A, document analysis, or knowledge base systems
- Distinctive: line 47
Re-ranking: After retrieval, re-rank by relevance using a cross-encoder.
- Distinctive: line 55
Retrieve, don't pre-load: Only load context relevant to the current query.
- Distinctive: line 63
If retrieval returns no relevant results: say so — do not hallucinate an answer.
- Distinctive: line 64
If retrieved documents are outdated: surface the document date to the user.
- Distinctive: line 65
If confidence is low: present the retrieved source and let the user evaluate.
- Distinctive: line 93
No defined behavior for empty retrieval results
- Distinctive: line 96
No source citations for retrieved information
≈ “Common” is an estimate: at least 1 in 5 of the 308,232 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
RAG and memory systems are how AI agents work with knowledge that exceeds their context window. Done well: agents give accurate, grounded answers. Done poorly: context overflow, hallucination from stale retrieval, and performance degradation.
This skill covers the design principles and failure modes of RAG and memory architectures for production AI systems.
When to Use
- Building any AI system that needs to access external knowledge
- When agent context windows are being exceeded
- When agents need to remember information across sessions
- When building Q&A, document analysis, or knowledge base systems
Process
Step 1: Choose the Right Memory Architecture
- Identify what the agent needs to remember:
- Ephemeral: Within a single session (use in-context memory)
- Session-persistent: Across a user's sessions (use external key-value store)
- Knowledge base: Organizational or domain knowledge (use vector DB + RAG)
- Procedural: How to do tasks (encode in SKILL.md / system prompt)
- Match the memory type to the store:
| Memory Type | Recommended Store |
| In-session facts | Context window (summarized) |
| User preferences | Key-value store (Redis, DynamoDB) |
| Document corpus | Vector database (Pinecone, Weaviate, pgvector) |
| Long-term facts | Structured DB + caching |
Verify: Each type of information the agent needs has a defined storage mechanism.
Step 2: Design the RAG Pipeline
- Chunking strategy: Break documents into chunks at semantic boundaries (paragraphs, sections) — not arbitrary character counts.
- Embedding model: Match the embedding model to your query type. Use the same model for indexing and retrieval.
- Retrieval: Retrieve top-K most semantically similar chunks. K = 3–7 is usually optimal.
- Re-ranking: After retrieval, re-rank by relevance using a cross-encoder. Top K becomes top 3–5 for the prompt.
- Context injection: Inject retrieved chunks into the prompt with clear source citations.
Verify: Retrieved chunks are genuinely relevant to the query before injecting into context.
Step 3: Prevent Context Bloat
- Summarize, don't accumulate: For long sessions, summarize previous turns rather than appending them indefinitely.
- Retrieve, don't pre-load: Only load context relevant to the current query. Don't pre-load everything.
- Set context budgets: Define maximum token allocations for: system prompt, retrieved context, conversation history, user message.
- Compress before injecting: Summarize long retrieved documents to extract the relevant portion only.
Verify: Total prompt length is within model limits with buffer. Retrieved context is relevant to current query.
Step 4: Handle Retrieval Failures Gracefully
- If retrieval returns no relevant results: say so — do not hallucinate an answer.
- If retrieved documents are outdated: surface the document date to the user.
- If confidence is low: present the retrieved source and let the user evaluate.
- Design for "no relevant information found" as a first-class outcome.
Verify: System has defined behavior for failed/empty retrieval.
Step 5: Measure and Optimize
- Track retrieval quality:
- Precision: Are retrieved chunks relevant to the query?
- Recall: Are relevant chunks being retrieved at all?
- Track answer quality: Use RAGAS or similar evaluation framework.
- Monitor: context length per query, retrieval latency, hallucination rate.
Verify: Baseline metrics established. Retrieval precision > 80%.
Common Rationalizations (and Rebuttals)
| Excuse | Rebuttal |
| "Let's just put everything in the context" | Context bloat degrades quality and costs money. Retrieve what's needed. |
| "The model knows this from training" | Training knowledge is stale. Use RAG for current information. |
| "Vector search is good enough without re-ranking" | Re-ranking improves precision significantly. It's a small cost for large quality gain. |
| "We'll fix retrieval quality later" | Poor retrieval quality compounds into poor answer quality. Fix it now. |
Red Flags
- Entire document corpus pre-loaded into every prompt
- Retrieval returning chunks from unrelated documents
- No defined behavior for empty retrieval results
- Context window regularly at 90%+ capacity
- Agent answering from "training knowledge" instead of retrieved documents
- No source citations for retrieved information
Verification
- Memory architecture matches the type of information needed
- RAG pipeline: chunk → embed → retrieve → re-rank → inject
- Context budgets defined for all prompt sections
- Empty retrieval has a defined graceful fallback
- Retrieval precision measured and > 80%
- Source citations included in AI responses
References
- hallucination-prevention skill
- multi-agent-orchestration skill
- ai-output-validation skill
In this repository34 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- agent-qa-run-reviewSkill
- ai-output-validationSkill
- api-designSkill
- ci-cd-pipelinesSkill
- citruscoastaiSkill
- code-explanationSkill
- code-reviewSkill
- context-loadingSkill
- debugging-methodologySkill
- documentationSkill
- error-handlingSkill
- frontend-engineeringSkill
- git-workflowSkill
- goal-driven-executionSkill
- hallucination-preventionSkill
- idea-to-specSkill
- incremental-codingSkill
- integration-testingSkill
- meeting-notes-to-tasksSkill
- multi-agent-orchestrationSkill
- observabilitySkill
- performance-optimizationSkill
- production-deploymentSkill
- prompt-injection-defenseSkill
- refactoringSkill
- research-and-summarizeSkill
- security-hardeningSkill
- simplicity-firstSkill
- surgical-changesSkill
- task-decompositionSkill
- test-driven-developmentSkill
- think-before-codingSkill
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.

