Skip to main content

Crate prompt_hash

Crate prompt_hash 

Source
Expand description

§prompt-hash

Deterministic cache key for an LLM prompt.

Inputs:

  • model — model id (already normalized by claude-cost/openai-cost if you care).
  • messages[(role, content)].
  • temperature — quantized to 2 decimal places to avoid floating point cache misses.

Output: 64-char hex SHA-256.

Use as a request-level cache key. For semantic cache keys (matching prompts with the same meaning but different wording), pair with semantic-cache-key.

§Example

use prompt_hash::key;
let k1 = key(
    "claude-sonnet-4-5",
    &[("user", "what is 2+2?")],
    1.0,
);
let k2 = key(
    "claude-sonnet-4-5",
    &[("user", "what is 2+2?   ")], // trailing ws ignored
    1.0,
);
assert_eq!(k1, k2);
assert_eq!(k1.len(), 64);

Functions§

key
Compute a 64-char hex cache key.