Skip to lesson

AWSAWS Certified Generative AI Developer - Professional2026-104.1 Implement cost optimization and resource efficiency strategies

Verify semantic matches before reusing answers

Completed by the knowledge check

Learning objective

Set semantic-cache similarity thresholds and verify answer relevance before cache reuse.

Core idea

A semantic-cache score measures the relationship between a new query and a stored query. The artifact you return is the stored answer. Those are different comparisons: two questions can be similar while the answer to one is irrelevant, incomplete, or wrong for the other.

For an architect setting a reuse policy, similarity should nominate a candidate rather than authorize publication. Accept it only when its scope, freshness, and answer checks also pass. Calibrate the similarity threshold on examples labeled for safe answer reuse, not on examples labeled only for topical similarity.

How it works

Establish the score contract

AWS documents semantic caching with ElastiCache for Valkey vector search and Bedrock embeddings. The documented implementation uses ElastiCache for Valkey 8.2 or later and creates a store using index_type: "HNSW", distance_metric: "COSINE", and a vector dimension matching its embedding model. The underlying index is created with FT.CREATE. Its application helper searches stored queries, sorts returned score values highest first, and rejects the top result when score < min_similarity.

The guide’s min_similarity is a parameter in example application code, not a Bedrock API parameter or universal ElastiCache acceptance switch. Likewise, the example’s score interpretation belongs to that store abstraction. Define whether your own backend returns similarity or distance and which direction is better before applying a threshold. A comparison copied from a higher-is-better similarity wrapper is unsafe for a lower-is-better distance output.

Keep embedding identity, dimensions, preprocessing, and metric compatible between stored and incoming queries. An embedding or index change creates a new calibration contract; a threshold from the previous representation does not establish the same reuse quality. See 1.5.3 for embedding selection and 1.4.7 for vector metric economics.

Filter candidates using trusted application context before accepting similarity. AWS recommends tag and numeric filters derived from context such as product ID, category, region, or user segment. Authorization partitioning is the stricter boundary described in 4.1.9. A wrong product version must not become eligible merely because it produces a high score.

Calibrate for false reuse

Build a reviewed set containing incoming requests, candidate questions, cached answers, required context, and a safe-reuse label. Include paraphrases that should hit and near matches that should miss: opposite actions, different quantities, changed dates, different versions, and questions whose conversation context changes the referent.

For each candidate threshold, measure accepted-hit rate and the proportion of accepted hits whose answers fail the reuse contract. Also measure false misses, validation latency, and total cost per accepted response. A high hit rate can look economical while concealing an expensive wrong-answer rate. Break results down by domain, language, and consequence rather than letting common easy questions hide rare consequential failures.

AWS’s best-practices page recommends starting with a similarity threshold of 0.90–0.95 and lowering it while monitoring accuracy. Treat those values as starting guidance for a compatible similarity scale, not guaranteed accuracy levels. Select the production value from your labeled outcomes and the cost of false acceptance. A business rule can require stricter acceptance or disable semantic reuse entirely for a request class.

Do not validate only obvious hits. Review boundary cases around the threshold and pairs with high similarity but changed meaning. Recalibrate after embedding, prompt, source, model, or validation-policy changes. A model revision can alter which stored answers remain acceptable even if query vectors are unchanged.

Verify the actual answer

Before release, compare the cached answer with the incoming request’s constraints. Does it answer the requested action, product, version, locale, and time frame? Does it preserve required qualifications and the output contract? Does it rely on evidence the caller can access? Reject candidates whose dependencies are stale; exact dependency and expiration handling is described in 4.1.7.

For supported standalone question-answering, summarization, and paraphrasing use cases, Bedrock Guardrails contextual grounding checks provide separate grounding and relevance assessments. Grounding asks whether evidence supports the answer. Relevance asks whether it answers the query. AWS explicitly excludes conversational QA/chatbot use cases from this feature; do not assume it validates arbitrary multi-turn exchanges.

Use ApplyGuardrail to assess a cached candidate without invoking the generation model. Provide the incoming question with qualifiers: ["query"], authorized current evidence with qualifiers: ["grounding_source"], and the candidate answer as the content to guard, optionally marked guard_content. Set source: "OUTPUT" for the candidate output and use the approved guardrailIdentifier and guardrailVersion.

The response contains action and assessments[].contextualGroundingPolicy.filters[], including type, score, threshold, and detected. Route an intervention to a cache rejection rather than returning the unchecked candidate. action: "NONE" means the configured guardrail did not intervene; it does not certify authorization, freshness, or every business constraint. See 3.1.6 for the broader response-evidence verification boundary.

Grounding and relevance thresholds range from 0 to 0.99; 1 is invalid. These are independent of the query-embedding similarity threshold. Configure and evaluate each against its own labeled outcomes. If validation is unavailable or inconclusive, treat the candidate as a miss under a policy that requires validation, then use the ordinary generation and validation path. Similarity nominates a candidate, and the answer check decides its release: an intervention is routed to a cache rejection.

Similarity nominates a candidate, and the answer check decides its releaseThe application searches stored queries in ElastiCache, receives the returned score values, sorts them highest first and rejects the top result when its score is below min_similarity. It then sends the incoming query, the authorized evidence and the candidate answer to ApplyGuardrail. An intervention is routed to a cache rejection. When the action is NONE, the candidate is accepted only if scope and freshness also pass.ApplyGuardrailElastiCacheApplicationReject whenscore < min_similarityCache rejectionScope andfreshness also passalt[Intervention-][actionNONE]Search stored queriesreturned score valuesquery, grounding_source,candidate answeraction, assessments

Key distinctions and trade-offs

A strict similarity threshold reduces some false hits but cannot eliminate semantic ambiguity. Deterministic product and version gates can reject a wrong-context candidate more directly than raising a global threshold.

Validation consumes time and resources, so compare the whole accepted-hit path with uncached generation. A cache that requires expensive evidence retrieval and checking can still be useful, but generation-call avoidance alone does not establish net savings. Measure costs through the attribution approach in 4.1.1.

For multi-turn use, AWS recommends combining the current message with relevant facts and recent messages from conversation memory instead of embedding the entire raw dialogue. That reduces irrelevant history, but does not prove equivalent conversation state. If critical context cannot be represented reliably, bypass semantic reuse for that turn.

Architecture / decision scenario

A standalone support endpoint answers versioned VPN installation questions. ElastiCache returns a high-scoring candidate for “remove the VPN client” because its cached question concerns “install the VPN client.” The product and version filters pass, but the answer describes the wrong action.

The team labels this pair as unsafe reuse during calibration and measures how often similar pairs pass candidate thresholds. At runtime, an action-compatibility check rejects the candidate. For an eligible installation paraphrase, ApplyGuardrail checks the stored answer against the incoming question and authorized manual excerpts. A relevance or grounding intervention sends the request to generation instead of releasing the candidate.

A conversational turn saying “What about the other laptop?” follows a separate policy. The team resolves the referent from conversation memory or bypasses reuse; it does not apply the standalone contextual-grounding feature as if it supported conversational QA. The released threshold is supported by measured safe reuse, not by a benchmark hit rate.

Common exam traps

  • Treating query similarity as proof that the cached answer is relevant.
  • Copying a similarity comparison onto a distance output without checking direction.
  • Selecting a threshold solely to maximize hits.
  • Applying one threshold across incompatible embeddings or context partitions.
  • Confusing query similarity, grounding, and answer relevance scores.
  • Assuming contextual grounding checks support arbitrary conversational chatbots.

What to remember

Similarity selects a candidate; answer relevance, evidence, authorization, and freshness determine reuse. Calibrate on safe-reuse labels and near misses. Use ApplyGuardrail for supported independent output checks, keep its thresholds separate from vector similarity, and fall back when the required validation cannot establish acceptance.

Knowledge check

References