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 contextctx-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.

Download files

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

Source Distribution

context_hygiene-0.3.2.tar.gz (50.0 kB view details)

Uploaded Source

Built Distribution

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

context_hygiene-0.3.2-py3-none-any.whl (44.7 kB view details)

Uploaded Python 3

File details

Details for the file context_hygiene-0.3.2.tar.gz.

File metadata

  • Download URL: context_hygiene-0.3.2.tar.gz
  • Upload date:
  • Size: 50.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for context_hygiene-0.3.2.tar.gz
Algorithm Hash digest
SHA256 53bfebf46438d724630496469fe252c28fd1c9115b38a6870f6b7c41c663cd5f
MD5 455bbcb478cc3a9eb5ba0e1fd66c34c5
BLAKE2b-256 2ddd3b417cc36b08e44ff7e82c129473e6ffe22dd8b9c808f0aa81b21c824b73

See more details on using hashes here.

Provenance

The following attestation bundles were made for context_hygiene-0.3.2.tar.gz:

Publisher: release.yml on AreteDriver/context-hygiene

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file context_hygiene-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: context_hygiene-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 44.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for context_hygiene-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 201684e2562e45cbf7d625a9000a962e8f86089cf5aaa2034a186619447cdef9
MD5 93a2df21b23f10750fa55cd6f767a393
BLAKE2b-256 07f0e2fb535c3904e256e6b3b9c39e172591dfac80716ce9f95ecbef855d09e9

See more details on using hashes here.

Provenance

The following attestation bundles were made for context_hygiene-0.3.2-py3-none-any.whl:

Publisher: release.yml on AreteDriver/context-hygiene

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 files

0.3.1

2 files

0.2.1

2 files

0.2.0

2 files

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