0tokens

Apply for AI Grants India

Financial support for innovators building the future of AI in India.

Apply now

Chat · retrieval augmented generation

Retrieval Augmented Generation: A Practical Guide

  1. aigi

    Retrieval augmented generation (RAG) is an AI architecture that allows a language model to retrieve relevant information from an external knowledge base before generating an answer. Instead of relying only on training data, a RAG system combines search with generation, making responses more current, traceable, and grounded in an organisation’s documents.

    For Indian startups, enterprises, universities, and public-sector teams, RAG is often the fastest path to useful generative AI. It can power support assistants, legal research, internal knowledge search, healthcare information systems, and multilingual applications without training a foundation model from scratch.

    What Is Retrieval Augmented Generation?

    Retrieval augmented generation is a pipeline with three core stages:

    1. Retrieve: Search a connected data source for passages relevant to a user query.
    2. Augment: Insert the retrieved passages, metadata, and instructions into the model’s context.
    3. Generate: Ask a large language model (LLM) to produce an answer grounded in that context.

    A conventional LLM answers from statistical patterns learned during pre-training and any later fine-tuning. That makes it powerful, but it can also produce outdated or unsupported claims. RAG adds an evidence layer at inference time.

    A simplified formulation is:

    answer = LLM(user_query + retrieved_context + system_instructions)

    The model still performs language generation, but the retrieval system determines which source material is available to it. Good RAG therefore depends as much on information architecture, document quality, and evaluation as on the choice of LLM.

    Why RAG Matters

    RAG addresses several limitations of standalone language models:

    • Freshness: Retrieve recently updated policies, prices, product information, or regulations.
    • Private knowledge: Answer from internal documents that were not part of public model training.
    • Traceability: Return citations, source links, page numbers, or document identifiers.
    • Lower training cost: Update a knowledge base without retraining a foundation model.
    • Domain adaptation: Support specialised terminology and workflows.
    • Access control: Filter retrieved content according to user permissions.

    For India-focused deployments, retrieval can connect models to GST and tax documentation, government schemes, company policies, local-language content, healthcare protocols, or region-specific product catalogues. However, regulated use cases still require human oversight, privacy controls, and well-defined accountability.

    How a RAG Architecture Works

    A production RAG system normally has an offline ingestion path and an online query path.

    1. Data ingestion

    The system collects source material from PDFs, websites, databases, ticketing systems, cloud storage, APIs, and enterprise applications. Ingestion should preserve useful metadata such as:

    • Document title and version
    • Author or owning department
    • Publication and expiry dates
    • Language and region
    • Access-control labels
    • Page, section, or paragraph identifiers

    Poor extraction can damage tables, headings, footnotes, and multilingual text before retrieval even begins. Scanned documents may require OCR, while complex PDFs often need layout-aware parsers.

    2. Cleaning and chunking

    Documents are divided into chunks that can fit into the model context while remaining meaningful. Fixed-size chunks are simple, but semantic or structure-aware chunking is usually better. A policy, for example, should ideally keep a rule with its scope, exception, and definition rather than splitting them arbitrarily.

    Common approaches include:

    • Token-based chunks with overlap
    • Paragraph or heading-based chunks
    • Recursive character splitting
    • Sentence-aware semantic chunks
    • Parent-child retrieval, where small passages point to larger sections

    Chunk size is a retrieval and generation trade-off. Very small chunks may lack context; very large chunks may dilute relevance and consume expensive context tokens. Test chunking against representative queries rather than selecting a universal number.

    3. Embedding and indexing

    An embedding model converts each chunk into a vector representing its semantic meaning. These vectors are stored in a vector database or search engine. At query time, the user’s question is embedded using the same model, and the system searches for nearby vectors.

    Typical infrastructure choices include vector databases, PostgreSQL extensions, search engines with vector support, and managed cloud services. Selection should consider scale, latency, filtering, backups, tenancy, and India data-residency requirements—not only benchmark scores.

    4. Query processing

    Before search, the application may rewrite an ambiguous question, detect language, extract filters, or classify intent. A query such as “What is the reimbursement limit?” may need conversation history or a department filter to become actionable.

    Useful techniques include:

    • Query rewriting
    • Multi-query expansion
    • HyDE-style hypothetical document generation
    • Metadata filtering
    • Language detection and translation
    • Conversational query resolution

    Query rewriting must not silently change user intent. Keep the original query for auditing and compare rewritten forms during evaluation.

    5. Retrieval and reranking

    The first retrieval stage commonly uses vector similarity, keyword search, or both. Hybrid search combines semantic matching with lexical matching, which is valuable for exact identifiers, product codes, legal clauses, and names.

    A reranker then scores the top candidate passages using a more precise model. This two-stage design improves relevance without applying an expensive model to the entire corpus. Retrieval results should include source metadata so the generator can cite evidence accurately.

    6. Prompt construction and generation

    The application builds a prompt containing the user’s question, selected context, behavioural instructions, and output format. A robust prompt should tell the model to:

    • Use only the supplied sources for factual claims where appropriate
    • Say when the evidence is insufficient
    • Distinguish conflicting or outdated sources
    • Preserve important numerical and legal details
    • Include citations tied to retrieved passages
    • Avoid revealing restricted content

    The model’s answer should be treated as a generated interpretation, not automatic proof that the retrieved passages support every statement.

    RAG Versus Fine-Tuning

    RAG and fine-tuning solve different problems. RAG changes the information available to the model at runtime. Fine-tuning changes model behaviour or weights by training on examples.

    Choose RAG when you need:

    • Frequently changing knowledge
    • Citations and source grounding
    • Private or tenant-specific documents
    • Fast updates without model training
    • Search over a large document collection

    Choose fine-tuning when you need:

    • Consistent output style or structure
    • Better adherence to a specialised task format
    • Classification or extraction behaviour
    • Domain-specific interaction patterns

    Many mature systems use both: fine-tune or configure a model for behaviour, then use RAG to supply current facts. Fine-tuning does not automatically create a reliable knowledge database, and RAG does not automatically teach a model a new workflow.

    How to Build a Reliable RAG System

    Define the answer contract

    Before selecting models, define what a successful answer means. Specify required citations, acceptable latency, supported languages, refusal behaviour, confidence handling, and escalation to a human. For example, a financial support assistant may be required to quote the policy version and refuse advice outside approved products.

    Build a representative evaluation set

    Collect real questions, including ambiguous queries, spelling errors, multilingual questions, adversarial prompts, and questions with no answer in the corpus. Label the relevant documents and expected answer characteristics. Do not evaluate only on queries written by the engineering team.

    Measure retrieval separately

    Useful retrieval metrics include:

    • Recall@k: Whether a relevant passage appears in the top k results
    • Precision@k: How many top results are relevant
    • MRR: How highly the first relevant result is ranked
    • nDCG: Ranking quality across graded relevance levels

    Generation metrics should assess factuality, completeness, citation correctness, refusal quality, and answer relevance. Automated judge models can help with scale, but human review remains important for high-risk domains.

    Add observability

    Log the query, rewritten query, filters, retrieved document IDs, scores, prompt version, model version, latency, token usage, and user feedback. Redact personal or sensitive data before storing logs. These traces make it possible to diagnose whether an error came from ingestion, retrieval, prompting, or generation.

    Design for failure

    A trustworthy assistant should not answer every question confidently. Add an abstention threshold, source-conflict handling, fallback search, and human escalation. If no relevant evidence is retrieved, the system should clearly say that it cannot verify the answer rather than inventing one.

    Security, Privacy, and Compliance

    RAG introduces risks because retrieved documents become model context. A malicious or compromised document can contain prompt-injection instructions designed to manipulate the model. Treat retrieved text as untrusted data, not as system-level instructions.

    Important controls include:

    • Enforce document-level and row-level permissions before retrieval
    • Isolate tenants and verify authorisation on every request
    • Encrypt data in transit and at rest
    • Minimise personally identifiable information in indexes and logs
    • Apply retention and deletion policies to source and vector data
    • Scan documents for malware and suspicious instructions
    • Prevent sensitive content from appearing in citations or answers
    • Test prompt injection, data exfiltration, and indirect instruction attacks

    For Indian deployments, map the design to applicable contractual, sectoral, and privacy obligations, including requirements concerning personal data processing, cross-border transfers, retention, and auditability. Obtain legal and security review for healthcare, finance, education, government, and critical infrastructure applications.

    Cost and Performance Optimisation

    RAG costs come from ingestion, embeddings, storage, retrieval, reranking, LLM tokens, and operational monitoring. The largest recurring expense is often generation, especially when prompts contain excessive context.

    Optimise by:

    • Removing duplicate and obsolete documents
    • Using smaller embedding models where quality permits
    • Applying metadata filters before vector search
    • Reranking a limited candidate set
    • Compressing or summarising redundant context
    • Caching stable queries and embeddings
    • Routing simple requests to smaller models
    • Streaming responses when appropriate
    • Measuring cost per resolved task, not only cost per request

    Latency budgets should be measured end to end. A fast vector search does not help if document parsing, reranking, model queueing, or network calls dominate response time.

    Common RAG Failure Modes

    Irrelevant retrieval

    The search system returns semantically similar but operationally incorrect passages. Improve metadata, hybrid search, chunking, query rewriting, and reranking. Exact identifiers and filters are especially important for enterprise data.

    Correct context, incorrect answer

    The model receives the right evidence but misreads it, combines conflicting statements, or performs unreliable calculations. Use structured output, explicit instructions, deterministic tools for arithmetic, and citation-level verification.

    Missing context

    A chunk contains a reference such as “this limit” without the definition in a neighbouring section. Use parent-child retrieval, larger contextual windows, or document structure-aware chunking.

    Outdated sources

    Indexing alone does not guarantee freshness. Track versions, expiry dates, deletion events, and source ownership. Prefer authoritative documents when multiple versions exist.

    Citation hallucination

    The answer includes plausible-looking citations that do not support the claim. Generate citations from retrieved document IDs and validate that each cited span actually supports the statement.

    Permission leakage

    A shared index can accidentally return confidential content to an unauthorised user. Apply access filters at retrieval time and verify them again before generation and display.

    RAG Use Cases in India

    RAG is particularly useful where information is fragmented, frequently updated, multilingual, or organisation-specific. Examples include:

    • Customer support: Search product manuals, warranty rules, and service records.
    • Government and civic services: Explain scheme eligibility from current official sources.
    • Legal operations: Retrieve clauses, precedents, and internal templates with citations.
    • Healthcare administration: Search approved protocols while escalating clinical decisions.
    • Education: Answer questions from course material in English and Indian languages.
    • Manufacturing: Assist technicians using equipment manuals and maintenance logs.
    • Financial services: Support policy lookup with strict permissions and audit trails.
    • Agriculture: Combine local-language guidance, weather information, and crop advisories.

    In each case, domain ownership and update processes matter as much as model performance. Assign responsibility for correcting source documents and reviewing failed answers.

    A Practical Implementation Checklist

    Before launching a retrieval augmented generation application, verify:

    • The corpus has clear owners, versions, and update schedules.
    • Documents are extracted accurately, including tables and scanned pages.
    • Chunking has been tested on real user questions.
    • Hybrid retrieval and metadata filters are available where needed.
    • Access control is enforced before content reaches the model.
    • Retrieval and generation are evaluated independently.
    • Answers provide verifiable citations or transparent uncertainty.
    • Prompt injection and data leakage tests are part of release review.
    • Costs, latency, token use, and error rates are monitored.
    • Human escalation exists for high-impact or unresolved cases.

    Frequently Asked Questions

    Is retrieval augmented generation the same as a chatbot?

    No. A chatbot is an interface or application pattern. RAG is a method for grounding an AI system in external information. A chatbot may use RAG, but RAG can also power search, document analysis, APIs, and workflow automation.

    Does RAG eliminate hallucinations?

    No. It can reduce unsupported answers by supplying relevant evidence, but retrieval errors, ambiguous documents, and model misinterpretation remain possible. Evaluation, citations, abstention, and human review are still necessary.

    Do I need to train an LLM to build RAG?

    Usually not. You can connect an existing language model to an embedding model, search index, document pipeline, and application layer. Fine-tuning may be useful later for specialised behaviour.

    Which database is best for RAG?

    There is no universal best option. Compare vector capability, hybrid search, metadata filtering, scale, latency, operations, tenancy, backups, and data-residency requirements against your workload.

    How much data is needed for RAG?

    RAG can work with a small policy library or millions of documents. Data quality, structure, permissions, and query coverage usually matter more than raw document volume.

    Apply for AI Grants India

    Building a retrieval augmented generation product for Indian users? Apply through AI Grants India to explore support and opportunities for your AI startup. Submit your venture details and take the next step toward developing a responsible, scalable AI solution.

AIGI may be inaccurate. Replies seeded from the guide above.