When to Build Your Own RAG System: Scenarios, Architecture, and Decision Checklist

    Long context windows do not replace retrieval-augmented generation. This article explains the four scenarios that justify building a self-hosted RAG system—corpus size beyond economic limits, tight latency budgets, strict controllability requirements such as citation traceability and permission isolation, and continuous updates—then outlines a minimal six-layer architecture and the cases where relying on long context is the better choice.

    1. First, a premise: long context is not a replacement for RAG

    Over the past two years, mainstream large models have expanded their context windows from tens of thousands of tokens to hundreds of thousands or even over a million, while prices have kept falling. Public pricing falls into roughly these ranges: one flagship class of model charges about $1.25 per million input tokens up to 200K tokens, then jumps to two tiers of $2.5 and $15 beyond 200K tokens; a mid-to-high-end tier is priced at about $3 and $15. These numbers show that “stuffing everything in” is no longer expensive on a per-token basis, but a low unit price does not mean a low total cost.

    In April 2025, a platform published a set of benchmarks: a lightweight long-context model took more than 30 seconds of inference time to process 360K input tokens, and close to a minute for 600K tokens. A retrieval approach of comparable scale (about 20,000 documents and 3 million vectors) brought end-to-end response time under one second. The core fact this comparison reveals: the cost of long context grows linearly with input size, while the cost of retrieval depends mainly on the recall size and is only weakly correlated with the total corpus size.

    So the scenarios that require a self-built RAG system are not fundamentally about “the model not being long enough,” but about the four types of constraints below holding individually or together.

    2. Four scenarios that require a self-built RAG system

    1. The corpus exceeds the economic boundary of “what can be stuffed in”

    Assume an enterprise knowledge base has 50,000 documents averaging 3,000 tokens each, for roughly 150 million tokens in total. Even at a unit price of $1.25 per million tokens, a single full injection costs on the order of $180 in input cost, and that cost is paid again on every question. At 10,000 Q&A requests per day, the daily cost quickly spirals out of control. With retrieval, each request injects only the 8–15 recalled chunks (about 3,000–8,000 tokens), cutting cost by three to four orders of magnitude.

    The rule of thumb is simple:

    1. cost per request ≈ unit price × (recalled chunk tokens + question tokens)
    2. full injection cost ≈ unit price × total corpus tokens

    When the ratio of total corpus tokens / recalled tokens exceeds 100 and call volume is not low, self-built retrieval starts to win decisively on economics.

    2. The latency budget is tight and cannot tolerate first-token responses above one second

    The overhead of long context is not only about money but also about time. The compute in the prefill stage is roughly proportional to input length, so inputs of hundreds of thousands of tokens naturally bring first-token latency on the order of tens of seconds. Interactive scenarios such as customer service, ticketing, shopping guidance, and coding assistants typically require first-token latency within 1–2 seconds. A retrieval pipeline can compress model input by an order of magnitude, leaving the time budget for the actual generation step.

    A common engineering breakdown of latency is: query rewriting 50–150ms, vector recall 20–80ms, reranking 80–200ms, first generated token 300–800ms. Each segment can be optimized independently, whereas a long-context approach leaves almost no tunable middle ground.

    3. Strong controllability is required: citation traceability, permission isolation, and deletion

    Generative model output is hard to guarantee as traceable sentence by sentence, yet compliance, audit, legal, and healthcare scenarios often require that “every conclusion can be clicked back to the source text.” A self-built retrieval system naturally produces chunk IDs, source documents, version numbers, and position offsets, which can be rendered directly into citation cards.

    Permissions are equally critical. Enterprise documents differ by department, project, and classification level. If access control lists (ACLs) are written into metadata at ingestion time and filtered upfront by user identity at retrieval time, you avoid “the model seeing content it should not see.” In full-injection mode, permission filtering can only happen before concatenation, which is nearly infeasible once the corpus is large.

    In addition, the “right to be forgotten” and data correction requirements mean a document must stop being used immediately after deletion. A self-built index can take effect within minutes; relying on a model’s internal memory cannot guarantee this.

    4. Continuous updates and domain adaptation are required

    The knowledge base changes every day: product parameter adjustments, price updates, new incident postmortems. The update path for a retrieval approach is “document change → chunking → embedding → index write,” which can be automated, rolled back, and released gradually. A long-context approach has to reorganize the input every time, and updates lack version management.

    3. A minimal viable architecture for self-built RAG

    A deployable system usually has six layers, and it is best to split and iterate in this order:

    1. Data ingestion layer: connect to object storage, message queues, relational databases, and change notifications from business systems, performing incremental sync and deduplication.
    2. Parsing and chunking layer: split by document structure (heading hierarchy, paragraphs, tables, code blocks) while preserving semantic completeness; a chunk length of 300–800 tokens with 10%–15% overlap is recommended.
    3. Indexing layer: a vector index handles semantic recall while an inverted index handles exact matching of keywords and proper nouns, running in parallel.
    4. Retrieval and reranking layer: first recall 50–100 candidates coarsely, then use a cross-encoder to rerank and take the top 10, balancing recall with noise suppression.
    5. Generation layer: assemble the prompt and constrain the model to “answer only based on the given chunks, and explicitly state when it cannot answer without support.”
    6. Evaluation and observability layer: log recall hit rate, answer faithfulness, citation accuracy, and latency percentiles, closing the loop with logging services and monitoring alerts.

    Illustrative code (pseudo-implementation, to express the layered design):

    1. def answer(query, user):
    2. rewritten = rewrite(query) # query rewriting, resolve references
    3. cands = vector_index.search(rewritten, top_k=80,
    4. filter=acl_of(user)) # upfront permission filtering
    5. cands += keyword_index.search(rewritten, top_k=40,
    6. filter=acl_of(user))
    7. top = reranker.rank(rewritten, cands)[:10] # rerank for noise reduction
    8. context = pack(top, max_tokens=6000) # control the injection budget
    9. return llm.generate(prompt(query, context),
    10. require_citation=True)

    4. When you should not build your own

    It is worth stating objectively that self-building is not better by default. If the total corpus is under 100,000 tokens, updates are infrequent, latency is not sensitive, and citation traceability is not needed, relying directly on long context is simpler. If a team lacks data governance capability and the chunking and metadata design are sloppy, the recall quality of a self-built retrieval system may fall below that of long context and actually lower answer accuracy. In addition, operating a vector database, upgrading embedding models, and rebuilding indexes all constitute ongoing investment, and staffing needs to be assessed in advance.

    5. Decision checklist

    • Does the total corpus exceed 100× the tokens recalled in a single request?
    • Is the first-token latency budget under 2 seconds?
    • Is sentence-level citation and document-level permission isolation required?
    • Do documents update daily, requiring versioning and rollback?
    • Is there already a stable data ingestion and quality validation process?

    If two or more of the first four items apply, the return on investment of self-built retrieval-augmented generation usually holds. If only one applies, start with a small-scale validation, compare accuracy and latency on a real Q&A set, and then decide whether to scale up. When deploying on the cloud, you can reuse common capabilities such as object storage, message queues, container platforms, logging services, and monitoring alerts to support each of the layers above, and focus your effort on the two points that truly determine effectiveness: the chunking strategy and the evaluation system.