Claude DCP — Dynamic Context Pruning for Claude Code
Intelligently manages conversation context to optimize token usage in Claude Code.
Inspired by opencode-dynamic-context-pruning for OpenCode, adapted for Claude Code's plugin and hooks architecture.
What DCP Catches That Claude Code Doesn't
Claude Code has built-in compaction, but it works differently. Here's what DCP adds:
| What Happens | Without DCP | With DCP |
|---|
| Same tool call again (60s later) | Permission prompt appears again | Blocked — "Use a different approach" |
| Transcript before compaction | Full duplicate outputs sent to summarizer | Early duplicates replaced with [Output deduplicated] |
| Input from failed command (4 turns ago) | Still in context | Replaced with [input removed — error occurred X turns ago] |
| Context at 150K tokens | No warning | "Context is ~150K tokens — consider /compact" |
| After compaction | Context refreshes silently | "Re-read files with line limits, tool signatures still tracked" |
| Session statistics | None visible | "DCP Savings: 2.3KB saved (~575 tokens est.)" |
DCP operates alongside Claude Code's native features, not replacing them. It preprocesses the transcript before native compaction runs, making that compaction more efficient.
Features
| Feature | How It Works | Native Equivalent |
|---|
| Tool Deduplication | Blocks duplicate tool calls (same tool + same args) via PreToolUse hook | None — Claude Code always prompts again |
| Error Input Purging | Removes inputs from failed tool calls after N turns via transcript optimization | None — native compaction keeps error context |
| Transcript Pre-Optimization | PreCompact hook removes duplicates BEFORE compaction runs | Native compaction summarizes everything at once |
| Context Nudges | Warns when context is large via UserPromptSubmit hook | None |
| Post-Compact Re-Injection | Re-injects reminders after compaction via PostCompact hook | None |
| Session Statistics | Tracks cumulative bytes/tokens saved per session | None |
Why Pre-Optimization Matters
Native compaction (/compact) is LLM-driven — it sends the full transcript to the model and asks it to summarize. DCP runs first and removes obvious waste:
┌─────────────────────────────────────────────┐
│ Before Native Compaction │
├─────────────────────────────────────────────┤
│ transcript.jsonl │
│ ├── "git status" output (duplicate) │ ← DCP removes this
│ ├── "git status" output (kept) │ ← DCP keeps this
│ ├── error from "npm install" (4 turns) │ ← DCP removes input, keeps error
│ ├── ... │
└─────────────────────────────────────────────┘
↓ DCP Pre-Optimization
┌─────────────────────────────────────────────┐
│ After DCP, Before Native Compaction │
├─────────────────────────────────────────────┤
│ transcript.jsonl (now smaller) │
│ ├── [Output deduplicated] │ ← Placeholder
│ ├── "git status" output (kept) │
│ ├── [input removed — error occurred] │ ← Error output preserved
│ ├── ... │
└─────────────────────────────────────────────┘
↓ Native Compaction
┌─────────────────────────────────────────────┐
│ Native Compaction Summary │
├─────────────────────────────────────────────┤
│ "• Ran git status—unchanged │
│ • npm install failed, retry needed │
│ • ..." │
└─────────────────────────────────────────────┘
The native summarizer sees less redundant content, produces a more accurate summary, and uses fewer tokens doing it.
Platform Support
| Platform | Status | Notes |
|---|
| macOS | Fully supported | Requires Python 3.9+ |
| Linux | Fully supported | Requires Python 3.9+ |
| Windows (WSL2) | Fully supported | Run Claude Code inside WSL2 |
| Windows (native) | Limited | See note below |
Windows native note: Claude Code's hook system currently has known issues on Windows
native (#19012,
#25832).
This affects all plugins with hooks, including official Anthropic plugins.
Hooks may fail silently or fire intermittently. Use WSL2 for full functionality.
Requirements
- Python 3.9+
- Claude Code CLI
Installation
From Plugin Directory (Development)
claude --plugin-dir /path/to/claude-dcp
Install Permanently
# Copy to your plugins directory
cp -r /path/to/claude-dcp ~/.claude/plugins/claude-dcp