docs(examples): add prompt-caching example covering 3 patterns#1480
Open
HermeticOrmus wants to merge 1 commit intoanthropics:mainfrom
Open
docs(examples): add prompt-caching example covering 3 patterns#1480HermeticOrmus wants to merge 1 commit intoanthropics:mainfrom
HermeticOrmus wants to merge 1 commit intoanthropics:mainfrom
Conversation
The examples/ directory has agents, batch, streaming, structured outputs, thinking, and MCP tools but no prompt-caching example. The feature is heavily documented on platform.claude.com but reading the SDK examples wouldn't tell you it exists. This adds one runnable file covering the three patterns most users hit: 1. Cache the system prompt (chatbots, long instructions). 2. Cache system + tool definitions (agent loops). 3. Cache long static context (RAG / Q&A over a fixed document). Each section has a first call that creates the cache and a second call that hits it. show_usage() prints input / cache_create / cache_read / output token counts so the operator can verify the cache hit.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
The
examples/directory has agents, batch, streaming, structured outputs, thinking, and MCP tools — but no prompt-caching example. The feature is documented at platform.claude.com, but a developer browsing the SDK won't find a runnable demonstration of where to putcache_control={\"type\": \"ephemeral\"}, what the response usage fields look like on a hit, or which patterns are worth caching.This adds one runnable file covering the three patterns most callers actually use.
What changed
examples/prompt_caching.py— one__main__script with three numbered sections:Each section has a first call that creates the cache and a second call that hits it. A
show_usage()helper printsinput / cache_creation / cache_read / outputtoken counts so the reader can confirm the hit visually.The script padding ensures each cache target exceeds the per-model floor (~1024 tokens) so the cache actually takes effect when run.
How to test
Expected: section [1] first-call shows non-zero
cache_creation_input_tokens; second-call shows non-zerocache_read_input_tokens. Same shape for [2] and [3].Linted clean:
ruff check examples/prompt_caching.py(passes) andruff format --check examples/prompt_caching.py(no changes). Project line-length 120 respected.Notes
examples/. An async twin (prompt_caching_async.py) is a natural follow-up if maintainers prefer the same coverage in both.claude-sonnet-4-5-20250929(the most-used model acrossexamples/).cache_creation_input_tokens/cache_read_input_tokensis recommended before merge.