Connecting a large language model to a legacy database looks deceptively simple on an architecture diagram.
Data is extracted. Documents are embedded. A user asks a question. The retrieval layer finds relevant records and sends them to Azure OpenAI. That basic flow matches Microsoft’s description of retrieval-augmented generation in Azure AI Search, but the useful work is in the constraints around it.
In production, the pipeline is rarely that clean.
Legacy estates contain duplicated records, obsolete versions, inconsistent terminology, unstructured notes, scanned documents, overloaded database fields, and decades of exceptions. A retrieval-augmented generation system does not make this disorder disappear. Unless the retrieval layer is carefully designed, it packages the disorder into tokens and sends it directly into the model’s context window.
That creates an expensive misconception: when answers are incomplete, the system simply needs more context.
It usually does not.
It needs greater information density.
The most effective RAG systems are not those that retrieve the most content. They retrieve the smallest amount of content required to produce a reliable answer.
A Context Window Is a Finite System
A context window is the total token capacity available to a model for the input, conversation history, retrieved material, instructions, and generated output.
Its exact size depends on the deployed model and configuration. Azure also distinguishes between a model’s per-request context limit and its tokens-per-minute quota. Increasing throughput quota does not increase the maximum amount of context the model can process in one request; Azure OpenAI quota documentation defines throughput limits separately as tokens per minute and requests per minute.
This makes a context window less like an unlimited knowledge store and more like a finite physical space.
Every token placed inside that space competes with every other token.
A paragraph containing the answer occupies capacity. So does a duplicate paragraph, an outdated policy, a navigation header, a database disclaimer, or an irrelevant customer note.
The model may be able to process all of it, but capacity is not the same as clarity.
A useful engineering analogy is to treat the context window as a container with three constraints:
- Volume: Only a finite number of tokens fit.
- Signal concentration: Relevant facts must remain distinguishable from noise.
- Processing cost: More input increases the amount of material that must be processed.
This is not literal physics. It is a practical model for understanding why indiscriminately adding context creates diminishing returns.
Legacy Data Has High Information Entropy
Well-maintained product documentation usually has recognizable structure.
Titles describe the subject. Headings separate concepts. Versions are identifiable. Terminology is relatively consistent. Related facts appear close together.
Legacy enterprise data often has the opposite characteristics.
A single business concept may be represented by:
- several table names created during different migrations;
- abbreviations used by separate departments;
- free-text comments written over many years;
- archived and active records stored together;
- multiple documents containing slightly different answers;
- fields repurposed without corresponding schema updates;
- OCR output from scanned files;
- values whose meaning depends on an undocumented business process.
For a RAG pipeline, this is a high-entropy environment. The information exists, but its meaning is distributed across inconsistent structures and competing representations.
Vector search alone does not resolve that ambiguity. Embeddings can identify semantically similar content, but similarity is not the same as authority, freshness, or business validity.
A query for a customer’s cancellation terms might retrieve:
- the current contract;
- an expired contract;
- an internal training guide;
- a support note describing an exception;
- a generic policy page;
- a document belonging to a different customer segment.
All six results may be semantically relevant. Only one or two may be appropriate for the answer.
The token problem therefore begins before the prompt is assembled. It begins with the quality of the source data and the precision of retrieval.
A Real-World Example
One of the more challenging integrations I worked on involved exposing a legacy COBOL application through a modern RAG interface. The application itself was still performing critical business functions reliably, which is why modernizing runtime without rewriting business logic is often less risky than pretending the legacy system has no remaining value. Almost all of the institutional knowledge surrounding it had disappeared. The original developers had long since retired, documentation was sparse, and many of the business rules existed only in COBOL programs written decades earlier.
The first instinct was to embed everything. Copybooks, program source, operational manuals, exported database records, and whatever documentation could still be found were indexed and made searchable. The retrieval results looked impressive, but the answers did not.
The model frequently returned multiple versions of the same business rule because similar logic appeared across several programs. Comments written years apart often contradicted one another, and identical paragraphs appeared repeatedly because they had been copied between documentation sets. The context window filled quickly, yet surprisingly little of it contained the information actually needed to answer the user’s question.
The breakthrough came when we stopped treating the problem as one of model capability and started treating it as one of information selection. Rather than sending every potentially relevant artifact to the model, we focused on identifying the authoritative source, filtering obsolete programs, preserving copybook relationships, and retrieving only the sections directly related to the requested business process.
The amount of retrieved context decreased significantly, but answer quality improved because the signal-to-noise ratio increased. That project reinforced an important lesson: in legacy modernization, the limiting factor is rarely the size of the context window. It is the quality of the information you choose to place inside it, especially when legacy systems consume AI outputs as if they were deterministic fields.
Why a Bigger Context Window Does Not Fix Poor Retrieval
Large context windows are valuable. They make it possible to analyze long documents, maintain richer conversations, and combine evidence from multiple sources.
They do not eliminate the need for retrieval discipline.
When a poorly tuned RAG system gains access to a larger context window, teams often respond by increasing the number or size of retrieved chunks. This can reduce “missing context” errors, but it may introduce a different class of failures:
- conflicting versions enter the same prompt;
- important evidence becomes surrounded by irrelevant material;
- input cost and latency increase;
- the model spends capacity reconciling duplication;
- less room remains for conversation history and output;
- debugging becomes harder because the source of an answer is less obvious.
The operational limit is also not just the context window. Azure OpenAI deployments have model-specific throughput quotas measured in tokens per minute and requests per minute. Sending unnecessarily large prompts can therefore reduce the number of requests a deployment can handle, even when each individual request remains within the model’s context limit. Microsoft gives the same practical rate-limit advice in its quota guidance: reduce prompt size where possible.
A bigger container helps only when the additional material contributes useful evidence.
Otherwise, the system is moving more noise at greater expense.
Retrieval Is a Compression Problem
A useful way to design enterprise RAG is to stop thinking of retrieval as document collection.
Think of it as controlled compression.
The source estate may contain millions of records, but the model should receive a compact representation of the evidence required for one question.
The pipeline becomes:
Legacy sources → normalization → filtering → retrieval → reranking → compression → context window → answer
Each stage should reduce uncertainty.
Normalization makes inconsistent data easier to compare. Filtering eliminates records that cannot be valid. Retrieval identifies potentially relevant material. Reranking improves the order of results. Compression removes material that does not contribute to the answer.
The objective is not maximum token utilization. It is maximum useful information per token.
This distinction is important because traditional database integration often rewards completeness. If a downstream application may need 40 fields, returning all 40 can be reasonable.
LLM context behaves differently. Every unnecessary field becomes additional language for the model to interpret. A database row containing 60 columns may need to become a five-line evidence record rather than a serialized copy of the entire row.
The best context is not always the most faithful representation of the source format. It is the smallest representation that preserves the meaning, provenance, and constraints needed to answer the question.
Start With Metadata Before Embeddings
One of the cheapest ways to reduce token consumption is to avoid retrieving invalid candidates.
Before performing semantic search, use deterministic metadata wherever possible.
Useful filters may include:
- customer or tenant ID;
- document type;
- product family;
- jurisdiction;
- security classification;
- effective date;
- expiration date;
- source system;
- business unit;
- language;
- record status;
- version number.
A user asking about an active New Zealand service agreement should not retrieve expired US agreements simply because the wording is similar.
Metadata filtering reduces the search space before any result reaches the model. It also encodes business rules that embeddings cannot be expected to infer reliably.
This is especially important in legacy integration because the same phrase may mean different things across departments, time periods, or source applications.
Combine Keyword and Vector Retrieval
Legacy systems are full of identifiers that semantic search may not handle as precisely as keyword search.
Account numbers, error codes, policy IDs, product names, acronyms, and exact field values often benefit from lexical matching. Broader natural-language questions benefit from vector similarity.
Azure AI Search supports hybrid search, which runs full-text and vector queries in parallel and combines their ranked results using Reciprocal Rank Fusion. This allows one query to benefit from the precision of keyword matching and the conceptual coverage of vector retrieval.
For legacy data, hybrid retrieval is often safer than relying on either method alone.
Consider the query:
Why did invoice INV-98421 fail after the ERP migration?
Vector search may find material about invoice failures and ERP migrations. Keyword search can prioritize the exact invoice identifier. Metadata filters can restrict results to the correct tenant and date range.
Together, these mechanisms reduce the chance that the context window is filled with generally relevant but operationally incorrect content.
Rerank Before Expanding the Prompt
Initial retrieval should produce candidates, not final context.
The top vector matches are not necessarily the best evidence. A chunk may rank highly because its language resembles the query, even though it is outdated, generic, or missing the decisive fact.
A reranking stage can reorder candidates using a stronger relevance assessment before the application selects content for the prompt. Azure AI Search semantic ranker applies language understanding models to rerank an existing result set, and Microsoft documents hybrid search with semantic ranking as an approach that can improve relevance.
The practical pattern is:
- Retrieve a moderately broad candidate set.
- Apply metadata and access-control constraints.
- Rerank the candidates.
- Keep only the highest-value evidence.
- Assemble context within a defined token budget.
This is more efficient than passing every candidate to the generation model and expecting it to perform retrieval, conflict resolution, and answer generation simultaneously.
Chunk Around Meaning, Not Arbitrary Length
Chunking is often treated as a fixed configuration value: 500 tokens, 1,000 tokens, or a set number of characters with overlap.
That is easy to implement but frequently wrong for legacy content.
A useful chunk should contain a coherent unit of meaning. Depending on the source, that may be:
- a policy clause;
- a troubleshooting step;
- a database record plus selected relationships;
- a procedure and its prerequisites;
- a table row with its headers;
- a contract section;
- a support incident summary;
- a code function and its comments.
Chunks that are too small lose context. Chunks that are too large carry irrelevant material into every retrieval result.
Overlap can protect meaning across boundaries, but excessive overlap creates duplication. If five retrieved chunks repeat the same introductory text, the context window pays for that text five times.
Azure AI Search supports indexing workflows that can chunk content and generate vector embeddings as part of an integrated vectorization pipeline. Its documented skillset approach can use text splitting or document-layout processing before storing chunks and vectors in the search index.
The tooling can automate the pipeline, but teams still need to choose chunk boundaries that reflect the semantics of their content.
Remove Duplication and Superseded Material
Legacy repositories often contain more duplication than teams realize.
The same policy may appear in SharePoint, a file share, an exported knowledge base, email attachments, and a database blob. Slightly different versions may all be indexed.
This creates two costs.
First, duplicate chunks consume storage, embedding, and indexing resources.
Second, duplicates can dominate retrieval. When several nearly identical chunks appear in the top results, the model receives less diverse evidence while consuming more tokens.
A useful ingestion process should identify:
- exact duplicates;
- near duplicates;
- boilerplate sections;
- superseded versions;
- empty or low-information records;
- generated navigation content;
- repeated headers and footers;
- OCR artifacts.
Do not delete historical information that has legal or operational value. Mark its status clearly and prevent it from entering current-answer workflows unless the question requires historical analysis.
Freshness should be an explicit retrieval signal, not an assumption left to the model.
Use Hierarchical Retrieval
Some questions need detail, but not at the beginning of the search.
Hierarchical retrieval starts with compact representations and expands only the most promising areas.
For example:
- Retrieve summaries of systems, documents, or record groups.
- Identify the most relevant source.
- Retrieve sections from that source.
- Fetch detailed records only when required.
This resembles navigating a map before inspecting individual buildings.
It works particularly well when the source estate contains long manuals, complex case files, or relational data with many dependent tables.
A user asking, “What changed in the billing process after the 2022 migration?” may first need a summary of the migration phases. Only after the relevant phase is identified should the system retrieve detailed change records, incident reports, and schema notes.
Without hierarchy, the application may retrieve detailed fragments from across the entire estate and force the model to reconstruct the structure itself.
Compress Retrieved Evidence Carefully
Retrieval compression can remove low-value text before the final prompt is assembled.
This may include:
- extracting only relevant sentences;
- converting verbose records into structured facts;
- removing duplicated passages;
- summarizing groups of related records;
- selecting only fields relevant to the question;
- preserving quotations for high-risk claims;
- retaining source IDs and timestamps for traceability.
Compression must be designed around the risk of information loss.
A customer-support assistant may safely summarize several similar notes. A legal, financial, or safety-related system may need exact wording and stronger provenance.
A reliable pattern is to preserve two layers:
Evidence layer: The original source text or exact structured values.
Context layer: A compact representation optimized for generation.
The answer can be generated from the context layer while citations, validation, or audit workflows remain connected to the evidence layer.
Assign a Token Budget Before Retrieval
Many RAG systems retrieve content first and discover the final prompt size afterward.
Reverse that sequence.
Define a context budget for each request type.
A simplified budget may reserve capacity for:
- system and safety instructions;
- conversation history;
- the user’s question;
- retrieved evidence;
- tool outputs;
- the model’s response.
The retriever can then select evidence until its allocated budget is reached.
This encourages explicit trade-offs. A conversational support assistant may reserve more room for history. A document-analysis workflow may allocate more capacity to evidence. A structured extraction task may need little output space but exact source text.
Token budgeting also improves operational predictability. It reduces sudden prompt growth, makes load testing more representative, and helps teams understand how retrieval changes affect deployment throughput.
Treat Conversation History as Retrieved Data
Context growth does not come only from documents.
Multi-turn applications often send the entire conversation with every request. Over time, old questions, superseded assumptions, verbose answers, and tool results consume more capacity than the current evidence.
Conversation history should be managed with the same discipline as external retrieval.
Possible strategies include:
- retaining recent turns verbatim;
- summarizing older turns;
- extracting stable user preferences or case facts;
- discarding resolved intermediate steps;
- preserving decisions separately from discussion;
- retrieving prior turns only when relevant.
The goal is not to erase memory. It is to prevent historical conversation from becoming unbounded context.
Azure’s chat documentation similarly notes that the message list grows with each question and answer, and that the combined input plus requested output must remain within the model’s token limit. The same issue shows up in agent-style systems where state size becomes latency and context overflow.
Use Prompt Caching for Stable Prefixes
Some prompt content is repeated across many requests:
- system instructions;
- policy definitions;
- tool schemas;
- formatting rules;
- stable reference material;
- few-shot examples.
For supported Azure OpenAI models, prompt caching can reduce latency and cost when longer prompts share identical content at the beginning. The stable prefix should therefore appear before request-specific content where the application design allows it.
Prompt caching is not a substitute for retrieval optimization.
It may make repeated tokens cheaper to process, but irrelevant retrieved material remains irrelevant. Caching improves the economics of stable context; it does not improve the quality of noisy context.
Treat it as one optimization layer within a broader token strategy.
Measure Information Yield, Not Just Retrieval Accuracy
A RAG evaluation should ask more than whether the correct document appeared in the top results.
It should measure how efficiently the pipeline delivered enough evidence for the model to answer.
Useful metrics include:
- answer correctness;
- citation correctness;
- retrieval precision;
- retrieval recall;
- tokens retrieved per request;
- tokens actually cited or used;
- duplicate-token ratio;
- average number of sources;
- latency by pipeline stage;
- input cost per successful answer;
- abstention rate;
- performance by source system or document type.
One particularly useful metric is evidence yield:
The proportion of retrieved tokens that contribute directly to the final answer.
This is not always easy to calculate automatically, but even a sampled human review can reveal waste.
If a system regularly sends 20,000 retrieved tokens and its answers rely on two 300-token passages, the primary problem is not the model. It is retrieval efficiency.
A Practical Azure Architecture
For a large legacy estate, a token-efficient architecture might use the following flow:
1. Ingestion and normalization
Extract content from databases, files, ticketing systems, document repositories, and archived applications.
Normalize dates, identifiers, document types, ownership, version status, and access-control metadata.
2. Deduplication and lifecycle handling
Detect repeated content, identify authoritative versions, and mark historical or superseded material.
3. Semantic chunking
Split documents and records into coherent units. Preserve relationships, headings, source identifiers, and effective dates.
4. Vectorization and indexing
Generate embeddings and store them alongside searchable text and filterable metadata in Azure AI Search. Azure AI Search supports vector and nonvector fields in the same index, enabling hybrid retrieval patterns. Microsoft’s Azure Architecture Center describes the same orchestration pattern in its guide to designing and developing a RAG solution.
5. Query analysis
Identify entities, exact identifiers, time constraints, document types, jurisdictions, and user permissions.
6. Filtered hybrid retrieval
Combine vector similarity with keyword search and metadata filters.
7. Reranking
Reorder the candidate set using semantic relevance and business signals such as freshness or authority.
8. Context compression
Remove duplication, select relevant passages, and transform verbose source structures into compact evidence.
9. Budgeted prompt assembly
Fit instructions, conversation state, evidence, and output allowance within an explicit token budget.
10. Generation with provenance
Generate the answer with references to the underlying sources and abstain when the evidence is insufficient or contradictory.
11. Evaluation and feedback
Track incorrect answers, retrieval failures, wasted tokens, stale sources, and user corrections. Feed those findings back into indexing and retrieval rules.
The Most Important Optimization Happens Before the Model
It is tempting to treat token consumption as a prompt-engineering problem.
Prompt design matters, but by the time a noisy 30,000-token evidence bundle reaches the prompt, the largest architectural mistake has already occurred.
The highest-value optimizations usually happen upstream:
- clean the data;
- identify authoritative sources;
- add metadata;
- remove duplication;
- retrieve with multiple signals;
- rerank candidates;
- compress evidence;
- enforce a budget.
The context window should be the final destination for selected evidence, not a dumping ground for everything that might be relevant.
Conclusion
A legacy database may be massive, but the answer to a user’s question is usually small.
The challenge is finding that answer without transporting the entire history of the organization into the model’s context window.
Thinking about context as a finite physical system creates a useful design constraint. Tokens occupy capacity. Noise dilutes signal. Duplication adds mass without adding meaning. Larger windows delay the limit, but they do not remove it.
The goal of enterprise RAG is therefore not to maximize how much data Azure OpenAI can receive.
It is to maximize the amount of trustworthy information carried by every token.
The best retrieval system is not the one that returns the most.
It is the one that knows what to leave out.





