$npx -y skills add DevelopersGlobal/ai-agent-skills --skill rag-and-memoryPatterns for Retrieval-Augmented Generation (RAG) and agent memory systems. Retrieves only relevant context, prevents context bloat, and maintains coherent state across sessions.
| 1 | ## Overview |
| 2 | |
| 3 | 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. |
| 4 | |
| 5 | This skill covers the design principles and failure modes of RAG and memory architectures for production AI systems. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Building any AI system that needs to access external knowledge |
| 10 | - When agent context windows are being exceeded |
| 11 | - When agents need to remember information across sessions |
| 12 | - When building Q&A, document analysis, or knowledge base systems |
| 13 | |
| 14 | ## Process |
| 15 | |
| 16 | ### Step 1: Choose the Right Memory Architecture |
| 17 | |
| 18 | 1. Identify what the agent needs to remember: |
| 19 | - **Ephemeral**: Within a single session (use in-context memory) |
| 20 | - **Session-persistent**: Across a user's sessions (use external key-value store) |
| 21 | - **Knowledge base**: Organizational or domain knowledge (use vector DB + RAG) |
| 22 | - **Procedural**: How to do tasks (encode in SKILL.md / system prompt) |
| 23 | 2. Match the memory type to the store: |
| 24 | |
| 25 | | Memory Type | Recommended Store | |
| 26 | |------------|------------------| |
| 27 | | In-session facts | Context window (summarized) | |
| 28 | | User preferences | Key-value store (Redis, DynamoDB) | |
| 29 | | Document corpus | Vector database (Pinecone, Weaviate, pgvector) | |
| 30 | | Long-term facts | Structured DB + caching | |
| 31 | |
| 32 | **Verify:** Each type of information the agent needs has a defined storage mechanism. |
| 33 | |
| 34 | ### Step 2: Design the RAG Pipeline |
| 35 | |
| 36 | 3. **Chunking strategy**: Break documents into chunks at semantic boundaries (paragraphs, sections) — not arbitrary character counts. |
| 37 | 4. **Embedding model**: Match the embedding model to your query type. Use the same model for indexing and retrieval. |
| 38 | 5. **Retrieval**: Retrieve top-K most semantically similar chunks. K = 3–7 is usually optimal. |
| 39 | 6. **Re-ranking**: After retrieval, re-rank by relevance using a cross-encoder. Top K becomes top 3–5 for the prompt. |
| 40 | 7. **Context injection**: Inject retrieved chunks into the prompt with clear source citations. |
| 41 | |
| 42 | **Verify:** Retrieved chunks are genuinely relevant to the query before injecting into context. |
| 43 | |
| 44 | ### Step 3: Prevent Context Bloat |
| 45 | |
| 46 | 8. **Summarize, don't accumulate**: For long sessions, summarize previous turns rather than appending them indefinitely. |
| 47 | 9. **Retrieve, don't pre-load**: Only load context relevant to the current query. Don't pre-load everything. |
| 48 | 10. **Set context budgets**: Define maximum token allocations for: system prompt, retrieved context, conversation history, user message. |
| 49 | 11. **Compress before injecting**: Summarize long retrieved documents to extract the relevant portion only. |
| 50 | |
| 51 | **Verify:** Total prompt length is within model limits with buffer. Retrieved context is relevant to current query. |
| 52 | |
| 53 | ### Step 4: Handle Retrieval Failures Gracefully |
| 54 | |
| 55 | 12. If retrieval returns no relevant results: say so — do not hallucinate an answer. |
| 56 | 13. If retrieved documents are outdated: surface the document date to the user. |
| 57 | 14. If confidence is low: present the retrieved source and let the user evaluate. |
| 58 | 15. Design for "no relevant information found" as a first-class outcome. |
| 59 | |
| 60 | **Verify:** System has defined behavior for failed/empty retrieval. |
| 61 | |
| 62 | ### Step 5: Measure and Optimize |
| 63 | |
| 64 | 16. Track retrieval quality: |
| 65 | - **Precision**: Are retrieved chunks relevant to the query? |
| 66 | - **Recall**: Are relevant chunks being retrieved at all? |
| 67 | 17. Track answer quality: Use RAGAS or similar evaluation framework. |
| 68 | 18. Monitor: context length per query, retrieval latency, hallucination rate. |
| 69 | |
| 70 | **Verify:** Baseline metrics established. Retrieval precision > 80%. |
| 71 | |
| 72 | ## Common Rationalizations (and Rebuttals) |
| 73 | |
| 74 | | Excuse | Rebuttal | |
| 75 | |--------|----------| |
| 76 | | "Let's just put everything in the context" | Context bloat degrades quality and costs money. Retrieve what's needed. | |
| 77 | | "The model knows this from training" | Training knowledge is stale. Use RAG for current information. | |
| 78 | | "Vector search is good enough without re-ranking" | Re-ranking improves precision significantly. It's a small cost for large quality gain. | |
| 79 | | "We'll fix retrieval quality later" | Poor retrieval quality compounds into poor answer quality. Fix it now. | |
| 80 | |
| 81 | ## Red Flags |
| 82 | |
| 83 | - Entire document corpus pre-loaded into every prompt |
| 84 | - Retrieval returning chunks from unrelated documents |
| 85 | - No defined behavior for empty retrieval results |
| 86 | - Context window regularly at 90%+ capacity |
| 87 | - Agent answering from "training knowledge" instead of retrieved documents |
| 88 | - No source citations for retrieved information |
| 89 | |
| 90 | ## Verification |
| 91 | |
| 92 | - [ ] Memory architecture matches the type of information needed |
| 93 | - [ ] RAG pipeline: chunk → embed → retrieve → re-rank → inject |
| 94 | - |