Skip to main content

Idempotency keys for LLM agent retries. Deterministic content-derived keys (sha256-hex, UUIDv5, scoped). Pairs with cachebench miss-aware retry. Zero runtime deps.

Project description

agentidemp-py

PyPI Python License: MIT

Idempotency keys for LLM agent retries.

When an agent retries a request, on a 429, a timeout, or a cache miss-aware policy, the retry needs the same idempotency key as the first attempt. Otherwise the dedup layer treats it as a new request and you double-bill or double-dispatch.

agentidemp-py derives stable keys from request content, so retries get the same key automatically. Three forms: short sha256 hex, deterministic UUIDv5, and a scoped helper for per-tenant dedup.

Sibling to the Rust crate agentidemp-rs. Pairs with cachebench for miss-aware retry that keeps the same key across attempts.

Install

pip install agentidemp-py

Zero runtime deps. Stdlib hashlib + uuid only.

Use

from agentidemp import sha256_hex, uuid5_key, NAMESPACE_ANTHROPIC

body = b'{"model":"claude-sonnet-4-20250514","messages":[{"role":"user","content":"hi"}]}'

# Short hex form (35 chars: "ik_" + 32 hex):
key1 = sha256_hex(body)
# -> "ik_a3f9c1d8b2e7f046189f43a2b8e7c106"

# UUIDv5 form (36-char UUID string for `Idempotency-Key: <uuid>` headers):
key2 = uuid5_key(NAMESPACE_ANTHROPIC, body)
# -> "5fd2604e-..."

# Fresh random key when you don't have content to derive from:
from agentidemp import random_key
key3 = random_key()

Scoped keys

If the same prompt body might recur across tenants or users and you want per-scope dedup, prefix with a scope:

from agentidemp import scoped_sha256_hex, scoped_key

# Single scope + content (matches the Rust sibling API):
k = scoped_sha256_hex("tenant-42", body)

# Variadic convenience: scope plus any number of parts:
k = scoped_key("tenant-42", "user-7", body)

The scope and each part are separated by a null byte so "ab" + "c" does not collide with "a" + "bc".

Composing with cachebench

cachebench's miss-aware policy retries on a silent cache miss. Pair with agentidemp so the retry is recognized as the same request:

from agentidemp import sha256_hex

key = sha256_hex(request_body_bytes)
headers = {"Idempotency-Key": key, ...}
# retries built by your cachebench policy reuse the same body, so the
# same key flows through and your dedup layer recognizes the retry.

What it does NOT do

  • No HTTP. Returns a string. You set the header.
  • No response caching. That belongs to a different layer (see cachebench).
  • No tracking of which keys you have already used. Stateless by design.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agentidemp_py-0.1.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agentidemp_py-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file agentidemp_py-0.1.0.tar.gz.

File metadata

  • Download URL: agentidemp_py-0.1.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for agentidemp_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1a8c44ecc83539a06095ff4952e05020af41ebb85f57b7850fe8e2cf1526f21c
MD5 d17f4695c33788e272606731f2107947
BLAKE2b-256 a5f13b9a4b2dd04dd0f6f7b4b94fe3c7938f1ab50c6acd45254c5705d8efe5d8

See more details on using hashes here.

File details

Details for the file agentidemp_py-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: agentidemp_py-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for agentidemp_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a274eed3f171da9c2bf823a104ef94185449b94829bf6b6602712b9036f1a656
MD5 dceecc1604785ddd5b33ea9f48c4b670
BLAKE2b-256 72fbbaccbaed95869cc9a569358432a0389471a75a6c2eb13ba7682fb4d87acb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page