Below is a brand‑new article built from the information in your post, with no new ideas added and the original source cited at the end.
—
# Building the Generation Brick of an Enterprise RAG System
In the Enterprise Document Intelligence series, generation is the fourth brick that completes an end‑to‑end RAG pipeline. With the first three bricks—document parsing, question parsing, and retrieval—already in place, generation is responsible for producing a typed, verifiable answer by orchestrating prompts, models, and traces.
## The Dispatcher: One Prompt Per Question Shape
A common anti‑pattern in RAG systems is the “mega‑prompt,” where a single prompt contains every conditional clause for every possible answer shape. This approach wastes tokens, is hard to debug, and creates maintenance chaos as the system evolves.
The dispatcher design replaces this with a **contract‑first** approach:
1. A `ParsedQuestion` arrives from the question‑parsing brick.
2. The dispatcher selects the appropriate schema from `ANSWER_REGISTRY` using `expected_answer_shape`.
3. It composes a system prompt from a stable **BASE** plus lightweight **fragments** driven by the question and its constraints.
4. It builds a user prompt that contains the original question, extracted keywords, and labeled passage lines.
5. It calls the model once, persists the full raw response to the trace, and returns a typed answer.
Adding a new answer shape now requires only adding a single fragment; new constraints are similarly additive. This keeps templates small, avoids combinatorial duplication of cross‑cutting rules, and makes behavior easy to audit.
## From Brief to Prompt in Practice
The `ParsedQuestion` carries a rich, relational structure (nesting Pydantic models) that includes:
– `expected_answer_shape`
– `generation` (format constraints, disambiguation, must‑distinguish rules)
– `keywords`
– `retrieval` and `generation` preparations for the next bricks
– `structural_hints` such as page or sheet references that scope retrieval
Because scope information travels with the question, the retrieval step can filter early without special pipeline flags. This works uniformly across document formats, from long PDFs to single‑page sources such as CVs or invoices. On one‑page documents, the same pipeline still applies; a page hint keeps the context window tightly focused while the schema and fragments ensure correct extraction.
The system prompt is composed of:
– **BASE**: rules that apply to every call (cite sources, use `GLOBAL_LINE` for line numbers, declare when evidence is missing or conflicting)
– **Shape fragments**: generation strategies for `text`, `amount`, `date`, `list`, `table`, `boolean`
– **Constraint fragments**: format rules, distinction pairs, and disambiguation text
This composition means that changing how amounts are rendered touches only the `amount` fragment, and new domain constraints can be added in one place without touching existing shapes.
## Prompt Assembly and the Call Pattern
The user prompt includes:
– The original question
– Echoed keywords
– Tab‑separated passage lines keyed by `GLOBAL_LINE`
Two design choices matter here:
– **Combined vs sequential calls**: when multiple retrieval results arrive, the system can process them all in one model call (good for synthesis across chunks) or sequentially with early termination when a confident answer appears in the top chunk (optimal for single‑fact questions). This decision is made upstream in question parsing, not in generation.
– **Margin management**: keeping input token usage below 70–80% of the model window preserves quality. If usage consistently runs high, the fix belongs to retrieval or schema design, not to generation.
## Validation and Traceability
Every generated answer is validated against its schema (Article 8A) in Article 8C. The full raw model response is persisted alongside metadata such as:
– Model and version
– Request and response IDs
– Token usage
– Fragments applied
Storing the complete payload enables forensic analysis months later (for example, when a user claims an answer changed) and supports cost and caching analysis.
## Multi‑Field Answers and Per‑Field Evidence
As requirements grow, teams often need to extract many typed fields rather than a single summary. The pattern scales by pushing the `AnswerWithEvidence` wrapper down to the field level: each field carries its own typed value and exact source span. A single structured call fills the entire field set, and post‑validation can verify every quoted substring against the source lines.
This field‑level evidence is the same primitive used for item‑level extraction today; extending it to fields keeps the system consistent and audit‑ready.
## Dynamic Few‑Shot Examples
At query time, the dispatcher can inject a small set of validated examples retrieved from a curated bank. These few‑shot examples:
– Correct recurring format mistakes (e.g., currency normalization)
– Teach extraction behavior for tricky sources
– Clarify ambiguous question parsing
The examples are chosen by similarity, included just before the main call, and kept optional to avoid introducing noise or leaking sensitive data.
## Conclusion
The generation brick turns a `ParsedQuestion` into a typed answer by:
– Using the registry to select a schema
– Composing BASE plus fragments into a precise system prompt
– Building a focused user prompt
– Calling the model once
– Persisting the full response for traceability
Nothing is left to the model’s creativity; validation enforces the contract before any answer is released. This engineered flow delivers bounded costs, reproducible behavior, and an audit trail that remains trustworthy over time.
—
**Sources**
The information above is derived from the original article series:
Insight Media Group contributor. “Part 8B (generation): the prompt‑assembly part, inside Part II (the four bricks).” *Enterprise Document Intelligence series*, 2026. (https://contributor.insightmediagroup.io/2026/07/enterprise-document-intelligence-part-8b-generation-brick/)



