RAG Pipeline Architecture Design
Errors in early pipeline stages propagate invisibly through to final answers.

The six stages run in sequence: ingestion, chunking, embedding, retrieval, augmentation, generation. Each stage produces an artifact the next stage consumes. This sounds clean on a whiteboard. In practice, errors do not stay local.
They propagate forward, and they do so quietly, which is the part that takes time to internalize. A retrieval mistake at stage four cannot be corrected by a better language model at stage six. The LLM generates from whatever context it receives, and it generates confidently. An answer grounded in the wrong retrieved document is indistinguishable in tone from an answer grounded in the right one. That is what makes upstream failures so treacherous: they are invisible at the output layer, precisely where most teams are watching.
Teams building their first RAG prototype almost always focus on the model. Understandable, since the model produces the visible output. Teams that have run these systems in production long enough to hit real failures tend to shift attention to retrieval first, then ingestion, because that is where the trouble actually starts. The model usually does its job. The question is what you handed it.
Modular design is the architectural principle that makes this tractable over time. Cleanly decoupled components can be swapped individually: embedding models, vector databases, LLMs, as better options emerge. In a field moving as quickly as this one, that independence is not an abstract engineering virtue. It is what keeps a production system from requiring a full rebuild every six months.
Ingestion: what gets into the pipeline and how clean it is when it arrives
Production ingestion rarely starts from a single clean format. The realistic picture is document repositories, databases, APIs, file systems, and knowledge bases, each with its own structure, its own formatting conventions, its own update cadence, and its own way of resisting clean extraction. The engineering effort required to span all of these consistently is almost always underestimated, often by a wide margin.
Extraction is the underappreciated step. Converting PDFs, HTML files, spreadsheets, and presentation decks into structured, semantically coherent text requires more than a parser. Tables need to survive conversion as tables, not as streams of whitespace-separated tokens. Headings need to be preserved as headings, not dissolved into body text. Source relationships and metadata need to travel with the content. When they do not, the damage is silent: downstream stages receive text that looks processable but carries invisible structural breaks.
The garbage-in, garbage-out principle applies more literally here than anywhere else in the pipeline. A poorly extracted table or a dropped section heading does not produce an error. It produces a subtly degraded embedding, a subtly worse retrieval result, a subtly wrong answer, manifesting far from its origin and easy to misattribute during debugging. I have seen teams spend days tuning retrieval parameters when the actual problem was a PDF extraction library quietly mangling column headers on ingest.
Metadata preservation at ingestion time is one of the highest-leverage investments a team can make. Source, date, document type, and section structure recorded at this stage enable filtering and provenance tracing at retrieval. Without them, the system can find a chunk but cannot tell you where it came from or how old it is. For enterprise applications where auditability is a product requirement, that is a genuine gap, not a nice-to-have.
Domain complexity compounds everything. Compliance documents, contracts, and financial filings often carry highly structured layouts that generic text extractors mangle. A section reference embedded in a table, or a footnote whose content modifies a clause in the main body, requires extraction logic that understands what those structures mean, not just how to tokenize them. It is usually the most structurally complex documents, the ones users ask the hardest questions about, where extraction quietly falls apart. That pattern repeats across industries and codebases with enough regularity that I no longer treat it as a coincidence.
Freshness originates here, not at retrieval. If ingestion runs on a weekly batch job, every answer the system produces is as stale as the oldest uncrawled update. For fast-moving domains, continuous or near-real-time ingestion is a functional requirement, and deferring it is a decision with consequences that compound over time.
Chunking: how you divide documents shapes what the retriever can find
Chunking is the act of dividing extracted text into retrieval units: the segments that get embedded, indexed, and returned at query time. The decision sounds mechanical. Its consequences are not.
The core tension is between context and signal density. A chunk too small loses the surrounding context that gives individual sentences their meaning. A sentence asserting that a product "meets the compliance standard" is nearly meaningless without knowing which standard and which product, information that may live in the surrounding paragraph. Chunks too large dilute the retrieval signal with irrelevant tokens and consume more of the model's context window at generation time. The practical resolution tends to sit in the low hundreds of tokens, with modest overlap between adjacent chunks to prevent information from falling on a boundary.
Fixed-size splitting is the simplest approach and the one most prototypes start with. It ignores document structure entirely, which means a paragraph boundary mid-sentence is treated as equivalent to a clean semantic break. The resulting chunks are syntactically broken in ways that affect embedding quality without producing obvious errors. You find out later, in production, when queries that should have worked do not and nobody can immediately explain why.
Recursive splitting improves on this by respecting natural document structure: paragraphs first, then sentences, then words, with character-level splits as a last resort. The hierarchy reflects how meaning is actually organized in text, which is worth respecting even when it adds preprocessing complexity.
Semantic chunking goes further, grouping sentences by meaning rather than by character count. Empirical work has found that this measurably improves retrieval recall over fixed-size approaches, though at higher preprocessing cost. For high-value corpora where retrieval quality is the critical variable, the cost is usually justified.
Anthropic's Contextual Retrieval work, published in 2024, introduced a targeted improvement: prepending a short context situating each chunk within its source document before embedding. The retriever gains more signal about provenance and subject matter without any change to chunk size or downstream architecture. The problem it addresses, chunks that are semantically coherent but contextually opaque, is precisely the class of failure that shows up at scale, not in a notebook.
Domain-specific norms matter throughout. Code, legal text, and clinical documentation warrant larger chunks than general prose because their internal structure carries meaning that breaks if split. A code function divided across two chunks is not half a function; it is two useless fragments. These domains require chunking strategies that respect their unit of meaning, not just their token count.
Chunking is also not a one-time architectural decision. Query patterns from production traffic frequently reveal that the initial strategy was optimized for the wrong thing, usually the shape of the documents rather than the shape of the questions being asked. Revisiting it is not a sign of poor initial design. It is a sign of a team that is actually watching the system.
Embedding and indexing: how semantic meaning becomes searchable structure
Embedding converts chunks into dense numerical vectors that encode semantic meaning. Similar meaning produces vectors close together in high-dimensional space; dissimilar meaning produces vectors farther apart. Retrieval, at its core, is the problem of finding vectors close to the query vector quickly and accurately enough for production use.
Model choice is a first-order decision that many teams treat as a second-order one. Embedding models vary significantly in accuracy, context window length, and language coverage. Selecting a model without benchmarking it against the specific domain and query types of the target application is one of the more consistent failure modes in the transition from prototype to production. A model that performs well on general-purpose benchmarks may underperform on specialized legal or financial text, where vocabulary and phrasing diverge sharply from general corpora. Without domain-specific evaluation, you would probably never know.
Newer embedding models have expanded context window lengths considerably, some supporting tens of thousands of tokens where earlier models truncated at a few thousand. This materially affects chunking strategy: longer windows reduce the need to aggressively split documents, which preserves more contextual coherence per chunk and can simplify the chunking architecture significantly.
The vector database is where embeddings live at query time. Purpose-built options in the current market include Pinecone, Qdrant, Milvus, and Weaviate. Hybrid approaches that add vector search to existing Lucene-based systems like Elasticsearch and OpenSearch are also widely used in organizations that already have search infrastructure and prefer not to introduce a separate dependency. Neither approach is universally superior; the right choice depends on operational context, existing tooling, and scale requirements.
Index type determines retrieval latency. Approximate nearest-neighbor indexes, particularly HNSW and IVFPQ, trade a small amount of accuracy for a large reduction in query time. For most production workloads, that tradeoff is the right one: the latency reduction is substantial, the accuracy cost is small, and users do not tolerate slow systems regardless of accuracy.
The embedding model and vector store are coupled decisions in a way that is easy to overlook. A high-accuracy embedding model paired with a poorly configured index will underperform a moderate model with a well-tuned one. They need to be evaluated as a pair.
Re-embedding cost is the operational argument for choosing the embedding model carefully before scale. If the model is swapped after indexing, every stored chunk must be re-embedded. For large corpora, this is not a trivial operation, and it is one of the more persuasive reasons to spend time on model selection up front rather than treating it as something easily revisited.
Retrieval: why this stage fails more often than generation, and how to fix it
The field's dominant finding, corroborated across evaluations of production RAG systems, is that when pipelines produce wrong answers, the failure is far more often at retrieval than at generation. The language model is doing its job. It received the wrong documents.
A confident, fluent answer grounded in the wrong retrieved chunk is harder to catch than a generation error because the prose looks correct. There is no syntactic signal of failure. The answer is well-formed, responsive to the question, and wrong. End-to-end answer quality metrics can mask retrieval failures for exactly this reason: an answer that sounds plausible from incorrect context scores better than a halting but accurate one in casual evaluation, which means the metric actively conceals the problem it was meant to surface.
Pure vector search has a known weakness. It handles semantic similarity well but fails on exact identifiers: SKU numbers, policy codes, contract IDs, regulatory citations, anything where the user's intent is precise recall of a specific token sequence rather than conceptual similarity. For these queries, keyword matching is more reliable. This is not a flaw in vector search; it is a mismatch between the modality and the query type.
Hybrid search has become the production standard precisely because it addresses this mismatch. Combining dense vector search with sparse BM25 keyword search, then fusing the two ranked lists using Reciprocal Rank Fusion, recovers the cases where either mode alone would fail. The implementation overhead is usually modest relative to the reliability improvement.
For knowledge-intensive domains where entities have explicit relationships, graph-based retrieval surfaces connections that vector similarity misses. Organizational hierarchies, regulatory frameworks, product taxonomies: these are structures where the relationship between entities is itself informative, and where a similarity search over isolated chunks cannot surface that structure.
Reranking is a post-retrieval correction step that meaningfully improves precision. After initial retrieval returns a candidate set, a cross-encoder reranker re-scores chunks against the query in full context. The cross-encoder sees the query and the chunk together, not as separate vectors, which makes it a more sensitive relevance signal. The cost is latency; for most applications, the precision improvement justifies it.
Query transformation addresses failures that originate in the query itself. Rewriting the user's query into multiple sub-queries, expanding it with hypothetical answers as in the HyDE approach, or decomposing a compound question before retrieval all improve recall for ambiguous or multi-part questions. Users rarely formulate queries in the way that maximizes retrieval performance. The pipeline should compensate for that rather than penalize it.
The most actionable implication here is operational. Teams that instrument retrieval separately from generation, logging what was retrieved and evaluating whether it was relevant, can diagnose failures that end-to-end metrics would hide. Without that separation, debugging is essentially guesswork dressed up as root cause analysis.
Augmentation and the prompt: how retrieved context gets assembled for the model
Augmentation is the act of constructing the prompt the LLM will actually receive: retrieved chunks, the original query, and system instructions assembled into a single input. It is where the upstream work either pays off or leaks into the output.
Context window management is the first constraint. The model has a fixed input limit, and if retrieved chunks exceed it, something must be dropped or compressed. The logic that decides what to drop directly affects answer quality, yet it is frequently implemented as naive truncation rather than relevance-weighted selection. This is one of the more consistent points of divergence between prototypes and production systems, and one of the less glamorous ones to fix.
Chunk ordering matters in ways that are not immediately obvious. Research on long-context attention patterns has established that models attend more strongly to the beginning and end of long inputs, a phenomenon that has come to be called the "lost in the middle" problem. Placing the most relevant chunk first is an architectural choice that affects how reliably the model uses the best available evidence.
Source attribution in the assembled context deserves explicit design attention. Including document titles, URLs, and dates enables the model to produce cited answers. For enterprise use cases, citability separates a system users will trust from one they quietly route around.
The system prompt sets the model's instructions for how to use retrieved context: what to do when context is insufficient, whether to acknowledge uncertainty, how to format the response. These decisions are made at augmentation time, not at retrieval time, which means the prompt engineer and the retrieval engineer need to be working from a shared understanding of what retrieval can and cannot guarantee. In practice, they often are not.
Filtering before assembly prevents low-quality context from entering the prompt. Retrieved chunks that fall below a minimum relevance threshold should be excluded, because a model handed marginally relevant or contradictory context tends to produce worse answers than one working from a smaller but cleaner set. Less context, when the marginal context is noise, is more.
Generation: what the language model contributes and where its limits begin
The generator's role in a RAG system is synthesis, not recall. Its job is to take retrieved context and produce a coherent, appropriately scoped answer. A model that ignores the retrieved context and answers from its training weights is producing unverifiable output, regardless of how fluent it sounds.
Faithfulness to context is the key generative virtue in RAG. The diagnostic distinction matters: fabricating details not present in the retrieved chunks is a generation failure; retrieving the wrong chunks is a retrieval failure. They look nearly identical in the output, but they require different fixes. Conflating them leads teams to tune the wrong stage. Most evaluation frameworks do not distinguish between the two explicitly, which means debugging budgets get spent in the wrong place, and the underlying problem persists.
Asai et al.'s Self-RAG work, published in 2023, introduced a meaningful step toward generation that is aware of its own reliability. By training a model to emit reflection tokens that signal when retrieval is needed and evaluate the quality of its own output, the authors demonstrated that the generator need not be a passive consumer of retrieved context. It can participate in the pipeline's quality control. The more recent work on agentic systems has continued building in that direction.
LLM selection, in a well-designed pipeline, is a late-stage swappable variable. The modular architecture principle established at the outset has a practical payoff here: teams can upgrade the generator as better models emerge without rebuilding retrieval and augmentation. The market incentive runs the other way, though. Model selection gets treated as the primary architectural decision when experience suggests it is, in fact, the most easily changed one.
Structured outputs deserve more attention at the production stage than they typically receive. JSON outputs with citations, step-by-step reasoning traces, and formatted source attributions are easier to validate programmatically and easier to integrate into downstream systems. The instinct to produce fluent natural language is correct for user-facing applications; it should not preclude designing for machine-readable output where the pipeline extends beyond the user interface.
Handling time-sensitive queries when the indexed corpus is already stale
Vector store freshness depends entirely on ingestion frequency, which means it compounds on every delay upstream. Ingestion lag plus embedding time plus index update time adds up: "real-time" RAG over a private corpus is rarely real-time in practice. This is a structural constraint, and framing it as an engineering problem with a clever solution tends to obscure it rather than resolve it.
For stable internal knowledge bases, the constraint is manageable. HR policies, product documentation, and historical contracts change slowly. A corpus indexed on a reasonable cadence covers the relevant ground, and the lag is acceptable.
For fast-moving domains, it is not. Financial markets, regulatory changes, competitive intelligence, and breaking news require the retriever to access information that may have been published hours or minutes ago. No ingestion pipeline, however aggressively scheduled, can cover content that has not yet been crawled.
Work on temporal grounding in RAG, including the TimeRAG line of research that emerged in 2025, explored mechanisms for improving how systems handle time-sensitive queries. The fundamental structural constraint remains: pre-indexed documents cannot cover what has not yet been ingested. Temporal grounding improvements are meaningful at the margin; they do not resolve the underlying architecture.
The practical resolution is routing. Some queries belong to the indexed corpus; some require a live source. Production systems increasingly implement both, with routing logic that determines which source to hit based on query type. That routing logic is itself a design problem worth solving deliberately rather than defaulting to one path for all queries and hoping it generalizes.
Connecting RAG pipelines to the live web via search APIs
A web search API designed for AI systems differs from a consumer search engine in ways that matter architecturally. It returns structured, machine-readable results: titles, URLs, extracted text, metadata, stripped of advertisements and HTML boilerplate. In pipeline terms, it functions as an alternative retriever, replacing or supplementing the vector store step for queries where the relevant context lives on the open web.
Microsoft's retirement of the public Bing Search API in 2025 disrupted pipelines that had been built on it. That transition was an instructive, if painful, demonstration of how infrastructure choices made early in a pipeline's life carry long-term consequences and of why purpose-built AI APIs are specifically designed to reduce that availability risk.
The LLM-native search API field has developed meaningful specializations. Tavily is purpose-built for RAG pipelines, aggregating multiple sources per call using AI-based ranking, which reduces the post-processing burden on the pipeline. Perplexity's API handles natural-language-understanding-heavy queries well and returns summarized answers rather than raw content, which changes the augmentation logic downstream. Google's Programmable Search Engine offers broad coverage, though it requires more post-processing before its outputs are usable as grounding context.
You.com's Research API occupies a distinct position in this field. It is designed specifically for AI grounding, delivering cited, real-time results in a format that reduces augmentation overhead. Its performance on the DeepSearchQA benchmark places it among the top options for pipelines that require both speed and verifiable accuracy. The Finance Research API extends this to a specialized domain, holding a leading benchmark position on FinSearchComp. For teams building pipelines over SEC filings, earnings data, or regulatory content, that specificity matters: cited accuracy in those applications is a compliance requirement, not a preference.
Evaluation criteria for web search API selection in a RAG context should be explicit rather than assumed. Latency at the p99 level matters more than median latency for user-facing applications. Freshness of results is the reason you are using a live API rather than a pre-indexed corpus in the first place. Whether output arrives pre-formatted for LLM consumption determines how much augmentation work the retrieved content requires before it is usable. Citation structure determines whether provenance is traceable. Privacy and data-retention posture is non-negotiable for any enterprise application handling sensitive queries.
Agentic RAG: when static pipelines are not enough for multi-step reasoning
Static RAG pipelines retrieve once and generate once. For single-hop questions, that is adequate. A user asks where the company's parental leave policy is; the pipeline retrieves the relevant document section, the model summarizes it. The architecture fits the task.
It does not fit queries that require synthesizing evidence across multiple sources, or reasoning over an intermediate answer before committing to a final one. A question like "how has the regulatory treatment of this product category changed over the last three years, and what does that imply for our compliance posture" requires sequential retrieval, intermediate synthesis, and evaluation of whether the accumulated evidence is sufficient before generating. The reason this fails in a static pipeline is structural: each retrieval pass surfaces context that changes what the next query should be, a dependency that a linear pipeline cannot express.
Agentic RAG embeds autonomous agents into the pipeline loop. The agent can decide to retrieve again if the first pass was insufficient, reformulate the query based on what it found, evaluate the quality of intermediate outputs, or invoke external tools to supplement the retrieved context. It is a feedback loop with decision points, and the agent's capacity to self-correct at each step is what makes multi-hop reasoning tractable.
The tradeoffs are real and worth taking seriously before committing to the architecture. Agentic systems are harder to debug than linear pipelines. Non-determinism compounds across agent steps, which means the same query can produce meaningfully different outputs across runs. Latency increases with each additional retrieval-and-reasoning cycle. For latency-sensitive applications, these costs can be prohibitive, and failures become harder to isolate than in a simpler linear design.
The design question is not whether agentic RAG is better than static RAG, but whether the query distribution of the target application requires it. Internal FAQ tools, document search, and single-topic summarization tasks do not. Research-intensive applications, multi-source financial analysis, and regulatory compliance queries often do. Matching the architecture to the query complexity is the decision, and it is one that benefits from actually examining the query logs rather than assuming.
What this progression makes visible is something implicit throughout: every stage in a RAG system is a variable, and every variable compounds on the others. Static pipelines make that compounding legible because there are few moving parts. Agentic loops make it non-linear. Either way, the teams that build systems that hold up are the ones who have been burned by that compounding at least once and designed with it in mind, not around it.


