As of November 2026 · CODLAB Team
Direct answer: Reliable Retrieval-Augmented Generation (RAG) depends far more on retrieval engineering than on picking a larger language model. Production-grade accuracy requires structure-aware chunking with metadata, hybrid lexical and vector search inside PostgreSQL, cross-encoder reranking, strict distance thresholds for honest abstention, and verifiable sentence-level grounding across multilingual (DE/IT/EN) document repositories.
Why Applied RAG Research Starts Upstream of the Language Model
Whenever an internal document assistant returns an incomplete or misleading answer in an enterprise trial, teams almost instinctively blame the language model. In our applied engineering and research methodology at CODLAB on Sovereign AI & Data, root-cause diagnostics start one stage earlier — in the retrieval layer: a model can write fluent prose and still fail factually whenever retrieval supplies truncated table rows, superseded policy revisions, or semantically similar paragraphs that apply to a completely different machine or contract type.
Retrieval-Augmented Generation was formalized by Patrick Lewis et al. (2020) to pair parametric neural generation with an explicit, updatable non-parametric document index (Lewis et al., NeurIPS 2020, arXiv:2005.11401). For mid-sized industrial, legal, and healthcare organizations, that architectural split reframes the core research question. Instead of asking which model memorized the most general trivia, we examine under which indexing, filtering, and ranking conditions a pipeline reliably surfaces authorized passages — and when it deterministically abstains because the corpus lacks evidence.
Separating retrieval diagnostics from generation diagnostics is a foundational principle across our CODLAB Engineering Notes, starting with Evaluating RAG: Separating Retrieval and Answers. Here we connect each stage of a modern RAG architecture and show how engineering teams and technical decision-makers can test design choices on their own documents.
1. Structure-Aware Chunking Instead of Arbitrary Character Windows
Every RAG pipeline begins by splitting documents into retrievable passages (chunks). Rapid prototypes typically slice text every N tokens with a fixed overlap. Across real-world Standard Operating Procedures, engineering manuals, and legal contracts, naive windowing systematically breaks the exact relationships needed for a correct answer:
- Tables and specification matrices: Separating column headers from lower rows strips tolerance values, torque figures, or statutory deadlines of their units and operating conditions.
- Rule-and-exception pairs: When a general rule sits at the bottom of section 4.1 and its decisive exclusion sits at the top of section 4.2, a fixed token boundary creates a standalone chunk that implies the opposite of the documented policy.
- Document provenance: Without attached structural metadata, an isolated paragraph no longer reveals whether it belongs to an approved operating manual or an obsolete draft.
For complex enterprise documents, our target architecture designs chunking as a structure-preserving transformation rather than a character counter: heading breadcrumbs (Document Title → Section → Subsection) and intact tables with column headers remain explicitly attached to each passage. Here we draw a clear distinction between that enterprise target architecture and the intentionally lightweight reference assistant (ragbot) in the codlab.de codebase (whose public chat demo has been switched off since October 2026, with code and data retained): because that module was designed to index concise web pages, its ingestion command (rag_ingest) flattens HTML to plain text and splits along sentence and character boundaries with overlap, without storing separate heading-hierarchy metadata. What the ragbot codebase implements directly is deterministic SHA-256 idempotency: its Django DocumentChunk model stores source_url, source_title, chunk_index, and an indexed content_hash, skipping unchanged passages on re-ingestion while selectively re-embedding modified sections.
Rather than claiming a universal "best chunk size," we treat segmentation granularity as an empirical parameter to be tested per corpus: narrow chunks sharpen single-fact precision at the cost of cross-paragraph context, whereas wide chunks preserve narrative continuity while diluting dense vector representations.
2. Dense Vectors, Lexical Full-Text Search and Hybrid Retrieval in PostgreSQL
Dense vector embeddings (Dense Passage Retrieval, see Karpukhin et al., 2020, arXiv:2004.04906) map text into continuous vector space so that paraphrases land close together. If a technician asks about "restarting after an emergency stop," vector similarity can retrieve a section titled "resetting the safety relay chain." For approximate nearest neighbor (ANN) search inside PostgreSQL, CODLAB uses the pgvector extension backed by HNSW graphs (Hierarchical Navigable Small World, see Malkov & Yashunin, arXiv:1603.09320).
In our reference assistant (ragbot), this vector layer is declared directly on the Django ORM model (1536 dimensions, HnswIndex with m=16, ef_construction=64, and vector_cosine_ops) and executes pure cosine similarity without full-text columns or multi-tenant fields. In industrial and regulatory corpora, however, pure vector search exhibits a well-known blind spot: exact identifiers such as part numbers (BG-4410-RevC), DIN/ISO standards (DIN EN ISO 13849-1), error codes, or statutory clauses often map to imprecise semantic neighbors.
In our enterprise target architecture, we therefore extend this PostgreSQL foundation into hybrid retrieval without introducing an external vector database whose permissions and backups can drift out of sync with the main application. Within a single PostgreSQL instance, two complementary retrieval paths execute under identical access controls:
- Semantic path: Approximate nearest-neighbor cosine search via
pgvectorto capture conceptual similarity and paraphrases. - Lexical path: Native PostgreSQL Full Text Search using
tsvector,tsquery, and GIN indexes (PostgreSQL Documentation, Chapter 12: Full Text Search), combined with exact metadata predicates for part codes, docket references, and revision status.
Candidate lists from both paths are merged through rank-based fusion (such as Reciprocal Rank Fusion) prior to prompt assembly. How tenant filters interact with PostgreSQL query planners and HNSW index traversal — and why selective WHERE clauses can silently reduce recall unless ef_search and iterative index scans (which require pgvector ≥ 0.8.0) are verified — is examined in detail in our note pgvector with Django: Checking Filters and Recall.
# Enterprise multi-tenant target pattern (extending the single-corpus reference ragbot model):
# Enforce authorization filters deterministically BEFORE cosine ranking and distance cutoff
allowed_chunks = EnterpriseDocumentChunk.objects.filter(
tenant_id=active_tenant.id,
collection__in=user_permitted_collections,
).exclude(embedding=None)
candidates = (
allowed_chunks
.annotate(distance=CosineDistance("embedding", query_vector))
.order_by("distance")[:candidate_pool_size]
)
verified_hits = [c for c in candidates if c.distance is not None and c.distance <= max_distance]
3. Cross-Encoder Reranking and the "Lost in the Middle" Effect
Because modern language models accept large context windows, teams are often tempted to bypass careful ranking altogether: why not stuff 40 to 60 retrieved chunks into the prompt and let the generator sort them out? Empirical work by Nelson F. Liu et al. demonstrates why that shortcut degrades both quality and economics (Liu et al., "Lost in the Middle: How Language Models Use Long Contexts", arXiv:2307.03172): language models attend reliably to evidence at the very beginning and end of long prompts, while accuracy drops sharply when the decisive fact sits buried in the middle. Meanwhile, prompt latency, KV-cache memory consumption, and exposure to conflicting passages all climb.
A two-stage retrieval pipeline resolves this trade-off through cross-encoder reranking (Nogueira & Cho, 2019, arXiv:1901.04085):
- Stage 1 (fast candidate recall): Hybrid search narrows the authorized corpus down to a bounded candidate pool (for instance top-25 to top-40); actual query latency is not a fixed constant, but depends on corpus size, HNSW parameters (
m,ef_search), filter selectivity, and hardware, and must be measured on the target dataset. Because bi-encoders embed queries and passages separately, fine-grained token interactions are not yet evaluated. - Stage 2 (joint cross-attention reranking): A compact cross-encoder scores each query–passage pair jointly across all transformer layers, producing a calibrated relevance ranking so only the highest-signal, deduplicated passages enter the generation prompt.
Equally critical is a hard distance or relevance cutoff. In our reference assistant (ragbot) in the codlab.de codebase, any candidate above a cosine distance of 0.62 (MAX_DISTANCE = 0.62 on top-5 retrieval) is discarded. Whenever zero passages survive that threshold, the backend does not call the generative model to improvise; it returns a deterministic refusal inviting a direct human clarification. In business workflows, controlled abstention is an essential engineering safeguard.
4. Citations, Grounding and Why a Footnote Marker Alone Proves Little
Displaying bracketed markers such as [1] and [2] has become standard across enterprise chat interfaces. Our reference assistant (ragbot) in the codlab.de codebase numbers retrieved blocks, deduplicates source URLs, and instructs the generator to rely strictly on the supplied context. Even so, applied research requires a sharp distinction between citation formatting and substantive faithfulness (grounding).
A model can attach a valid link [1] to a sentence while omitting a crucial safety caveat stated in source [1], or blending two separate clauses that apply to different scenarios. Furthermore, retrieved documents themselves must be treated as untrusted data inputs capable of carrying conflicting or manipulative instructions — a boundary we analyze in RAG: Sources as Untrusted Data.
During evaluation, we therefore verify grounding at the atomic-claim level: every factual statement in a candidate answer is decomposed and checked against the exact text of the cited passage. If a qualifying condition is dropped, the response is scored as unfaithful regardless of whether the hyperlink resolves to the right file.
5. Multilingual Corpora (DE/IT/EN) in European Mid-Market Operations
Across Central Europe, mid-sized engineering and service firms routinely operate across three languages at once: internal quality standards are written in German, component test protocols from Northern Italian suppliers arrive in Italian, and electronics or software manuals are maintained in English. Monolingual RAG setups break down here in two ways: lexical matching fails across languages, and ad-hoc translation of domain terms distorts regulatory or technical meaning.
Our applied research evaluates three composable patterns for DE/IT/EN repositories:
- Multilingual dense embeddings paired with explicit language tags: Cross-lingual embedding models align semantically equivalent passages across German, Italian, and English while preserving document language metadata for auditability.
- Glossary-constrained query expansion: Approved company terminology maps domain-specific abbreviations and component names across languages before hybrid search runs, preserving exact lexical recall.
- Source-transparent generation: Answers are formulated in the user's query language while explicitly indicating the original language and title of the cited source passage so specialists can inspect the authoritative wording. Additional engineering checks for multilingual routing and metadata are detailed in Django i18n: Verifying Language, URLs and Metadata.
6. Synthetic Evaluation Blueprint for Technical Decision-Makers
Methodological note: The scenario below is entirely synthetic and serves solely to illustrate our experimental protocol; it does not describe a client engagement or claimed customer metrics.
Suppose a mid-sized machinery manufacturer wants field engineers to query approved maintenance manuals (German), subsystem datasheets (English), and supplier inspection sheets (Italian). Rather than indexing entire file shares blindly, we structure a reproducible pilot experiment:
- Curate a bounded test corpus: Select 30 to 50 representative approved documents, intentionally including two revision levels of one manual and one restricted document to verify access-control enforcement.
- Define ground-truth question–passage pairs: Assemble realistic engineering queries covering exact error codes, cross-lingual lookups, conflicting document versions, and out-of-scope questions that require abstention.
- Compare retrieval configurations side by side: Measure pure vector search against hybrid search plus reranking using separate metrics for passage recall@k, refusal accuracy on unanswerable prompts, and claim-level faithfulness.
- Document release criteria: Production rollout proceeds only when retrieval reproducibly surfaces the expected evidence and database-level permission boundaries pass negative testing.
You can review the scope of such structured pilots on our Document Assistants page. To discuss how a verifiable RAG architecture fits your own document landscape, reach out for an initial technical conversation via Contact & Project Inquiry.
Continue in this research series: For how to evaluate retrieval and answer faithfulness reproducibly across stochastic decoding runs, read our companion study on multi-run metrics for stochastic AI systems; for architectural defenses against poisoned passages, see our analysis of Indirect Prompt Injection and source boundaries.
Verified Primary Sources
- Lewis, P. et al. (2020): Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (NeurIPS 2020, arXiv:2005.11401).
- Karpukhin, V. et al. (2020): Dense Passage Retrieval for Open-Domain Question Answering (EMNLP 2020, arXiv:2004.04906).
- Malkov, Yu. A. & Yashunin, D. A. (2016/2018): Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs (arXiv:1603.09320).
- Nogueira, R. & Cho, K. (2019): Passage Re-ranking with BERT (arXiv:1901.04085).
- Liu, N. F. et al. (2023/2024): Lost in the Middle: How Language Models Use Long Contexts (TACL, arXiv:2307.03172).
- PostgreSQL Global Development Group: PostgreSQL Documentation — Chapter 12: Full Text Search and pgvector v0.8.0.
Frequently Asked Questions (FAQ)
Long context windows remain vulnerable to the Lost-in-the-Middle effect, increase per-query latency and GPU KV-cache memory, and cannot replace database-enforced document permissions, revision filtering, and auditable retrieval across thousands of files.
Usually not. PostgreSQL combined with pgvector (HNSW indexing) and native Full Text Search runs semantic vectors, lexical search, relational metadata filters, and tenant permissions inside one transactional database.
A distance cutoff filters out weak matches before generation. When no authorized chunk meets the threshold, the backend returns a deterministic refusal rather than letting the language model speculate without evidence.
Multilingual embeddings align semantic meaning across languages while an approved domain glossary preserves exact part and process terminology. The assistant answers in the query language while citing the exact original-language source passage.
Related operational guide (German edition): KI-Dokumentensuche auf Ihren eigenen Daten: Was vor dem Pilot geklärt sein sollte.