Skip to main content

Crate prompt_cache_key

Crate prompt_cache_key 

Source
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§

KEY_PREFIX

Functions§

canonical_json
Serialize value as JSON with recursively sorted object keys and the compact separator "," / ":" (no spaces). This matches Python’s json.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 blocks up to and including the LAST cache_control marker. If no marker is present, the full list is returned. Non-arrays produce an empty list.