Skip to main content

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

Metadata

Release files for agentidemp-py 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agentidemp-py 0.1.0
File Size Uploaded Provenance
agentidemp_py-0.1.0.tar.gz 6.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for agentidemp-py 0.1.0
File Interpreter ABI Platform
agentidemp_py-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.7 kB

Release files / agentidemp_py-0.1.0.tar.gz

Download URL agentidemp_py-0.1.0.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
1a8c44ecc83539a06095ff4952e05020af41ebb85f57b7850fe8e2cf1526f21c
BLAKE2b-256 checksum
How to use checksums
a5f13b9a4b2dd04dd0f6f7b4b94fe3c7938f1ab50c6acd45254c5705d8efe5d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Release files / agentidemp_py-0.1.0-py3-none-any.whl

Download URL agentidemp_py-0.1.0-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a274eed3f171da9c2bf823a104ef94185449b94829bf6b6602712b9036f1a656
BLAKE2b-256 checksum
How to use checksums
72fbbaccbaed95869cc9a569358432a0389471a75a6c2eb13ba7682fb4d87acb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Security for agentidemp-py 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page