context-hygiene
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
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
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: 1,847 | Grade: D
- "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: 1,847 → 1,124 (save 723)
After: The same conversation, pruned to 10 segments with no contradictions and no deadweight.
Tokens: 1,124 | Grade: B | Recoverable: 723 tokens (39%)
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 scoredoes 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file context_hygiene-0.3.1.tar.gz.
File metadata
- Download URL: context_hygiene-0.3.1.tar.gz
- Upload date:
- Size: 45.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a6d8e944bc98ef5a91859a9f61031a24ed5e68942f6bb1fbf88f24ffa551e742
|
|
| MD5 |
76257ce266969c753d17b42b27a89f1f
|
|
| BLAKE2b-256 |
2e93536707944b4276bb31f3d57fb8cd1bea2c154c1bf69f7fe9dc0295e4651b
|
Provenance
The following attestation bundles were made for context_hygiene-0.3.1.tar.gz:
Publisher:
release.yml on AreteDriver/context-hygiene
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
context_hygiene-0.3.1.tar.gz -
Subject digest:
a6d8e944bc98ef5a91859a9f61031a24ed5e68942f6bb1fbf88f24ffa551e742 - Sigstore transparency entry: 2205351090
- Sigstore integration time:
-
Permalink:
AreteDriver/context-hygiene@26aa59e15264a2390034ebbab693bf4c506f113b -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/AreteDriver
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@26aa59e15264a2390034ebbab693bf4c506f113b -
Trigger Event:
push
-
Statement type:
File details
Details for the file context_hygiene-0.3.1-py3-none-any.whl.
File metadata
- Download URL: context_hygiene-0.3.1-py3-none-any.whl
- Upload date:
- Size: 41.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7a3b2c7b39ea7447e30a14ff7e84861425902aa3235368f4b3b8fb96fde8ed7c
|
|
| MD5 |
70cf19d2c992464be4c5ae165b5d26ba
|
|
| BLAKE2b-256 |
7c47ffa74603dadb2bd30c7496c1e3567b3cbe03bdfce2d122ffa02ae3294797
|
Provenance
The following attestation bundles were made for context_hygiene-0.3.1-py3-none-any.whl:
Publisher:
release.yml on AreteDriver/context-hygiene
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
context_hygiene-0.3.1-py3-none-any.whl -
Subject digest:
7a3b2c7b39ea7447e30a14ff7e84861425902aa3235368f4b3b8fb96fde8ed7c - Sigstore transparency entry: 2205351122
- Sigstore integration time:
-
Permalink:
AreteDriver/context-hygiene@26aa59e15264a2390034ebbab693bf4c506f113b -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/AreteDriver
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@26aa59e15264a2390034ebbab693bf4c506f113b -
Trigger Event:
push
-
Statement type: