claude-code - 💡(How to fix) Fix Expose context window utilization to hooks and as machine-readable output [2 comments, 3 participants]

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…
GitHub stats
anthropics/claude-code#49226Fetched 2026-04-17 08:47:13
View on GitHub
Comments
2
Participants
3
Timeline
6
Reactions
0
Timeline (top)
commented ×2labeled ×2cross-referenced ×1referenced ×1

Code Example

{
  "session_id": "abc123",
  "tool_name": "Edit",
  "tool_input": {},
  "tool_response": {},
  "context": {
    "tokens_used": 487000,
    "tokens_available": 1000000,
    "utilization": 0.487
  }
}

---

{
  "tokens_used": 487000,
  "tokens_available": 1000000,
  "utilization": 0.487,
  "components": {
    "conversation": 320000,
    "system_prompt": 15000,
    "claude_md": 8000,
    "tool_definitions": 45000,
    "auto_memory": 3000
  }
}
RAW_BUFFERClick to expand / collapse

The Ask

Expose current context window utilization (tokens used / tokens available) in two places:

  1. As a field in the PostToolUse hook stdin JSON (e.g. "context_utilization": 0.47)
  2. As machine-readable output from /context --json

Why

We're building an agent memory system where Claude Code writes decisions, errors, and learnings to a persistent vector store (mcp-memory-service) throughout a session. The challenge is knowing when to flush memories before context gets compressed.

Currently available:

  • PreCompact hook — fires when compaction happens, but by then it's too late for a thoughtful memory write. It's the fire alarm, not the smoke detector.
  • Tool call counting — crude proxy that doesn't account for varying response sizes.
  • Manual /context — not machine-parseable, requires human intervention.

What we need is a gauge, not just an alarm. A context utilization metric would let us:

  • Trigger memory writes at 50% saturation (while there's still rich context to capture)
  • Implement context health handoff — summarize and offload to external memory before compaction compresses details away
  • Build smarter pre-compaction hooks that prioritize what to preserve

Proposed Shape

PostToolUse stdin addition:

{
  "session_id": "abc123",
  "tool_name": "Edit",
  "tool_input": {},
  "tool_response": {},
  "context": {
    "tokens_used": 487000,
    "tokens_available": 1000000,
    "utilization": 0.487
  }
}

Or /context --json:

{
  "tokens_used": 487000,
  "tokens_available": 1000000,
  "utilization": 0.487,
  "components": {
    "conversation": 320000,
    "system_prompt": 15000,
    "claude_md": 8000,
    "tool_definitions": 45000,
    "auto_memory": 3000
  }
}

Thanks to the entire Claude Code team — the hook system, the extensibility, and the overall developer experience are genuinely exceptional. The fact that we can build a Kurzweil-inspired hierarchical memory system on top of Claude Code's hook architecture is a testament to how well it was designed. Really appreciate all the work that's gone into this platform.

extent analysis

TL;DR

To address the need for a context utilization metric, consider adding a context_utilization field to the PostToolUse hook stdin JSON and implementing a machine-readable output from the /context --json command.

Guidance

  • Review the proposed JSON shapes for PostToolUse stdin and /context --json to ensure they meet the requirements for exposing context window utilization.
  • Determine the calculation method for tokens_used and tokens_available to accurately reflect context utilization.
  • Consider implementing a threshold-based system to trigger memory writes when context utilization reaches a certain percentage (e.g., 50%).
  • Evaluate the feasibility of integrating the proposed solution with the existing hook system and memory service.

Example

{
  "session_id": "abc123",
  "tool_name": "Edit",
  "tool_input": {},
  "tool_response": {},
  "context": {
    "tokens_used": 487000,
    "tokens_available": 1000000,
    "utilization": 0.487
  }
}

This example illustrates the proposed PostToolUse stdin JSON shape with the added context field.

Notes

The solution relies on accurate calculation of tokens_used and tokens_available. Ensure that these values are correctly updated and reflected in the proposed JSON outputs.

Recommendation

Apply the proposed workaround by adding the context_utilization field to the PostToolUse hook stdin JSON and implementing the machine-readable output from /context --json. This will provide the necessary gauge for context utilization, enabling more informed memory management decisions.

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