Skip to main content

context-hygiene

PyPI version Python 3.10+

Context window hygiene analyzer for LLM conversations.

Heuristic detection of staleness, contradictions, deadweight, and compression opportunities in CLAUDE.md files, prompt chains, and agent configs. No LLM required for basic analysis.


Install

pip install context-hygiene

Optional extras:

pip install "context-hygiene[anthropic]"  # AI-powered deep analysis
pip install "context-hygiene[watch]"      # Live file monitoring

Quick Start

# Audit a CLAUDE.md file (heuristics, no LLM)
ctx-hygiene audit CLAUDE.md

# Quick staleness score
ctx-hygiene score CLAUDE.md

# Auto-clean deadweight and stale segments
ctx-hygiene clean CLAUDE.md

# View audit history
ctx-hygiene history

# Check license and config
ctx-hygiene status

# Watch a file for changes (re-audit on save)
ctx-hygiene audit CLAUDE.md --watch

# Enforce grade threshold in CI
ctx-hygiene audit CLAUDE.md --fail-under B

# Output SARIF for GitHub Code Scanning
ctx-hygiene audit CLAUDE.md --format sarif > results.sarif

# Shell completions
ctx-hygiene completion bash >> ~/.bashrc
ctx-hygiene completion zsh >> ~/.zshrc
ctx-hygiene completion fish | source

GitHub Action

Use context-hygiene in your workflows:

- uses: AreteDriver/context-hygiene@v1
  with:
    files: "CLAUDE.md"
    fail-under: "B"
    format: "sarif"

Pre-Commit Hook

Keep your context files clean before committing:

# .pre-commit-config.yaml
repos:
  - repo: https://github.com/AreteDriver/context-hygiene
    rev: v0.3.1
    hooks:
      - id: context-hygiene

The hook runs ctx-hygiene score on all *.md, *.txt, and *.jsonl files. Fail the build if the grade drops below a threshold:

- id: context-hygiene
  args: ["audit", "--fail-under", "B"]

How It Works

context-hygiene parses structured context files into conversation segments and runs four heuristic analysis passes:

1. Staleness Detection

Identifies potentially outdated segments based on:

  • Position decay — earlier segments in a long conversation are more likely stale
  • Language patterns — detects corrections ("actually", "instead", "scratch that"), restarts ("let me start over"), and explicit staleness ("old", "deprecated")
  • Error content — large traceback blocks after a fix is applied
  • Short mid-conversation messages — often fragmented or superseded context

Scored 0–1 per segment (0 = fresh, 1 = completely stale).

2. Contradiction Detection

Finds conflicting instructions between user/system segments using regex pattern matching:

  • Positive vs. negative directives ("use X" vs. "don't use X")
  • Opposing adverbs ("always" vs. "never")
  • Incompatible toggles ("enable" vs. "disable", "include" vs. "exclude")

Flagged with confidence score (currently fixed at 0.7; deep mode uses LLM for refinement).

3. Deadweight Detection

Identifies zero-influence messages that consume tokens without shaping output:

  • Acknowledgment-only messages ("ok", "thanks", "got it")
  • Filler words ("hmm", "um", "well")
  • Assistant confirmation preambles ("Sure, I'd be happy to...")
  • Exact duplicates of earlier segments
  • Empty or whitespace-only messages

4. Compression Detection

Finds opportunities to condense without information loss:

  • Consecutive same-role runs (3+ messages from user/assistant in a row)
  • Large code blocks that could be referenced instead of inlined
  • Verbose assistant explanations where prose exceeds code content

Supported Formats

Format Extension Description
Claude exports .json Claude conversation JSON exports
Codex sessions .jsonl Codex CLI session transcripts
OpenAI / ChatGPT .json ChatGPT conversation exports
Markdown conversations .md, .txt Generic markdown with ## User / ## Assistant markers
AI instruction files .md CLAUDE.md, AGENTS.md, INSTRUCTIONS.md — split by headers

Fast vs. Deep Mode

Fast (default) Deep (Pro)
How it works Regex + heuristic scoring LLM semantic analysis
Speed Milliseconds Seconds to minutes
Cost $0 LLM API tokens
Staleness Pattern-based Semantic drift detection
Contradictions Regex pairs LLM cross-references
Deadweight Acknowledgment/filler filters Semantic relevance scoring
Compression Token thresholds Content summarization

Fast mode is sufficient for most use cases. Deep mode is useful when heuristic patterns miss nuanced semantic drift.


Free vs. Pro

Feature Free Pro ($8/mo)
audit (fast mode) 10/month Unlimited
score / clean / history Unlimited Unlimited
audit --deep (AI analysis) — Yes
watch (live monitoring) — Yes

Subscribe Monthly ($8/mo) | Subscribe Yearly ($69/yr)

All 5 Tools Bundle: Monthly ($29/mo) | Yearly ($199/yr) — includes claudemd-forge, agent-lint, ai-spend, promptctl, context-hygiene

After purchase, activate via:

export CONTEXT_HYGIENE_LICENSE="CTHG-XXXX-XXXX-XXXXXXXXXXXXXXXX"

Or save to ~/.config/context-hygiene/license.


Programmatic API

You can also use context-hygiene from Python without shelling out to the CLI:

from context_hygiene import audit_file, score_file

# Full audit report
report = audit_file("CLAUDE.md")
print(report.grade)               # 'B'
print(report.tokens_recoverable)  # 1,247

# Quick score
score = score_file("conversation.json")
print(score.grade)      # 'C'
print(score.staleness)  # 0.34

Before / After Demo

Before: A messy 14-segment conversation with stale instructions, deadweight, and contradictions.

Tokens: 240  |  Grade: C
- "ok" (deadweight)
- "Sure, let me know..." (assistant preamble)
- "Actually, scratch that. Use poetry instead." (supersedes prior pip advice)
- "Never mind, let me start over. I'll use uv instead." (supersedes poetry)
- "Use pip for everything" vs "Don't use pip, use poetry" (contradiction)

Run the cleaner:

ctx-hygiene clean conversation.md --apply
# Pruning plan: remove 4/14 segments
# Tokens: 240 → 202 (save 38)

After: The same conversation, pruned to 10 segments with no contradictions and no deadweight.

Tokens: 202  |  Grade: B  |  Recoverable: 38 tokens (16%)

📺 Watch the live demo

What This Is (and Isn't)

context-hygiene is a practical heuristic tool, not a novel research metric. It doesn't measure "semantic entropy" or "information-theoretic density." It applies well-understood pattern-matching techniques to a specific problem: finding waste in LLM context windows.

If you're looking for:

  • Token-level compression → LLMLingua, Selective Context
  • Novelty scoring → Build your own embedding-based metric
  • A quick sanity check before sending a long context → ctx-hygiene score does exactly that

Community

Discord — Join the community

License

BSL-1.1 (Business Source License 1.1)

The core heuristic analyzer is free to use and modify. AI-powered deep analysis (--deep) and live file monitoring (watch) require a Pro license.

Metadata

Release files for context-hygiene 0.3.2

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

Source distribution (sdist)

Source distribution for context-hygiene 0.3.2
File Size Uploaded Provenance Actions
context_hygiene-0.3.2.tar.gz 50.0 kB With provenance Details for context_hygiene-0.3.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for context-hygiene 0.3.2
File Interpreter ABI Platform Actions
context_hygiene-0.3.2-py3-none-any.whl Python 3 none any Details for context_hygiene-0.3.2-py3-none-any.whl

Total release size: 94.7 kB

Release files / context_hygiene-0.3.2.tar.gz

Download URL context_hygiene-0.3.2.tar.gz
Size 50.0 kB
Tags Source
SHA-256 checksum
How to use checksums
53bfebf46438d724630496469fe252c28fd1c9115b38a6870f6b7c41c663cd5f
BLAKE2b-256 checksum
How to use checksums
2ddd3b417cc36b08e44ff7e82c129473e6ffe22dd8b9c808f0aa81b21c824b73
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 20, 2026.

Transparency log

Release files / context_hygiene-0.3.2-py3-none-any.whl

Download URL context_hygiene-0.3.2-py3-none-any.whl
Size 44.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
201684e2562e45cbf7d625a9000a962e8f86089cf5aaa2034a186619447cdef9
BLAKE2b-256 checksum
How to use checksums
07f0e2fb535c3904e256e6b3b9c39e172591dfac80716ce9f95ecbef855d09e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 20, 2026.

Transparency log

Security for context-hygiene 0.3.2

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.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for context_hygiene-0.3.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 20, 2026.

Transparency log

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