Enter tokens despatched to the inspiration mannequin (FM) on each name are sometimes a significant a part of the price of working Retrieval Augmented Era (RAG) at scale. Question-aware compression gives one approach to scale back what number of of them attain the mannequin. Amazon Bedrock supplies the basis fashions and options to construct RAG purposes. RAG retrieval normally tunes for top recall, returning a broad set of probably related chunks so the first mannequin has thorough supply materials to work with. This design helps builders really feel assured that the fitting data is offered at inference time. As workloads scale, builders typically search for methods to optimize the cost-performance tradeoff by lowering the variety of enter tokens the first mannequin processes whereas sustaining reply high quality. The open, composable structure of Amazon Bedrock helps customized post-retrieval processing steps that refine what reaches the first mannequin.
On this submit, we describe a post-retrieval customization sample that achieves important input-token discount, and subsequently value financial savings, whereas preserving reply high quality. It’s suitable with RAG retrievers on Amazon Bedrock, together with Amazon Bedrock Information Bases. As a secondary profit, eradicating irrelevant context reduces the floor space for hallucination. After retrieval however earlier than the ultimate reply name, a smaller, lower-cost mannequin on Amazon Bedrock filters retrieved chunks in opposition to the person’s question. The first mannequin then receives the filtered context and generates the reply.
We cowl the sample’s structure at a excessive degree, present the core Amazon Bedrock implementation in a AWS Lambda operate, stroll by means of the associated fee mannequin and the latency tradeoff, and describe how we evaluated reply high quality. We additionally have a look at how this sample can layer on prime of current Amazon Bedrock capabilities like immediate caching, Amazon Bedrock Clever Immediate Routing, and the Rerank API for compounding value financial savings.
Stipulations
To implement the answer, full the next prerequisite steps:
- Have an energetic AWS account.
- Create an AWS Id and Entry Administration (IAM) function for the Lambda operate to entry Amazon Bedrock. For directions, consult with Create a task to delegate permissions to an AWS service.
- Add coverage permissions to the IAM function.
- Amazon Bedrock mannequin entry in your AWS Area for the 2 fashions within the cascade: a smaller compression mannequin (Anthropic Claude Haiku on this submit) and a major mannequin (Anthropic Claude Sonnet). Request entry on the Amazon Bedrock console beneath Mannequin entry.
How query-aware compression reduces RAG prices on Amazon Bedrock
A RAG move utilizing conventional RAG infrastructure or frameworks seems like:
- The applying embeds the person question.
- The vector index returns the top-k chunks (generally 5–20).
- Optionally, a reranker reorders these chunks by relevance.
- All retrieved chunks are concatenated into the immediate.
- The first mannequin generates the reply.
Retrieved context scales with top-k and chunk measurement: retrieving 5–20 chunks at typical chunk sizes places many technical-documentation and authorized RAG workloads within the vary of a number of thousand enter tokens per question. Decreasing the per-query token rely can yield significant value financial savings.
Structure overview
A smaller mannequin reads the retrieved chunks alongside the question and outputs solely the verbatim spans related to the query. We use Claude Haiku on this submit, however the sample works with different small/major mannequin pairs inside a mannequin household on Amazon Bedrock. Each the compression name and the first mannequin’s reply name run inside a single AWS Lambda operate. Upstream, a retriever embeds the question and returns the top-k chunks. An Amazon Bedrock information base, the absolutely managed RAG functionality backed by Amazon OpenSearch Serverless, is one such retriever. The Lambda operate receives these chunks as enter and returns the ultimate reply. The compression name is the one step added to a normal RAG move.
As a result of the smaller mannequin prices much less per token than the first mannequin, trimming the context earlier than the costly reply name is the place the financial savings come from. How giant these financial savings are comes down to 2 issues.
The next diagram exhibits the answer structure.
Determine 1: Question-aware context compression structure on Amazon Bedrock
The move proceeds by means of the next steps:
- The person submits a question to the appliance.
- The applying sends the question to the retriever, which searches the vectorized information sources.
- The retriever returns the top-k chunks (full retrieved context) to the appliance.
- The applying passes the question and all retrieved chunks to an AWS Lambda operate, which sends them to a smaller mannequin (Claude Haiku) by means of the Amazon Bedrock Converse API. That is the compression name.
- The smaller mannequin outputs solely the verbatim spans related to the question (the compressed context).
- The Lambda operate sends the question and the compressed context to the first mannequin (Claude Sonnet) by means of the Converse API. That is the reply name.
- The first mannequin generates the ultimate reply, which the appliance returns to the person.
Why this works economically
The economics depend upon two elements: the value ratio between the small and first fashions on Amazon Bedrock, and the compression ratio the smaller mannequin achieves.
For a single RAG question with R retrieved enter tokens, a compression ratio of c (the place c > 1), a last reply output of A tokens, and per-token costs P_small_in / P_small_out (smaller mannequin enter and output worth) and P_large_in / P_large_out (major mannequin enter and output worth):
| Stage | Baseline | With compression | What occurs |
| Smaller mannequin reads chunks | — | R × P_small_in |
The smaller mannequin receives the question and all retrieved chunks as enter |
| Smaller mannequin writes compressed output | — | (R/c) × P_small_out |
The smaller mannequin outputs solely the verbatim spans related to the question |
| Main mannequin reads context | R × P_large_in |
(R/c) × P_large_in |
The first mannequin receives the context as enter to generate the reply |
| Main mannequin writes reply | A × P_large_out |
A × P_large_out |
The first mannequin generates the ultimate reply for the person |
| Complete | R·P_large_in + A·P_large_out |
R·P_small_in + (R/c)·P_small_out + (R/c)·P_large_in + A·P_large_out |
The compression name provides the enter and output value of the smaller mannequin. Financial savings come from sending R/c as an alternative of R tokens to the first mannequin. The economics favor compression when:
- Retrieved context is giant (the financial savings on the bigger mannequin dominate).
- The worth ratio between giant and small is excessive (Sonnet/Opus paired with Haiku).
- A significant portion of the retrieved content material will be trimmed for a given question with out affecting reply high quality.
Implementation on Amazon Bedrock
The sample matches between retrieval and the ultimate reply name. We implement it as a single AWS Lambda operate that orchestrates the 2 Amazon Bedrock mannequin invocations utilizing the Converse API. The operate receives the person question and the retrieved chunks as its enter occasion.
The compression immediate
The compression immediate is a very powerful a part of the implementation. It should instruct the smaller mannequin to extract spans relatively than summarize, forbid paraphrasing and rewriting, and protect sufficient surrounding context for citations to stay correct.
The Lambda operate
The operate takes the person question and the retrieved chunks, reads the 2 mannequin IDs from atmosphere variables, and initializes an Amazon Bedrock Runtime shopper configured with adaptive retries. It then makes two calls by means of the Bedrock Converse API: the primary to the smaller mannequin to compress the chunks, and the second to the first mannequin to generate the reply from the compressed proof. The compression name runs at temperature 0.0, which retains the extraction deterministic so the smaller mannequin copies spans as they seem within the supply:
Step 1: Compress. The smaller mannequin filters the retrieved chunks.
Step 2: Reply. The first mannequin causes over the filtered proof.
Word: The prompts are an instance and needs to be tailored to your paperwork and query varieties.
Methodology
Earlier than recommending this sample, we evaluated it empirically. The benchmark lined:
- A corpus of greater than 500,000 paperwork spanning 9 enterprise supply varieties, together with chat messages, e-mail, issue-tracker tickets, shared-drive paperwork, CRM data, assembly transcripts, code repositories, and wiki pages.
- A set of 500 questions throughout 10 classes, starting from slender factual lookups to broader multi-part questions, together with each exactly worded and extra informally phrased queries.
- A number of compression situations: no compression (baseline), compression, and rerank-plus-compression.
- Every question run by means of baseline and optimized pipelines, with solutions scored by a giant language mannequin (LLM) choose.
- High quality scoring throughout 4 dimensions (correctness, completeness, quotation accuracy, and conciseness) in opposition to a reference reply. Faithfulness was tracked individually, checking every reply’s claims in opposition to the proof the first mannequin obtained.
These figures describe one corpus, one area, and one question distribution. Outcomes by yourself paperwork and queries will differ.
Outcomes
The next desk summarizes the headline outcomes throughout the queries, evaluating the baseline, compression, and rerank + compression pipelines.
| Metric | Baseline | Compression | Rerank + Compression |
| Value | 100% (ref) | 67% | 64% |
| Tokens to mannequin | 100% (ref) | 12% | 10% |
| Latency | 0% (ref) | +19% (slower) | +12% (slower) |
| High quality (composite, 4 dims) | 100% (ref) | 97.5% | 97.6% |
| Hallucination charge | 51% | −7 pts | −13 pts |
The next figures come from the benchmark. Outcomes by yourself corpus, queries, and mannequin selections will differ. The next determine exhibits the typical of question value saving (left axis, share versus baseline) and the discount in context despatched to the first mannequin (proper axis, instances fewer tokens). Compression achieved a 33 % value saving or 8.6× fewer tokens. Rerank + compression reached 36 % value saving and 10.1× fewer tokens.
Determine 2: Value financial savings and discount in context tokens despatched to the first mannequin
The next determine exhibits the LLM-judge scores (1–5) throughout the 4 answer-quality dimensions for every pipeline. Correctness stays inside 0.07 of baseline throughout situations. Completeness and quotation accuracy are barely decrease beneath compression, whereas conciseness is barely increased.
Determine 3: LLM-judge scores throughout the 4 answer-quality dimensions by pipeline
The next determine exhibits the hallucination charge for every pipeline, measured because the share of solutions containing not less than one declare not supported by the reference. The baseline is 51 %, compression 44 %, and rerank + compression 38 %.
Determine 4: Hallucination charge by pipeline
The next determine exhibits the associated fee saving versus baseline for the typical-query set and the hard-query set. Compression strikes from 37 % to 26 %, and rerank + compression from 40 % to 30 % between the 2 units.
Determine 5: Value financial savings for the typical-query and hard-query units
Concerns for manufacturing use
Three issues should be weighed earlier than this sample is adopted: the latency of the added compression name, the impression on reply high quality, and whether or not the workload is a match. Every is roofed within the following sections.
Latency
Including a smaller-model name introduces one additional step within the path. Claude Haiku is optimized for pace, and since the first mannequin then processes a smaller, targeted context, a part of that added time is recovered on the reply name. The overall end-to-end latency is the compression name plus the reply name on the targeted context. Web impression will depend on how compute-bound the first mannequin is on the unique context measurement.
For latency-critical surfaces (sub-second chat), measure with your individual context sizes earlier than deploying.
High quality
Compression entails a number of elements price deliberate engineering.
| Dimension | Design consideration | How the sample handles it |
| Factual consistency | The first mannequin ought to obtain all of the proof it wants | The immediate instructs trustworthy, verbatim extraction of all query-relevant proof |
| Quotation preservation | Spans keep tied to their supply chunk | [CHUNK_ID: markers carried by means of the immediate |
| Trustworthy extraction | Output stays verbatim from the supply | temperature=0.0, “verbatim solely” instruction, elective post-hoc span validation |
| Multi-step reasoning | Proof chains keep intact for analytical questions | The compression mannequin preserves extra spans when the query attracts on a number of sources |
| Numerical reasoning | Particular numbers are retained | The “verbatim solely” instruction preserves numbers and calculations as they seem within the supply |
Selecting the smaller mannequin
A number of concerns might help information the choice of the smaller mannequin:
- Relative pricing. The financial savings of this sample depend upon the value ratio between the smaller mannequin and the first mannequin. A decrease relative worth for the smaller mannequin leaves extra room for internet financial savings as soon as the price of the compression name is included. Amazon Bedrock mannequin households span a spread of worth factors, with present charges accessible on the Amazon Bedrock pricing web page.
- Inference pace. The compression name is added to the request path, so a smaller mannequin optimized for quick inference limits the extra latency.
- Mannequin-family alignment. Choosing the smaller and first fashions from the identical household retains formatting, chunk markers, and instruction dealing with constant throughout each calls. Claude Haiku with Claude Sonnet, and Amazon Nova Micro with Amazon Nova Professional, are examples of such pairings on Amazon Bedrock. For mannequin availability by Area, consult with Supported fashions by AWS Area in Amazon Bedrock.
Advisable analysis strategy earlier than deploying
- Assemble an analysis set of consultant queries out of your software logs (de-identified), protecting each slender lookups and broader multi-fact questions within the proportion they happen in manufacturing.
- Run the baseline and compressed pipelines on the identical retrieval outcomes, so the comparability displays solely the impact of compression.
- Rating each units of solutions on the scale that matter for the workload, comparable to correctness, completeness, and quotation accuracy in opposition to a reference reply, together with faithfulness in opposition to the proof the first mannequin obtained, which surfaces claims not supported by that proof.
- Set acceptance thresholds that match the workload, and promote the configuration after it meets them.
For manufacturing deployments, Amazon Bedrock Guardrails can present extra content material filtering and grounding validation as complementary controls alongside the compression sample.
The place this sample matches properly
The sample supplies worth when three situations line up. Retrieved context is giant, the first mannequin is the costly a part of the decision, and the query is slender relative to the breadth of what was retrieved. The next situations share these traits.
- Regulated-industry compliance and coverage assistants. Utilities, monetary companies, insurance coverage, and healthcare preserve lengthy bulletins, tariffs, and coverage paperwork the place a single part solutions most questions. Retrieval pulls complete bulletins (8,000–15,000 tokens) however the analyst wants one clause. Verbatim extraction preserves the precise regulatory wording and the supply ID, which issues when the reply needs to be defensible in an audit.
- Buyer-support copilots over a big product information base. High-k is ready excessive so the assistant is much less prone to miss an edge case, which means most tickets retrieve way over they want. A “how do I reset X” or “what’s the payment for Y” ticket normally resolves from one or two paragraphs buried in a protracted article. Compression cuts the per-ticket inference value with out touching the retrieval technique help engineers already belief. At help volumes (a whole lot of 1000’s of tickets a month), small per-query financial savings accumulate rapidly.
- Inside engineering and operations assistants over runbooks and wikis. Confluence, SharePoint, and runbook RAG retrieves giant chunks as a result of inside docs are verbose and loosely structured. Questions are normally particular (“which IAM function does service Z assume”, “what’s the rollback step for migration N”). The reply is a number of strains. The encompassing coverage and background will not be wanted for the response.
- Monetary analysis and earnings-call evaluation. Retrieved chunks are dense (10-Okay sections, transcript paragraphs) and questions are pointed (“what did the CFO say about FX publicity in Q3”). Compression preserves the analyst’s potential to cite the supply verbatim whereas lowering the per-query value on the bigger mannequin.
This sample is probably going price a prototype if most of those are true in your workload:
- Common retrieved context per question exceeds roughly 5,000 tokens.
- The first mannequin’s enter tokens are costly sufficient {that a} 3–10× discount in context is materials.
- Most questions are slender relative to the amount retrieved, relatively than broad “summarize all the things” requests.
- Your latency finances can soak up one additional mannequin name (a number of hundred milliseconds to roughly 1 second).
- You might have, or can construct, an analysis set to check high quality earlier than and after.
In case your workload is sub-second conversational chat with small retrieved context, this sample might not be the perfect match. Contemplate Amazon Bedrock options like immediate caching and Clever Immediate Routing, which give extra worth with much less added latency.
Conclusion
The sample matches into RAG pipelines constructed on Amazon Bedrock: A single Lambda operate between retrieval and the ultimate mannequin name compresses context and fine-tunes value and high quality in your particular workload.
The compression immediate handles each easy lookups and complicated multi-source questions with out requiring separate routing logic. Mixed with current options like immediate caching, Clever Immediate Routing, and the Rerank API, this strategy delivers compounding optimizations throughout the complete RAG pipeline.
You should use this sample behind a function flag, measure it in your actual question distribution, and tune the compression prompts to match your area.
To get began, consult with the Amazon Bedrock documentation and the Amazon Bedrock pricing web page for present mannequin prices.
In regards to the authors

