Key takeaways
- The RAG Cross-Contamination Challenge: When scaling knowledge bases with similar documents (such as bank annual reports), standard semantic search can inadvertently retrieve paragraphs from competitors due to shared formats, boilerplate text, and overlapping terminology.
- The Power of Context Lines: Adding explicit context lines that identify the owner and time period at the start of every chunk helps prevent retrieval pipelines from mixing up data across similar documents.
- Managed vs. Reference Pipelines: Native retrieval solutions (like MariaDB AI Retrieval) maintain strict document separation out of the box, whereas open reference pipelines require careful configuration—including context lines and rerankers—to prevent cross-document citation errors.
- Mandatory Citation Validation: Implementing automated post-retrieval checks to verify that every cited source matches the target company and fiscal year is essential to eliminate hallucinations and ensure data integrity.
Suppose your team has built an AI agent that explains a monthly exception report. A SQL query flags each company whose figures cross a threshold, such as net income that fell more than 25% from the prior year. The agent reads the flagged company’s financial data with SQL, uses AI Retrieval to find the reason behind the flag in its annual reports, and cites a source for every number and every cause.
In a pilot with one company’s annual reports, the agent works great. Every chunk a retrieval returns belongs to that company, so every citation names the right source. Then you expand the knowledge base to include similar data for many companies. Now it has hundreds of documents with a similar format, common boilerplate text, and, in many cases, duplicate data, such as figures each report repeats from earlier years.
A retrieval-augmented generation (RAG) pipeline splits each document into short passages called chunks, and a semantic search returns the chunks closest in meaning to the question. Annual reports from similar companies mean nearly the same thing. A chunk from the middle of an annual report rarely names its company or year, so the search can return a competitor’s paragraph that answers the question well, and the agent cites it.
I loaded the annual reports of 19 similar banks into a single MariaDB AI Retrieval knowledge base and connected the agent from From Exception Report to AI Agent to it. I also built my own retrieval pipeline as a reference, and ran every question through both for comparison.

The agent code I wrote for my one-company pilot didn’t need to change. The application logic needed three additions, each a few lines of code.
Similar Banks Make a Hard Retrieval Test
Banks make a hard retrieval test, because their annual reports cover the same topics in roughly the same order. I chose 25 small and midsize US commercial banks from SEC data and loaded reports for 19 of them, which puts the banks in three groups:

- 16 banks with all 3 reports. Every retrieval and agent question is about one of these.
- 3 banks with some reports. A retrieval can mistakenly return chunks from them.
- 6 banks with no reports. SQL has their financial data, and the knowledge base has none of their reports, as with a customer whose upload failed.
Every question names one bank and one fiscal year:
| Test | Questions | Runs |
|---|---|---|
| Retrieval | 96: each of the 16 banks, each of 3 years, on 2 topics | 1,536 retrieval calls across 16 retrieval configurations, such as with and without the reranker |
| Agent | 32: one for each row of an exception report that flags a drop in net income | 208 agent runs, including repeats to measure noise |
| Missing bank | 36: each of the 6 banks with no reports, each of 3 years, on 2 topics | 72 agent runs |
I focused my questions on 2 topics: why the bank’s net interest income changed that year, and what its annual report says about competition. Both question templates came from earlier tests with these banks, written before I saw any result from these pipelines.
The reference pipeline is the kind a team might build for itself from open parts: MariaDB Community Edition as the vector store, the qwen3-embedding:8b embedding model, and the bge-reranker-v2-m3 reranker. When both pipelines get a question wrong, the documents are the likely cause.
MariaDB AI Retrieval Kept the Banks Apart, Even With Its Reranker Disabled
Across 480 retrieval calls in 5 of the configurations, AI Retrieval returned 2,874 chunks, and none came from another bank’s report. The right bank’s chunk came first for every question. I ran the agent on the 32 report rows twice, once with each retrieval limited to the bank’s 3 reports and once without that filter. In both runs, none of the 32 answers cited another bank’s report.
The reference pipeline matched that result only with two parts in place. The first is a context line that names the bank at the start of every chunk. The second is a reranker, a model that reads the question and each candidate chunk together and returns the best matches. I removed one part at a time and ran the questions again:

Without the reranker, close to half the reference pipeline’s questions returned another bank’s chunk, and nothing reported an error. With its reranker off, AI Retrieval still returned none. Keep questions like these in your application’s test suite, so a change to a retrieval setting fails a test before wrong answers reach a reviewer.
AI Retrieval kept the banks apart, but retrieval isn’t access control. The agent writes its own queries, and in 4 of the 32 runs without a filter, one of them returned other banks’ chunks. The model left those chunks out of its answers. If your documents belong to customers who must not see each other’s data, the application has to set a filter on every retrieval request, as in the second addition below.
Name Each Document by Company and Year
AI Retrieval searches by meaning, as any vector search does. An embedding model turns each chunk into a vector, and the search returns the chunks whose vectors are closest to the question’s. A paragraph from the middle of an annual report says “the Company” or “we,” and two banks often explain the same result in the same boilerplate.

AI Retrieval adds that context line for you. It starts most chunks with a line built from the document’s name, so the file name decides what each chunk says about where it came from. Put the company and the year in that name. In this test, the upload named each 10-K, the annual report a US public company files with the SEC, like this:
python
# AI Retrieval builds each chunk's context line from this name,
# for example [Context: example-bancorp-10-k-fy2024].
# slug() turns a company name into lowercase words joined by hyphens.
filing["named"] = f"{slug(filing['company'])}-10-k-fy{filing['year']}.html"
The name also gives every AI Retrieval result a ref, a short source label that the agent cites in its answer, such as example-bancorp-fy2024#12.
Filter Each Retrieval to the Report for That Year
Each annual report repeats the previous years for comparison, so two reports from one bank describe the same year. Retrieval always put the right bank’s report first, but not always the report for the right year:
| Retrieval | Net interest income questions | Competition questions |
|---|---|---|
| AI Retrieval, no filter | 75.0% | 66.7% |
| Reference pipeline, no filter | 87.5% | 72.9% |
| AI Retrieval, filtered to the bank’s 3 reports | 68.8% | 64.6% |
| AI Retrieval, filtered to the bank’s report for the year | 100% | 100% |
Share of questions whose top chunk came from the question’s fiscal year.
Limiting each retrieval to the bank’s 3 reports didn’t help because each report also covers earlier years. Limiting it to the bank’s report for that year put the right report first every time. The exception report row names both, so the application sets the document_ids parameter of AI Retrieval before the agent starts. The same request asks for a hybrid search, which runs a keyword search next to the vector search, and reranks the best 25 chunks down to 6:
python
# self.scope holds the document IDs of the reports the row is about.
body = {"query": query, "retrieval_method": "hybrid", "top_k": 25,
"enable_rerank": True, "rerank_top_k": 6}
if self.scope is not None:
# Set by the application from the report row. The tool's schema has
# no parameter for it, so the model can't see or change it.
body["document_ids"] = self.scope
I measured the year filter on retrieval only. The agent runs used the wider filter, the bank’s 3 reports, and that was enough to keep every retrieval inside the right bank. Without a filter, the agent’s own queries returned another bank’s chunk in 4 of 32 runs.
Check the Company of Every Cited Chunk
A missing report is easy to notice when the knowledge base holds one company. With many companies, someone will ask about a company whose reports nobody loaded, or whose upload failed. Questions about missing companies were the only case in this test that produced wrong citations.
I asked 36 questions about the 6 banks with no reports. With nothing of theirs to find, every retrieval returned other banks’ chunks, and 7 of 36 first answers cited another bank’s chunk as a labeled example:
I can’t tell you what [Bank A] says about competition, because none of its 10-Ks are in the knowledge base. […] As an example of what that kind of section covers, [Bank B]’s 2023 10-K says that changes in technology are likely to make competition stronger. […] That is [Bank B]’s statement, not [Bank A]’s.
A careful reader isn’t misled, but the citation is wrong. An application that stores the citation, in a report or in the agent’s memory, would record Bank B as a source for a finding about Bank A.
The citation check runs in code after every answer. Among its checks, it confirms that a tool returned each cited ref, and that each cited chunk comes from the bank in the question:
python
# refs: the refs the answer cites. sql and passages: the refs the tools
# returned. company_of("example-bancorp-fy2024#12") returns "example-bancorp".
for ref in refs:
if ref not in sql and ref not in passages:
failures.append({"check": "ref_exists", "ref": ref})
elif ref in passages and company_of(ref) != company:
failures.append({"check": "company_match", "ref": ref})
All 7 answers failed the check, and on its one retry the model dropped Bank B’s chunk, so no final answer cited another bank’s report.
AI Retrieval Needed Fewer Settings and No Extra Services
| AI Retrieval | Reference pipeline | |
|---|---|---|
| Settings I chose | 4 | 12 |
| Extra services to run | None | An embedding server and a reranker service on a GPU machine |
| Code for loading and retrieval | 407 lines | 603 lines |
| Loading 53 reports | 1,627 MCUs | About 2.3 hours of GPU time, almost all of it embedding |
| Retrieval per agent run | Under 0.01 MCUs | GPU time for embedding and reranking |
| Model cost per agent run | About $0.07 | About $0.07 |
MCUs are MariaDB Compute Units, the billing unit for AI Retrieval.
Of the 12 reference pipeline settings, 2 decided whether answers stayed with the right bank: whether to add the context line and whether to rerank. I chose them correctly only because earlier tests had failed without them. AI Retrieval’s 4 settings were choices from short lists, such as the parsing strategy and whether to rerank. Its retrieval method defaults to hybrid search.
Try AI Retrieval on Your Own Documents
MariaDB AI Retrieval is in Public Preview in MariaDB Cloud, and the Public Preview includes 100 MCUs per organization each month. Loading documents uses most of the allowance, and the retrievals in these steps use almost none. These bank 10-Ks took about 31 MCUs each, so 100 MCUs loads about 3 of them, and loading cost depends on file size. To run the tests in this article on your own documents, start with short ones:
- Create a document knowledge base with the AI Retrieval quickstart.
- Upload 4 short documents that look alike, 2 years each from 2 owners, and name each file by what people ask about, such as
<owner>-<document>-<year>. - Ask questions that name each owner, and check the source document of every chunk that comes back.
- Ask the same questions with
document_idsset to the owner’s document for the question, and compare which chunks come first. - Ask about an owner you didn’t upload, and check which owners’ chunks come back. If your application cites chunks, add a check that compares each cited chunk’s owner with the owner in the question.
Frequently Asked Questions
When scaling knowledge bases with similar documents, such as bank annual reports or financial filings, standard semantic search can inadvertently retrieve paragraphs from competitor documents due to shared formatting, boilerplate text, and overlapping terminology.
Adding explicit context lines that identify the owner and time period at the start of every text chunk helps retrieval pipelines distinguish between similar documents and avoid mixing up data across sources.
Managed native retrieval solutions like MariaDB AI Retrieval maintain strict document separation out of the box with minimal configuration, whereas open reference pipelines require manual setup, including custom context lines and dedicated rerankers, to prevent citation errors.
Automated post-retrieval checks that verify every cited source matches the correct target company and fiscal year are essential to eliminate hallucinations and ensure data integrity in AI-generated reports.
