Skip to content

Add exercise-25: KV cache reuse demo (Latent Briefing) - #17

Open
cktricky wants to merge 2 commits into
mainfrom
feat/exercise-25-kv-cache-demo
Open

cktricky wants to merge 2 commits into
mainfrom
feat/exercise-25-kv-cache-demo

Conversation

@cktricky

@cktricky cktricky commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Adds scripts/extras/exercise-25/ demonstrating Anthropic prompt caching as a simple illustration of KV cache reuse from Ramp's Latent Briefing paper
  • Uses AnthropicBedrock client with cache_control on a large security policy, then sends 3 different queries showing cache creation vs cache read metrics
  • First exercise in the repo to use the raw anthropic SDK (via Bedrock wrapper)

Test plan

  • Script runs end-to-end with existing AWS credentials
  • All 3 queries return meaningful security analysis responses
  • Cache metrics are printed (populate when prompt caching is enabled for the model/region)
  • Verify with a caching-enabled model/region if available

🤖 Generated with Claude Code

Demonstrates Anthropic prompt caching as a simple illustration of the
KV cache reuse mechanism described in Ramp's Latent Briefing paper.
Uses AnthropicBedrock client with cache_control to show how shared
context is computed once and reused across multiple queries.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings August 2, 2026 17:21
@dryrunsecurity

dryrunsecurity Bot commented Aug 2, 2026

Copy link
Copy Markdown

DryRun Security

No summary was generated for this pull request.


All finding details can be found in the DryRun Security Dashboard.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new “exercise-25” under scripts/extras/ to demonstrate Anthropic prompt caching (as a practical KV-cache reuse example aligned with Ramp’s Latent Briefing concept) using the AnthropicBedrock client, and documents how to run/interpret the demo.

Changes:

  • Added a runnable Python demo that caches a large “security policy” system block and runs 3 queries while printing cache creation vs cache read usage metrics.
  • Added exercise instructions explaining the concept mapping to Latent Briefing and how to observe caching metrics.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
scripts/extras/exercise-25/kv_cache_demo.py Implements the KV-cache/prompt-caching demo using AnthropicBedrock and prints cache usage metrics across multiple calls.
scripts/extras/exercise-25/INSTRUCTIONS.md Documents the objective, background, and how to run the demo and interpret the caching metrics.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +18 to +20
client = AnthropicBedrock(
aws_region=os.environ.get("AWS_DEFAULT_REGION", "us-east-1"),
)
Comment on lines +41 to +44
- Remove the `cache_control` block and compare — all calls pay full input token cost
- Add more queries to observe continued cache hits within the 5-minute TTL
- Increase the policy size and observe how cache savings scale
- Try setting cache TTL to longer durations for persistent caching
Prompt caching does not work with us.anthropic.* inference profiles
since requests route across regions. Updated script output and
INSTRUCTIONS.md to explain this and note alternatives.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings August 2, 2026 22:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (2)

scripts/extras/exercise-25/kv_cache_demo.py:184

  • These TAKEAWAY lines currently imply that calls 2 and 3 will show cache-read metrics, but with the default us.anthropic.* cross-region model ID this will not happen (cache metrics remain 0). Align the messaging to be conditional on using a caching-enabled model/region so the output matches what users will actually observe by default.
print("TAKEAWAY: When prompt caching is active, calls 2 and 3 show")
print("cache_read tokens instead of cache_creation tokens. The model")
print("reuses pre-computed KV representations — the same mechanism")
print("Ramp's Latent Briefing paper uses to share context between")
print("orchestrator and workers.")

scripts/extras/exercise-25/kv_cache_demo.py:9

  • The docstring states that the policy is cached on the first call and reused on subsequent calls, but with the default cross-region model ID in this script prompt caching is not supported (as noted at the end of the script). This makes the docstring misleading; please phrase this as conditional on using a caching-enabled model/region.

This issue also appears on line 180 of the same file.

The large security policy is cached on the first call. Subsequent calls reuse
the cached KV representations, avoiding redundant computation.

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.

2 participants