Skip to main content

contextdecay

Persistent, cross-session context decay detection for LLM apps.

contextdecay tracks how much your Claude session has drifted from its original context — across multiple sessions — and surfaces it on demand via decaycheck() or the contextdecay check CLI command.


Install

pip install contextdecay

Quick start

from anthropic import Anthropic
from contextdecay import ContextDecayDetector

client = Anthropic()
detector = ContextDecayDetector(client, persist="./myapp.decay")

# Normal calls — zero overhead
response = detector.messages.create(
    model="claude-haiku-4-5-20251001",
    system="You are a customer support assistant for RetailCo.",
    messages=[{"role": "user", "content": "What is your return policy?"}],
    max_tokens=200,
)

# Check drift on demand
report = detector.decaycheck()
print(report)

Output:

DecayReport (sessions=3, turns=18)
  cumulative : score=0.721 | on_track | OK
  recent     : score=0.812 | on_track | OK

How it works

Session 1, msgs 1–5  →  original context fingerprint captured + saved to .decay file
Session N, any turn  →  response embedded locally (~20ms) — zero API cost
decaycheck()         →  cumulative score (vs original) + recent score (vs last check)
                     →  if triggered: Haiku explains what drifted
                     →  checkpoint updated

Original context = system prompt + first 5 user+assistant messages. Fingerprint = persisted to a local .decay JSON file — survives across sessions.


CLI

# Check drift now (requires ANTHROPIC_API_KEY)
contextdecay check
contextdecay check --file ./myapp.decay

# Inspect fingerprint metadata (no API call)
contextdecay status
contextdecay status --file ./myapp.decay

# Reset — wipe fingerprint and start fresh
contextdecay reset
contextdecay reset --file ./myapp.decay

DecayReport

report = detector.decaycheck()

# Cumulative: vs original context (session 1)
report.cumulative.score        # 0.38
report.cumulative.level        # DecayLevel.SIGNIFICANT
report.cumulative.triggered    # True
report.cumulative.explanation  # "Responses shifted from retail support to..."

# Recent: vs last decaycheck() call
report.recent.score            # 0.71
report.recent.level            # DecayLevel.ON_TRACK
report.recent.triggered        # False

# Session metadata
report.session_count           # 4
report.total_turns             # 23

Decay levels

Score Level Meaning
≥ 0.55 on_track Aligned with original context
0.40 – 0.54 mild Some drift
0.25 – 0.39 significant Noticeable drift
< 0.25 severe Serious misalignment

Configuration

detector = ContextDecayDetector(
    client,
    persist="./myapp.decay",       # where to store the fingerprint
    threshold=0.40,                 # trigger sensitivity
    explain=True,                   # Haiku explains drift on trigger
    embedding_model="all-MiniLM-L6-v2",
    explainer_model="claude-haiku-4-5-20251001",
)

Session management

# Check if original context has been captured yet
detector.is_fingerprinted   # True after 5 messages

# Current session and turn counts
detector.session            # 3
detector.turn               # 18

# Wipe everything and start fresh
detector.reset()

Cost and latency

Operation Extra tokens Cost Latency
Normal API call 0 $0 ~20ms (local embed)
decaycheck() — no trigger 0 $0 <5ms
decaycheck() — triggered ~300 ~$0.0003 +500ms
First run (model download) 0 $0 ~3s one-time

Run the example

ANTHROPIC_API_KEY=your_key python3 examples/quickstart.py

Run tests

pip install -e ".[dev]"
pytest tests/ -v

Roadmap

  • Multi-anchor support
  • Decay history timeline
  • Streaming support
  • OpenAI / Gemini adapters
  • Web dashboard

License

MIT

Metadata

Release files for contextdecay 0.2.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 contextdecay 0.2.0
File Size Uploaded Provenance
contextdecay-0.2.0.tar.gz 15.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for contextdecay 0.2.0
File Interpreter ABI Platform
contextdecay-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 30.6 kB

Release files / contextdecay-0.2.0.tar.gz

Download URL contextdecay-0.2.0.tar.gz
Size 15.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4ebf12726de41b4eb25f97a8d4d708d66b2df87bbf4f6c555a3c998bd7efc26e
BLAKE2b-256 checksum
How to use checksums
fc847cf2bdae5d548cba314528e4d823617f013f688f3b8f07ca56e44aed4954
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / contextdecay-0.2.0-py3-none-any.whl

Download URL contextdecay-0.2.0-py3-none-any.whl
Size 15.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cb8702e4fbcb8fd91131fc829aa2df7ed759f5f3743a26e3bba47c0e78b6cdad
BLAKE2b-256 checksum
How to use checksums
b0ee65153af65a1aff40786beb0069d16cdafeb028f12b92506bb721e72cb52f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Security for contextdecay 0.2.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.2.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