claude-code - 💡(How to fix) Fix Feature request: expose context window utilization to hooks (CLAUDE_CONTEXT_PERCENT)

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Root Cause

We built a Write-Ahead Log (WAL) system for Claude Code that prevents context loss across compaction. It works — 100% recovery on first test. But it can't prevent gradual drift during long sessions because hooks have no way to know how full the context is.

RAW_BUFFERClick to expand / collapse

The Ask

Expose context window utilization as an environment variable to hook scripts. One env var: CLAUDE_CONTEXT_PERCENT (integer 0-100), set on every hook invocation.

Why

We built a Write-Ahead Log (WAL) system for Claude Code that prevents context loss across compaction. It works — 100% recovery on first test. But it can't prevent gradual drift during long sessions because hooks have no way to know how full the context is.

The WAL refresh system re-injects operating principles and checkpoint data at escalating intervals. Today we use tool-count as a rough proxy for context consumption. It's imprecise — 30 small file edits consume far less context than 30 large file reads. We just violated our own branch discipline principle because the refresh hadn't fired at the right moment.

With CLAUDE_CONTEXT_PERCENT, hooks could fire at exactly the right moments (25%, 50%, 75%) instead of guessing from tool counts.

What Exists Today

  • PreCompact / PostCompact hooks fire when compaction happens — too late
  • No env var, no API, no status line metric for context percentage
  • /context command shows it interactively but is not automatable
  • Hook input JSON has no context metrics

What Would Work

Any of these:

  1. Env varCLAUDE_CONTEXT_PERCENT=73 available to all hook scripts (simplest)
  2. New hook eventContextThreshold that fires at configurable percentages
  3. Field in hook input JSON{"context_percent": 73} alongside cwd and tool_name

Option 1 is trivial to implement and enables everything else in userspace.

Concrete Use Case

The WAL system (github.com/jodybrownell/claude-wal) uses hooks to maintain context continuity. It currently:

  • Logs events via PostToolUse
  • Captures checkpoints via PreCompact
  • Replays state via PostCompact
  • Refreshes principles every N tool calls via Stop hook

With context percentage, the refresh becomes precise instead of approximated. The branch check, drift detection, and principle re-injection all fire at the right moments.

Impact

This unblocks any hook-based system that needs context awareness — not just the WAL. Cost monitoring, auto-summarization triggers, session health dashboards, proactive compaction — all become possible with one env var.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix Feature request: expose context window utilization to hooks (CLAUDE_CONTEXT_PERCENT)