Skip to main content

Crate agentidemp

Crate agentidemp 

Source
Expand description

Idempotency keys for LLM agent retries.

When an agent retries a request — whether due to a 429, a timeout, or a cache miss-aware policy (cachebench) — the retry should carry the same idempotency key as the first attempt so the server (or your dedup layer) recognizes it as a retry, not a new request.

agentidemp derives stable keys from the request content. Two flavors:

  • sha256_hex — short hex digest of the content, prefixed with ik_.
  • uuid_v5 — namespaced deterministic UUID, suitable for headers that expect UUID format (e.g. Idempotency-Key: <uuid>).

Plus random for the “I want a fresh key” case.

§Quick start

use agentidemp::{sha256_hex, uuid_v5, NAMESPACE_ANTHROPIC};

let body = serde_json::json!({"model": "claude", "messages": [{"role": "user", "content": "hi"}]});
let bytes = serde_json::to_vec(&body).unwrap();

let k = sha256_hex(&bytes);
assert!(k.starts_with("ik_"));
assert_eq!(k.len(), 3 + 32); // ik_ + 32 hex chars

let u = uuid_v5(&NAMESPACE_ANTHROPIC, &bytes);
assert_eq!(u.to_string().len(), 36);

Constants§

NAMESPACE_ANTHROPIC
A stable namespace UUID for Anthropic-bound requests. Use as the first arg to uuid_v5 to scope your keys.

Functions§

random
Generate a random UUID v4. For when you don’t want determinism — e.g. the very first attempt where you have no prior key to reuse.
scoped_sha256_hex
Combine scope and content into a single hashed key. Useful when the same content might recur across users/tenants and you want per-scope deduplication.
sha256_hex
Compute a 16-byte sha256 prefix of content, hex-encoded, with the ik_ marker prefix. Useful for Idempotency-Key HTTP headers that don’t require a UUID format.
uuid_v5
Compute a deterministic UUID v5 for content under namespace.