LESSON 74 · REAL-WORLD PROJECTS

RAG Application

Build an RAG application that answers employee questions from trusted retrieved knowledge using RAG, access-aware retrieval, citations, and production monitoring.

30 min read•Intermediate•Python•RAG Project

How a RAG Application Works

DocumentsPDFs + web + data
IngestParse + clean
ChunkSplit context
IndexVectors + metadata
RetrieveTop-K + rerank
GenerateGrounded + cited
iCore idea: a RAG application separates knowledge retrieval from generation. The model should answer using relevant retrieved context rather than relying only on its pretrained knowledge.
1

What Are We Building?

Build an RAG application that answers employee questions using company documents, policies, product information, and knowledge bases. The assistant should retrieve relevant retrieved knowledge before generating an answer.

User experience

Ask a natural-language question and receive a concise answer with supporting sources.

Application experience

Control sources, accesss, freshness, evaluation, and feedback.

2

Understand the Architecture

A practical architecture separates ingestion from question answering: documents are collected and indexed once, while employee questions trigger retrieval and generation.

SourcesApproved content
IngestionParse + chunk
IndexVectors + metadata
QueryRetrieve + filter
AnswerGround + cite
3

Connect Knowledge Sources

Typical sources include PDFs, DOCX files, web pages, support articles, databases, and application data. Choose sources that are trustworthy, current, and relevant to the use case.

4

Ingest Documents

The ingestion pipeline loads source content, extracts text, preserves metadata, cleans noise, chunks the content, creates embeddings, and stores searchable records.

5

Preserve Metadata

Keep document ID, title, department, source URL, access group, section, timestamps, and version information. Metadata becomes important for filtering, citations, and accesss.

6

Chunk Company Knowledge

Good chunks preserve a coherent policy, procedure, product explanation, or section. Avoid chunks that mix unrelated departments or topics.

7

Create Embeddings

Embeddings turn chunks into vectors so a question such as “How many days can I work remotely?” can retrieve semantically related policy text even when the wording differs.

</> Python
from dataclasses import dataclass

@dataclass
class CompanyChunk:
    text: str
    document_id: str
    department: str
    access_group: str

chunk = CompanyChunk(
    text="Employees receive 20 paid vacation days.",
    document_id="hr-policy-2026",
    department="hr",
    access_group="employees",
)

print(chunk.document_id, chunk.department)
8

Store the Knowledge Index

Use a vector database or search index to store embeddings, text, and metadata. Keep the original source reference so answers can show where information came from.

9

Retrieve Relevant Knowledge

At query time, embed the employee question, retrieve candidate chunks, apply metadata and access filters, and optionally rerank the candidates before generation.

10

Apply Metadata and Access Filters

Retrieval must respect the employee’s accesss. A user should never receive retrieved text from a private HR, finance, legal, or executive document they are not authorized to access.

Security rule: apply authorization before the model receives retrieved content, not after the answer has already been generated.
11

Build a Grounded Prompt

Give the model only the selected company context and clear instructions to answer from that context. If the evidence is insufficient, the assistant should say that it cannot verify the answer.

</> Python
def build_grounded_prompt(question: str, contexts: list[str]) -> str:
    evidence = "\n\n".join(contexts)
    return f"""Answer using only the company evidence below.
If the evidence is insufficient, say you cannot verify the answer.

Evidence:
{evidence}

Question: {question}"""

prompt = build_grounded_prompt(
    "How many vacation days do employees receive?",
    ["HR policy: Employees receive 20 paid vacation days."],
)
print(prompt)
12

Return Sources and Citations

Show document titles, sections, page numbers, or links where possible. Citations let employees verify important answers and help administrators debug retrieval.

13

Handle Unknown Questions

Do not force an answer. If retrieval confidence is low or the context does not support the question, route to a human, ask for clarification, or state that the knowledge base does not contain enough information.

14

Keep Knowledge Fresh

Company information changes. Track document versions, update timestamps, ingestion status, and deletion events. Re-index changed content instead of blindly creating duplicates.

  • Track source version
  • Record updated timestamp
  • Remove deleted content
  • Re-index changed documents
15

Combine RAG with Tools

Use RAG for knowledge and tools for live actions. For example, retrieve the leave policy with RAG, then call an HR system to check an employee’s remaining leave balance.

16

Monitor the Assistant

Track retrieval quality, answer quality, latency, token usage, citations, failed searches, access denials, and user feedback.

</> Python
def can_access(user_groups: set[str], required_group: str) -> bool:
    return required_group in user_groups

user_groups = {"employees", "engineering"}
print(can_access(user_groups, "employees"))
print(can_access(user_groups, "executives"))
17

Evaluate with Real Questions

Create a representative evaluation set from real employee questions. Measure retrieval relevance, groundedness, citation correctness, refusal behavior, and answer usefulness.

18

Secure the Knowledge Base

Protect source documents, embeddings, credentials, logs, and prompts. Treat retrieved company data as sensitive application data and apply least-privilege access.

IdentityAuthenticate every user.
AuthorizationFilter by access groups.
DataProtect documents and logs.
19

Build the RAG Application

A useful first version can support one department, a small document set, citations, access filters, feedback buttons, and an escalation path. Expand after measuring quality.

20

Common Mistakes

Indexing everything without ownership, ignoring accesss, returning uncited answers, using stale documents, retrieving too much context, and treating the LLM as the source of truth are common production mistakes.

21

Mini Project: RAG Question Answering Assistant

Create an assistant for a product, support, or internal knowledge domain. Index approved documents, attach metadata, filter by employee access, retrieve the top candidates, generate a grounded answer, and return citations plus a “source or human fallback” fallback.

22

30-Second Recap

RAG Application = trusted sources + ingestion + chunking + embeddings + retrieval + access filtering + grounded generation + citations + monitoring.

23

Key Takeaways

Treat retrieved knowledge as a governed data system. Retrieval should be access-aware, answers should be grounded and cited, and freshness and evaluation must be part of the design.

Quick Check

1. What should the assistant do when the knowledge base cannot verify an answer?

A. Guess confidently B. Say it cannot verify the answer or escalate C. Invent a policy D. Ignore retrieval

2. Why is access-aware retrieval important?

A. It makes embeddings smaller B. It prevents users from receiving unauthorized company information C. It removes the need for authentication D. It increases model temperature

3. What should be returned with important answers?

A. Only a confidence score B. Source references or citations C. The full database D. Hidden prompts

Key Takeaways

  • Use trusted sources as the foundation for answers.
  • Make retrieval access-aware before sending context to the model.
  • Return citations so employees can verify important information.
  • Track freshness, retrieval quality, groundedness, latency, and feedback.