Optimizing the RAG Pipeline: From Chunking and Recall to Reranking and Generation Accuracy
The most common predicament after a RAG system launches: the knowledge base contains a perfectly correct answer, yet the bot still answers wrongly. Should you change chunking? Swap the embedding model? Tune the prompt? Blame the LLM? If you cannot say which link failed, optimization is just gambling.
This post is part of an experience-sharing series for AI customer service product managers and developers, drawn from hands-on practice as an intelligent service product manager at a large general e-commerce platform. In this post I want to share how to design the RAG system — optimizing chunking, recall, reranking, and answer generation — when the bot must simultaneously use the product knowledge base, platform rule base, and real-time order data, and how to solve the two classic failure modes of "retrieval is right but the answer is wrong" and "knowledge is right but business state is stale".
The value of this design is that it decomposes RAG from "we plugged in a vector database" into an attributable, stage-by-stage optimizable engineering pipeline.

Two Key Points in RAG Pipeline Optimization
- Full-pipeline RAG optimization: levers for all four stages — chunking, recall, reranking, generation — not just embedding tweaks.
- Attribution ability: separating "retrieval problems" from "generation problems" and placing each Bad Case in a specific stage.
The Overall Design
Step 1: Knowledge chunking optimization
- Chunk by semantic structure, not fixed length: platform rules split by "clause", product knowledge by "attribute group"; every chunk keeps metadata like title, category, and applicable scope for downstream filtering and arbitration.
- Store tabular knowledge (shipping fees, compensation standards) in structured form and inject it structurally after retrieval, so the model never misreads rows and columns — LLMs are far worse at reading tables than people assume.
Step 2: Multi-path recall and reranking
- Hybrid retrieval: vector semantic recall + keyword/inverted-index recall, covering both "similar meaning" and "literal match"; order-related queries run entity recognition first and route to real-time APIs — order status never comes from a static knowledge base.
- A reranking model orders candidate chunks by "relevance to the question + freshness + source priority"; stale knowledge is filtered or down-weighted at the retrieval layer, never allowed to reach generation.
Step 3: Answer generation control
- Constrain the model during generation: answer only from the given chunks, cite sources, and clearly say "not found" when coverage is insufficient — never fabricate a confident wrong answer.
- Numeric answers (refund amounts, compensation) are computed by the rule engine or an API and injected into a template; the model only organizes language, never does arithmetic on money. Letting an LLM calculate refunds is the number-one source of hallucination incidents.
Step 4: Solving the two classic failure modes
Retrieval is accurate but the answer is wrong — a generation problem. Fixes: tighten generation constraints (citations must come from chunks); add post-verification of key facts — compare numbers and order IDs in the answer against chunks or API results, and trigger retry or fallback on mismatch; build evaluation sets from high-frequency errors for regression.
Knowledge is correct but business state is stale — a data-source problem. Fixes: real-time state always goes through APIs, never cached; knowledge entries carry freshness metadata and are filtered against current business state at retrieval; run a state-freshness check before generation, with state-change events proactively invalidating knowledge.
Two methodologies worth stating
An "answer post-verification" layer: after generation, verify key facts with rules — the last line of defense before hallucination reaches the user. Low cost, certain benefit; it should be on by default.
Four-way Bad Case attribution: not recalled, recalled but not ranked, ranked but generated wrong, or data itself stale — four categories calling for entirely different fixes. Classify first, then act; that is the precondition for RAG optimization that is not guesswork.
How This Maps to Bytedesk
Bytedesk, the open-source AI customer service system (GitHub, site), covers the main stages of this pipeline: the knowledge base supports multi-format import and category management so chunks can follow business semantics; retrieval supports hybrid vector + keyword recall; the AI Agent can route order-type real-time queries to business APIs instead of static knowledge; generation enforces citation constraints through prompting; and session quality inspection and statistics accumulate Bad Cases for regression evaluation. Answer post-verification and state-freshness checks are advanced capabilities that can be built on Bytedesk's open Agent and tool-call mechanisms.
Key Takeaways
- Chunk by semantic structure, not fixed length; store tables structurally; keep metadata with every chunk.
- Hybrid vector + keyword recall; real-time business state via APIs, never the knowledge base; rerank on relevance, freshness, and source priority.
- In generation: answer only from hits, cite sources, prefer "not found"; rules compute numbers, the model only writes prose.
- "Retrieved right but answered wrong" is a generation problem; "knowledge right but state stale" is a data-source problem — the fixes differ completely.
- Answer post-verification + four-way Bad Case attribution turn RAG optimization from luck into engineering.
