Retrieval engineering · Advanced

Build a financial RAG agent and compare retrieval approaches

Собираем финансового RAG-агента и сравниваем retrieval-подходы
Temporary fallback cover; replace in editorial pass.

A practical, evidence-bounded workflow for building a financial assistant that retrieves the right filing passages, cites its evidence, and measures quality, latency, and cost before anyone trusts it with real questions.

40 min read Updated 23 November 2026 By Agent Lab Journal

What you will build

This article describes a reproducible prototype for a retrieval-augmented generation (RAG) assistant over financial documents. The prototype accepts a natural-language question, retrieves candidate passages using several strategies, optionally applies a reranker, asks a language model to answer only from the selected evidence, and records enough metadata to evaluate the whole pipeline.

The comparison is deliberately small and explicit. You will test:

  • lexical retrieval with BM25;
  • dense retrieval with embedding vectors;
  • hybrid retrieval that combines lexical and dense rankings;
  • hybrid retrieval followed by a cross-encoder reranker.

The article does not claim benchmark results for a particular model, corpus, cloud, or customer. The commands and schemas are implementation patterns. You must run them against your own documents, hardware, model versions, and labeled questions before drawing conclusions.

Why financial search is unusually unforgiving

Financial questions often contain several constraints at once: a company, a reporting period, a document type, a metric, a unit, and a comparison. “What was operating cash flow in 2023?” is not equivalent to “What was operating cash flow in the first quarter of 2023?” A result from an earnings presentation may also answer differently from a result in an audited annual report.

Exact terms matter. Ticker symbols, accounting labels, section headings, table row names, dates, and footnote references are often more useful than semantic similarity. At the same time, a user may ask with a phrase that never appears verbatim in the source. “How much cash did the company burn?” may require retrieving “net cash used in operating activities.”

This is why a single retrieval method is a weak default. Lexical retrieval is strong at exact identifiers and rare phrases. Dense retrieval is strong at paraphrases. A reranker can inspect the interaction between the query and each candidate passage, but it adds compute and latency. The correct choice is an empirical result for a defined workload, not a universal claim.

Define the evidence boundary first

Before indexing anything, write down what the assistant is allowed to answer. A safe prototype should have a narrow source boundary, such as public annual reports, quarterly reports, earnings releases, and investor presentations that you have downloaded and hashed locally.

Store provenance with every chunk. At minimum, keep:

{
  "document_id": "acme_2023_annual_report",
  "source_uri": "https://example.invalid/source",
  "document_type": "annual_report",
  "issuer": "ACME",
  "period_end": "2023-12-31",
  "page": 87,
  "section": "Liquidity and capital resources",
  "content_hash": "sha256:...",
  "retrieved_at": "2026-11-23T00:00:00Z"
}

The URI above is a placeholder, not a source citation. Replace it with the URI or internal file identifier for a document you actually possess. Do not put credentials, private customer data, account numbers, or unpublished material into a benchmark corpus.

A useful answer contract is: if the selected evidence does not support the requested issuer, period, metric, and unit, the assistant must say that it cannot verify the answer. “I do not have enough evidence” is a valid output and should be scored as such.

Prepare a small, versioned corpus

Reproducibility starts with a manifest. Keep source files outside the application image when practical, and record the exact preprocessing configuration used to create the index.

corpus/
  raw/
    acme_2023_annual_report.pdf
    acme_2024_q1_earnings_release.html
  normalized/
  chunks.jsonl
  manifest.json
  questions.jsonl
  runs/

Example manifest:

{
  "corpus_version": "2026-11-23-demo-01",
  "files": [
    {
      "document_id": "acme_2023_annual_report",
      "path": "raw/acme_2023_annual_report.pdf",
      "sha256": "replace-with-computed-hash"
    }
  ],
  "chunking": {
    "target_tokens": 450,
    "overlap_tokens": 75,
    "preserve_tables": true
  }
}

Extract text with a parser that preserves page numbers and table boundaries. Do not silently flatten every table into a paragraph: a row label without its column heading can change the meaning of a number. If your parser cannot preserve a table reliably, mark that table as unsupported or use a table-aware representation and test it separately.

Chunking: preserve the unit of evidence

Chunking is not only a token-budget decision. It defines what the model sees as one piece of evidence. A chunk that combines a management discussion with an unrelated accounting policy may retrieve well but provide ambiguous support. A chunk that splits a table from its header may be short and precise but unusable.

Start with section-aware chunks of roughly 300–600 tokens and a modest overlap. Treat these values as configuration, not as a fact about all financial documents. Run a small sensitivity test with at least two chunk sizes while keeping the corpus and question set fixed.

Each chunk should include context fields in the text sent to retrieval and generation. For example:

[issuer=ACME]
[document=annual_report]
[period_end=2023-12-31]
[page=87]
[section=Liquidity and capital resources]

Net cash used in operating activities was ...

These labels help lexical search and make citations easier to render. They are not a substitute for metadata filters. Filter by issuer or period when the question makes those constraints explicit, and log whether the filter was applied.

Build four retrieval baselines

1. Lexical retrieval with BM25

BM25 scores documents using term frequency, inverse document frequency, and document-length normalization. It is a useful baseline because it is interpretable and often strong for ticker symbols, dates, rare accounting terms, and exact table labels.

Normalize case and whitespace, but be conservative with stemming and stop-word removal. “Loss” and “losses,” or “current” and “non-current,” may carry important meaning in financial text. Test the analyzer on examples rather than assuming a generic web-search configuration is suitable.

2. Dense retrieval with embeddings

Embeddings represent chunks and queries as vectors. A vector index returns passages that are close under a similarity function, usually cosine or dot-product similarity. Dense retrieval can connect paraphrases, but it may underweight exact identifiers or return passages that are semantically related but from the wrong period.

Record the embedding model identifier, dimension, normalization setting, batch size, and index configuration. Changing any of these can invalidate a comparison. If you use an external embedding service, record model version and request timestamps without storing sensitive query content in logs.

3. Hybrid retrieval

Hybrid retrieval combines lexical and dense candidates. A practical implementation retrieves k_lexical and k_dense candidates independently, then combines their ranks using reciprocal rank fusion (RRF):

RRF(d) = Σₘ 1 / (k + rankₘ(d))

The constant k is a tunable fusion parameter. It is not the same as the final number of passages shown to the language model. Keep candidate depth and final context size separate in your configuration.

4. Hybrid retrieval plus reranking

A cross-encoder reranker reads the query and each candidate together and assigns a relevance score. Use it after broad candidate generation, not as a replacement for candidate generation. The reranker can improve ordering, but it cannot recover a passage that no first-stage retriever returned.

Log both stages. Otherwise, a higher answer score may hide a first-stage recall regression that will become visible when questions get harder.

Reference pipeline

Keep the system modular so retrieval approaches can be swapped without changing generation. A minimal interface looks like this:

class Retriever:
    def search(self, query: str, filters: dict, limit: int) -> list[dict]:
        """
        Return chunks with:
        - chunk_id
        - text
        - metadata
        - score
        - retriever_name
        """
        raise NotImplementedError

The application flow should be explicit:

  1. Parse the question and extract optional constraints.
  2. Apply only constraints that are present and validated.
  3. Run the selected first-stage retriever or retrievers.
  4. Fuse candidates when using hybrid retrieval.
  5. Rerank the candidate set when configured.
  6. Trim evidence to a context budget while preserving provenance.
  7. Generate an answer under a strict evidence-only instruction.
  8. Validate citations and persist the run record.

Example configuration:

retrieval:
  mode: hybrid_rerank
  lexical_top_k: 40
  dense_top_k: 40
  fusion: rrf
  fusion_k: 60
  rerank_top_k: 20
  context_top_k: 6
  filters:
    allow_issuer_filter: true
    allow_period_filter: true

generation:
  temperature: 0
  max_output_tokens: 500
  require_citations: true
  abstain_if_evidence_insufficient: true

The values are starting points for an experiment, not measured recommendations. Tune them on a development split and report the final values with the benchmark.

Use an evidence-first answer prompt

Prompting cannot repair bad retrieval, but it can reduce unsupported synthesis. Pass the model numbered evidence blocks with stable identifiers:

SYSTEM:
You answer questions about financial documents.

Rules:
1. Use only the evidence blocks supplied in this request.
2. If the evidence does not establish the issuer, period, metric, and unit,
   say that the answer cannot be verified from the supplied documents.
3. Do not infer a number from a chart, table, or neighboring period unless
   the evidence explicitly supports it.
4. Cite every material claim as [chunk_id, page].
5. Distinguish reported values from calculations.

USER:
Question: {question}

Evidence:
{numbered_evidence_blocks}

Add a post-generation validator. It should check that cited chunk identifiers were actually supplied, that citations contain known page metadata where available, and that numeric claims have at least one citation. This validator is a guardrail, not a fact checker: a model can cite a real but irrelevant passage.

Create a labeled evaluation set

Retrieval quality cannot be inferred from a few attractive demos. Build a question set that reflects the failure modes you care about. Each question should have a source-level label, not merely an expected answer string.

{
  "question_id": "q-001",
  "question": "What was operating cash flow in 2023?",
  "constraints": {
    "issuer": "ACME",
    "period_end": "2023-12-31",
    "unit": "USD"
  },
  "relevant_chunk_ids": [
    "acme_2023_annual_report:p87:c03"
  ],
  "answerable": true,
  "gold_answer": "Record the verified answer during annotation.",
  "annotation_notes": "Use the consolidated cash-flow statement."
}

The example gold answer is intentionally a placeholder. Do not copy it into a benchmark as if it were a verified financial fact. Annotators should record the value from the source document and retain the page and table references.

Include several question families:

  • exact lookup: a named metric in a named period;
  • paraphrase: a user phrase that differs from the source wording;
  • comparison: one metric across two periods;
  • table question: a row and column intersection;
  • footnote question: a definition or accounting policy;
  • ambiguous question: missing issuer, period, or unit;
  • unanswerable question: the corpus does not contain enough evidence.

Split by question, not by chunk. Keep a development set for tuning and a held-out test set for the final comparison. If the corpus is small, document the limitation and use repeated annotation or cross-validation rather than presenting a tiny test set as definitive.

Measure retrieval separately from answers

For each query, save the ranked list returned by every retrieval variant. At minimum, calculate:

  • Recall@k: whether a labeled relevant chunk appears in the first k results.
  • Mean reciprocal rank (MRR): how early the first relevant result appears.
  • nDCG@k: how well graded relevance is ordered when several passages matter.
  • filter accuracy: whether issuer and period constraints were respected;
  • candidate count: how many chunks reached the reranker and generator.

Use graded labels when useful: 2 for directly sufficient evidence, 1 for supporting but incomplete context, and 0 for irrelevant or contradictory content. Define the labels before evaluation and keep the rubric with the benchmark.

Conceptual pseudocode:

for question in test_questions:
    for variant in variants:
        ranked = variant.retrieve(question.text, question.constraints)
        metrics[variant].update(
            recall_at_k=recall_at_k(ranked, question.relevant_chunk_ids, k=5),
            mrr=reciprocal_rank(ranked, question.relevant_chunk_ids),
            ndcg=ndcg(ranked, question.relevance_labels, k=10)
        )

Do not average away the hard cases. Report aggregate metrics and a slice by question family, document type, period, and answerability. A method that improves average recall by returning more loosely related passages may still harm table questions or increase generation cost.

Measure answer quality with a rubric

Retrieval metrics do not guarantee a correct answer. Evaluate generated answers against the source evidence using a rubric that separates correctness from citation behavior:

  1. Answer correctness: Is the value, direction, comparison, and unit correct?
  2. Evidence support: Does the cited passage support the claim?
  3. Completeness: Are all material parts of the question answered?
  4. Period and entity fidelity: Is the answer about the requested issuer and period?
  5. Abstention quality: Does the assistant decline when the corpus is insufficient?
  6. Format quality: Are citations and uncertainty statements usable?

Use human review for the final test set when the decision matters. An automated judge may help triage, but it should not be treated as ground truth without calibration against human labels. If you use a model judge, record its model identifier, prompt, temperature, and rubric version.

A useful failure label is “retrieval error” when the needed passage was absent from the context, versus “generation error” when the needed passage was present but the answer was wrong or unsupported. This distinction tells you whether to improve indexing and ranking or the answer policy.

Measure latency and cost as distributions

Report more than an average. Record end-to-end latency and stage timings:

{
  "question_id": "q-001",
  "variant": "hybrid_rerank",
  "timings_ms": {
    "query_parse": 4,
    "lexical_search": 18,
    "dense_search": 31,
    "fusion": 1,
    "rerank": 94,
    "generation": 680,
    "total": 828
  },
  "usage": {
    "input_tokens": 3200,
    "output_tokens": 180
  }
}

The numbers above are illustrative placeholders, not measured results. Replace them with values captured from your own run. Report median, p95, and error rate for each variant. If the system uses remote services, separate network time from service processing time when the API exposes that information.

Cost accounting should be formula-based and versioned. For a hosted model, use the provider’s current price table at the time of your experiment and store the exact model and pricing date. For local inference, include a declared allocation for compute, storage, and operational overhead if you need a total-cost estimate. If you cannot verify a price, report token counts and omit a monetary total rather than inventing one.

run_cost =
    embedding_requests * embedding_unit_cost
  + reranker_units * reranker_unit_cost
  + input_tokens / 1_000_000 * input_price
  + output_tokens / 1_000_000 * output_price

Cache policy matters. Run one benchmark with cold caches and one with the cache behavior you expect in production. Label both clearly; otherwise a latency comparison may conflate retrieval quality with cache state.

Run the comparison reproducibly

A command-line runner makes the experiment easier to repeat:

python -m finrag.index \
  --corpus-version 2026-11-23-demo-01 \
  --chunk-config configs/chunking.yaml \
  --output artifacts/indexes/demo-01

python -m finrag.evaluate \
  --questions corpus/questions.jsonl \
  --index artifacts/indexes/demo-01 \
  --variants lexical,dense,hybrid,hybrid_rerank \
  --split test \
  --output artifacts/runs/demo-01.jsonl

python -m finrag.report \
  --runs artifacts/runs/demo-01.jsonl \
  --output artifacts/reports/demo-01.html

Pin application dependencies and record the runtime, operating system, CPU or GPU type, index build time, model identifiers, and environment configuration. A container or lockfile is useful, but it does not make external model APIs deterministic. State which parts remain variable.

For answer generation, use deterministic settings where supported. Do not claim exact reproducibility if the provider does not guarantee deterministic output. Instead, run multiple seeds or repeated calls and report variation in answer metrics.

How to interpret the result

The comparison should produce a table with one row per variant and separate columns for retrieval quality, answer quality, latency, and cost. Avoid collapsing everything into one score too early. A weighted utility score can be useful for a deployment decision, but publish the weights and show the underlying metrics.

Typical interpretations are conditional:

  • If BM25 has strong exact-lookup recall but weak paraphrase recall, improve query normalization or add dense retrieval.
  • If dense retrieval returns the right topic but the wrong period, strengthen metadata filtering and test whether period fields are indexed.
  • If hybrid retrieval increases candidate recall but answer quality falls, the generator may be overwhelmed by contradictory or redundant context.
  • If reranking improves nDCG but p95 latency becomes unacceptable, reduce candidate depth, cache stable queries, or use a smaller reranker.
  • If answer quality improves only on answerable questions but abstention worsens, evaluate unanswerable questions separately.

Do not call one approach “best” without defining the objective. For a compliance-oriented assistant, citation support and abstention may matter more than a small latency improvement. For an interactive research tool, p95 latency may be a hard constraint. The benchmark should expose that trade-off.

Failure cases worth testing deliberately

Wrong period, right metric

Put adjacent annual and quarterly documents in the corpus. Ask for one period and verify that the answer does not silently use another. Include the period in metadata and in the evidence block shown to the model.

Right issuer, wrong document type

An earnings presentation may round a number while an annual report provides the audited figure. Label both and decide which source hierarchy your product requires. The assistant should explain the source it used rather than blending values.

Table header loss

Remove or corrupt a table header in a test fixture. A system that retrieves the row but cannot identify the period or unit should abstain instead of guessing.

Conflicting passages

Include a restated figure or a later correction. Test whether the metadata and document policy identify the preferred source. Do not hide contradictions by deduplicating text without a trace.

Unanswerable question

Ask for a metric absent from the corpus. A high-quality system should retrieve related material, recognize the gap, and say that it cannot verify the requested fact. This is a product behavior to score, not an embarrassing edge case to remove.

Prompt injection inside a document

Treat retrieved text as untrusted data. A filing or uploaded document may contain instructions that attempt to change the assistant’s behavior. The system prompt should explicitly separate document content from instructions, and the evaluator should include an injection fixture.

Operational controls before real use

A prototype becomes a service only after it has controls around data, observability, and change management. Keep immutable run records for evaluation, but minimize production logs and redact user-provided sensitive information. Restrict who can upload or re-index documents. Verify file types and scan inputs before parsing.

Version the corpus, parser, chunker, embedding model, lexical analyzer, reranker, prompt, and evaluation rubric. A retrieval change can alter answers even when the language model is unchanged. Expose the evidence passages and document metadata to reviewers so an answer can be audited.

Add regression tests for known questions. A release should fail if a critical slice loses recall, citation support, or abstention quality beyond a declared threshold. Thresholds should be agreed with the product owner; this article does not prescribe universal values.

Limitations of this benchmark design

A small labeled set may not represent the long tail of financial research. Human labels can disagree, especially for comparative or definition questions. Retrieval metrics depend on chunking: a passage can be judged irrelevant simply because the answer was split into neighboring chunks. Model and provider changes can alter latency, tokenization, and answer behavior.

Cost estimates are only as reliable as their inputs. Do not compare providers or models using prices copied from memory. Record the pricing source and date in your experiment artifacts, or report token and request counts without converting them to money.

Finally, better retrieval does not establish financial truth. The system can only expose evidence from its corpus. Source quality, document coverage, restatements, accounting interpretation, and human review remain separate responsibilities.

Completion checklist

  • Corpus files are hashed and assigned a version.
  • Chunks preserve issuer, period, page, section, and document identifiers.
  • Lexical, dense, hybrid, and reranked variants share the same questions.
  • Development and held-out test questions are separated.
  • Recall@k, MRR, nDCG, answer quality, citation support, and abstention are measured.
  • Median, p95, errors, token usage, and cache state are recorded.
  • Prices are sourced and dated, or monetary cost is omitted.
  • Failure cases include wrong periods, tables, contradictions, unanswerable questions, and prompt injection.
  • Every generated answer can be traced to the exact retrieved chunks and run configuration.

Conclusion

To build a financial RAG agent and compare retrieval approaches responsibly, treat retrieval as an experiment rather than a library choice. Start with a versioned corpus and labeled questions. Compare lexical and dense retrieval, combine them through hybrid retrieval, add reranking only when its quality gain justifies its latency and cost, and evaluate the answer against the evidence instead of trusting fluency.

The result is not a claim that one architecture wins everywhere. It is a repeatable decision process: identify which questions fail, determine whether the failure is retrieval or generation, measure the operational trade-off, and keep the evidence boundary visible to the person using the assistant.

We publish what works for us—and implement the same solutions for your business. We design AI automation, Telegram bots, chats, and AI agents for real-world processes. Discuss your project →