Expand description
§prompt-cache-key
Stable Anthropic prompt-cache scope hashes.
Anthropic’s prompt cache hits when the prefix (system + tools, up to a
cache_control breakpoint) is byte-identical to a previously seen
request. Coordinating that across workers needs a deterministic scope
key everyone can compute locally.
compute_cache_key walks (model, system, tools) into a canonical
byte stream and returns a SHA-256 hex digest prefixed with the model:
use prompt_cache_key::{compute_cache_key, System};
let key = compute_cache_key("claude-opus-4-7", System::Text("You are helpful."), None);
assert!(key.starts_with("anthropic-cache:claude-opus-4-7:sha256:"));Anything AFTER the last cache_control breakpoint in system is
excluded from the key because it isn’t part of the cached scope.
find_breakpoints returns the indices of those markers if you need
to inspect them yourself.
use prompt_cache_key::find_breakpoints;
use serde_json::json;
let blocks = json!([
{"type": "text", "text": "a"},
{"type": "text", "text": "b", "cache_control": {"type": "ephemeral"}},
{"type": "text", "text": "c"},
]);
assert_eq!(find_breakpoints(&blocks), vec![1]);Companion to llm-message-hash,
which hashes the full request for idempotency rather than just the
cache scope.
Enums§
- System
- System prompt input form. Anthropic accepts either a plain string or a list of content blocks; this enum mirrors that surface.
Constants§
Functions§
- canonical_
json - Serialize
valueas JSON with recursively sorted object keys and the compact separator","/":"(no spaces). This matches Python’sjson.dumps(..., sort_keys=True, separators=(",", ":"), ensure_ascii=False). - compute_
cache_ key - Stable scope key for
(model, system, tools). - find_
breakpoints - Return zero-based indices of blocks carrying a non-null
cache_control. - scope_
blocks - Return the prefix of
blocksup to and including the LASTcache_controlmarker. If no marker is present, the full list is returned. Non-arrays produce an empty list.