Knowledge retrieval (rag)

RAG (Retrieval-Augmented Generation) is one of the most important patterns in production AI. Instead of relying solely on the LLM's training data, you retrieve relevant documents from a knowledge base and include them as context. This gives the model up-to-date, domain-specific information.

Agent memory types

How agents store and share knowledge across different memory layers

RAG pipeline

No, but it significantly reduces them by grounding responses in real documents. The LLM can still misinterpret retrieved content, which is why you will later learn relevancy checks and evaluation patterns to catch remaining issues.

patterns/14_knowledge_retrieval.py
python
class KnowledgeBase:
    def __init__(self):
        self.documents = []

    def add_document(self, content, metadata=None):
        self.documents.append({
            "content": content, "metadata": metadata or {}
        })

    def search_knowledge(self, query, top_k=3):
        """Simple keyword-based search (use vector DB in production)."""
        scored = []
        query_words = set(query.lower().split())
        for doc in self.documents:
            doc_words = set(doc["content"].lower().split())
            overlap = len(query_words & doc_words)
            scored.append((overlap, doc))
        scored.sort(reverse=True, key=lambda x: x[0])
        return [doc for _, doc in scored[:top_k]]

class RAGSystem:
    def __init__(self):
        self.knowledge_base = KnowledgeBase()
        self.llm = get_llm()

    def query(self, question):
        docs = self.knowledge_base.search_knowledge(question)
        context = "\n".join([d["content"] for d in docs])
        prompt = f"""
        Knowledge Context:
        {context}

        Question: {question}
        Answer using ONLY the provided context.
        """
        return self.llm.generate(prompt).content

A RAGSystem that searches a knowledge base and augments the LLM prompt with context.

AI prompt: Try it with AI

Loading practice…

Quiz: Quiz

Loading practice…

Matching exercise: Match RAG Components

Loading practice…

Flashcards: Flashcards

Loading practice…

Adding static context works for small, fixed information. Knowledge retrieval dynamically fetches relevant information based on the query. As your knowledge base grows to thousands of documents, you cannot fit everything in the prompt. Retrieval selects only what is relevant, keeping costs down and accuracy up.

You now have a working RAG pattern that grounds AI responses in real documents. Next, we will enable agents to communicate with each other through a message hub.