Skip to content

New serverless pattern - bedrock-semantic-cache-s3vectors-sam - #3262

Open
manishh-13 wants to merge 4 commits into
aws-samples:mainfrom
manishh-13:manishh-13-feature-bedrock-semantic-cache
Open

New serverless pattern - bedrock-semantic-cache-s3vectors-sam#3262
manishh-13 wants to merge 4 commits into
aws-samples:mainfrom
manishh-13:manishh-13-feature-bedrock-semantic-cache

Conversation

@manishh-13

@manishh-13 manishh-13 commented Aug 4, 2026

Copy link
Copy Markdown

New serverless pattern: bedrock-semantic-cache-s3vectors-sam

Serverless semantic cache for Amazon Bedrock using AWS Lambda and Amazon S3 Vectors.

An AWS Lambda function sits in front of Amazon Bedrock and returns a cached answer whenever an incoming prompt is semantically similar to one already answered, skipping the LLM call on repeats and near-repeats.

How it works

  • Lambda embeds the prompt with Amazon Titan Text Embeddings v2 (1024-dim).
  • Queries an Amazon S3 Vectors index for the nearest prior prompt (cosine similarity); the cached answer is stored in the vector metadata, so no separate database is needed.
  • HIT (similarity >= configurable threshold, within TTL, current epoch, passes negation-parity guard): returns the stored answer in ms at $0 LLM cost.
  • MISS: calls the Bedrock text model, stores embedding + answer + metadata, returns the fresh result.

Correctness features

  • Tunable similarity threshold (precision vs. hit rate).
  • Freshness TTL to bound staleness.
  • One-call force-invalidation via an epoch counter in SSM Parameter Store (no scan/delete).
  • Negation-parity guard (prevents "is X" vs "is NOT X" false hits).
  • Top-K retrieval so a stale near-neighbour never blocks a valid hit.

Notes

  • Fully serverless: S3 Vectors scales to zero; Lambda is stateless (cache survives cold starts/redeploys).
  • Access via IAM-signed Lambda function URL + optional app-level API key. Least-privilege IAM.
  • Model-agnostic: the answer model is a drop-in for any Amazon Bedrock model (Amazon Nova, Anthropic Claude, Meta Llama, Mistral) via the converse API; the embedding model is configurable as long as the index dimension matches.
  • Complements (does not replace) Bedrock native prompt caching, which is exact-prefix based.

Includes README.md, template.yaml (AWS SAM), example-pattern.json, and src/ (Python). Language: Python. Level: 300. Framework: AWS SAM.

Pattern-submission issue: #3263

Serverless semantic cache for Amazon Bedrock using AWS Lambda and Amazon
S3 Vectors. Returns a cached answer when an incoming prompt is
semantically similar to a prior one (cosine similarity threshold), with
freshness TTL, one-call epoch force-invalidation via SSM Parameter Store,
and a negation-parity guard. Cache persists in S3 Vectors (scales to
zero); Lambda is stateless. SAM template, Python.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants