prompt-cache-key 0.1.0

Stable Anthropic prompt-cache scope hashes. Given (system, tools, model), produce a deterministic key that survives benign reordering.
Documentation
  • Coverage
  • 90%
    9 out of 10 items documented1 out of 6 items with examples
  • Size
  • Source code size: 25.39 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 375.78 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 5s Average build duration of successful builds.
  • all releases: 5s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • MukundaKatta/prompt-cache-key-rs
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • MukundaKatta

prompt-cache-key

Crates.io Documentation License

Stable Anthropic prompt-cache scope hashes.

Anthropic's prompt cache hits when the prefix (system plus tools, up to a cache_control breakpoint) is byte-identical to a previously seen request. This crate produces a deterministic key from (model, system, tools) that survives benign reordering of JSON keys.

Install

[dependencies]
prompt-cache-key = "0.1"
serde_json = "1"

Use

use prompt_cache_key::{compute_cache_key, System};
use serde_json::json;

let key = compute_cache_key(
    "claude-opus-4-7",
    System::Text("You are helpful."),
    Some(&json!([
        {"name": "search", "description": "", "input_schema": {}},
    ])),
);
assert!(key.starts_with("anthropic-cache:claude-opus-4-7:sha256:"));

When system contains a cache_control marker, anything after the last marker is excluded from the key, matching Anthropic's cached prefix.

use prompt_cache_key::{compute_cache_key, System};
use serde_json::json;

let blocks = json!([
    {"type": "text", "text": "stable", "cache_control": {"type": "ephemeral"}},
    {"type": "text", "text": "this changes per request"},
]);
let key = compute_cache_key("claude-opus-4-7", System::Blocks(&blocks), None);

Pairs with llm-message-hash (hashes the full request, not just the cache scope).

License

MIT