Skip to main content

ctxdrift

Your agent read 40 files. Then the context window filled up. What survived?

ctxdrift audits the moment every AI coding agent is afraid to talk about: compaction. It parses your session transcripts, finds every context-window summary injection, and tells you exactly what your agent forgot — files erased, decisions lost, work re-done, secrets dropped from the record.

tests PyPI Python License: MIT Zero dependencies

pip install ctxdrift


The problem nobody measures

Every AI coding agent — Claude Code, Codex, OpenCode, your own agent loop — eventually hits the context window limit. When it does, the runtime silently summarizes everything that came before and throws the original away.

Nobody audits that moment. Until now:

  • Your agent read /payments/refund.py 4 times before compaction. Does the summary mention it? ctxdrift checks.
  • Your agent declared "Decision: we will route refunds through the gateway adapter" — then the summary crushed it to "made some edits." ctxdrift catches the decision loss.
  • Your agent re-read the same file after compaction that it already read before. That's a forgetting signature. ctxdrift counts it.
  • One 120K-token tool result right before compaction — the probable trigger. ctxdrift points at it.

This is not a token-counting dashboard. Those tell you what you spent. ctxdrift tells you what you lost.

Quick start

pip install ctxdrift

# audit one transcript
ctxdrift ~/.claude/projects/myproject/abc123.jsonl

# audit every session, worst first
ctxdrift ~/.claude/projects/

# machine-readable, gate your CI on it
ctxdrift ./transcripts --format json --min-severity warn

What a run looks like

ctxdrift v0.1.0
==========================================================
file        : /tmp/demo/session.jsonl
events      : 12
compactions : 1  (at line 10)

findings
----------------------------------------------------------
✗ CD04  [fail]  1 file(s) read before compaction #1 are absent from the
                injected summary text itself.
        - /app/payments/refund.py
! CD03  [warn]  1 file(s) re-read after compaction #1 although they were
                already read before it — the agent forgot it had them.
        - /app/payments/checkout.py (pre x1, post x1)
! CD05  [warn]  1 assistant decision/plan statement(s) before compaction #1
                never reappear afterwards.
        - Decision: we will route refunds through the gateway adapter...

The rules

Rule Severity What it catches
CD01 info Compaction count and boundary lines
CD02 warn Files touched pre-compaction, never seen again post
CD03 warn Re-read loop across a boundary — the classic forgetting signature
CD04 fail File read pre-compaction, absent from the summary text itself
CD05 warn Stated decisions/plans that vanish after compaction
CD06 info Shell commands erased from the record
CD07 info Oversized tool results right before compaction (probable trigger)
CD08 fail Rapid re-compaction — summary losing ground
CD09 info Env vars / config keys that vanish from the record
CD10 info Session never compacted (clean)

Exit codes are CI-gate ready: 0 clean · 1 warnings · 2 failures.

Works with

  • Claude Code — ~/.claude/projects/**/*.jsonl ✅
  • Codex CLI — ~/.codex/sessions/**/*.jsonl ✅
  • Any agent that writes JSONL with user/assistant/tool messages — generic mode ✅
  • Compaction markers auto-detected across runtimes, including [CONTEXT COMPACTION], This session is being continued from a previous conversation, and <summary> blocks.

Why ctxdrift

  • Zero dependencies. One Python file, stdlib only. No telemetry, no API keys, no cloud, no model calls. Everything stays on your machine.
  • Offline and deterministic. Same transcript in, same report out. Twice.
  • Fast and robust. Fuzz-tested against 50 adversarial transcripts — binary junk, truncated JSON, 200K-char blobs, unicode garbage — zero crashes. ReDoS-hardened regexes.
  • Honest. A finding cites its evidence. If the summary mentions the file, ctxdrift doesn't flag it. Exit codes map to real severities, not marketing.

Use cases

  • Before you resume a compacted session — check what the summary dropped before you trust "continue from where we left off."
  • Post-mortems — "the agent forgot the constraint we set two hours ago" now has a receipt, with line numbers.
  • Agent engineering — measure whether your system prompt, memory files, or summary prompts actually survive compaction. Iterate with evidence.
  • CI gate — run it over recorded agent sessions in your pipeline; fail the build when work gets silently erased.

Python API

import ctxdrift as cd

events = cd.parse_jsonl("~/.claude/projects/app/session.jsonl")
compactions = cd.find_compactions(events)
findings = cd.analyze(events, compactions)

for f in findings:
    print(f.rule, f.severity, f.message)

Part of the ZidoCode agent-audit suite

ctxdrift is one of a family of zero-dependency agent-audit tools:

Validation

Run against five real Hermes sessions (2,812–11,273 events each), exported from state.db with e2e/export_session.py:

session events compactions findings fail warn info
20261009_192032 11,273 22 71 9 30 32
20261013_030651 7,801 12 46 5 17 24
20261017_094726 6,888 54 111 51 23 37
20261019_030548 5,381 13 50 8 19 23
20261024_233747 2,812 4 17 4 4 9

CD02/CD04 only count files the agent actually opened — via a tool file_path argument or a shell command — not paths that merely appear in command output, tracebacks, URLs, or code. That change cut one session's CD04 evidence from 15,834 items to 33, and a hand check of the remaining items found 3 of 3 genuine (config and systemd unit files absent from the summary).

Reproduce on your own transcripts:

python3 e2e/export_session.py <session_id> transcript.jsonl   # Hermes state.db
python3 e2e/verify_cd04.py transcript.jsonl 1 5               # show evidence + provenance

Not yet validated against native Claude Code or Codex transcripts — only Hermes exports.

License

MIT — see LICENSE.

If ctxdrift caught something your summary dropped, that's a star. ⭐ It means the audit worked.

Metadata

Release files for ctxdrift 0.1.3

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

Source distribution (sdist)

Source distribution for ctxdrift 0.1.3
File Size Uploaded Provenance Actions
ctxdrift-0.1.3.tar.gz 15.8 kB Without provenance Details for ctxdrift-0.1.3.tar.gz

Built distribution (wheel)

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

Total release size: 30.0 kB

Release files / ctxdrift-0.1.3.tar.gz

Download URL ctxdrift-0.1.3.tar.gz
Size 15.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4adaab3fb35e12e70909c42fdbc2ba17f286c0006a9e0aa9680eb38fcccbab53
BLAKE2b-256 checksum
How to use checksums
56bdf7c16c6c69d28fe7aae0ddf70429ac7a8160704deaac655c22abfbba08b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.18 {"installer":{"name":"uv","version":"0.12.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / ctxdrift-0.1.3-py3-none-any.whl

Download URL ctxdrift-0.1.3-py3-none-any.whl
Size 14.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3dfc9a86affdda251edb9d0065080cd27fa71558bed67862579d35f418a91f66
BLAKE2b-256 checksum
How to use checksums
25b1526d72c15c9c5ec713361b042f3bf229a2f5acdee5f7bc9bed2102dcee09
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.18 {"installer":{"name":"uv","version":"0.12.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for ctxdrift 0.1.3

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.

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