[FEATURE] Queryable context usage + selective context purge

Resolved 💬 3 comments Opened Mar 28, 2026 by dstolts Closed Apr 30, 2026

Preflight Checklist

  • [x] I have searched existing requests and this feature hasn't been requested yet
  • [x] This is a single feature request (not multiple features)

Problem Statement

Claude Code provides a visual context usage indicator in the status bar, but there is no way to:

  1. Query context usage programmatically -- hooks, agents, and automation cannot read current context consumption to make decisions (e.g., auto-save at 70%, graceful shutdown at 85%)
  2. Inspect what is consuming context -- users cannot see which tool results, file reads, or agent returns are taking up space
  3. Selectively remove items from context -- the only option is /clear (lose everything) or /compact (lossy compression of everything). There is no way to drop specific large tool results while keeping the rest of the conversation intact.

This forces a binary choice: keep accumulating context until compaction fires (losing control of timing), or /clear and lose all context. For users running long autonomous sessions with scheduled agents, deploy monitors, and multi-phase sprints, there is no graceful middle ground.

Related but distinct from #11008 (token usage in hook inputs) which focuses on cost/token counts. This request is about inspecting and managing what is IN the context window.

Proposed Solution

1. /context command -- inspect context contents
/context

== Context Usage: 67% (134K / 200K tokens) ==

| # | Type       | Tokens | Age  | Summary                                   |
|---|------------|--------|------|-------------------------------------------|
| 1 | system     | 12K    | --   | System prompt + tool definitions          |
| 2 | claude.md  | 8K     | --   | CLAUDE.md + rules/ (14 files)             |
| 3 | tool:Read  | 6K     | 12m  | templates/commands/session.md             |
| 4 | tool:Agent | 18K    | 8m   | Explore agent: scan for sensitive content |
| 5 | tool:Bash  | 1K     | 5m   | git log --oneline -5                      |
| 6 | tool:Agent | 4K     | 3m   | Housekeeper timer return                  |
| 7 | assistant  | 85K    | --   | Claude responses (cumulative)             |

Drop items: /context drop 4,6 (removes specific items)
2. /context drop -- selective purge

Remove specific context items without clearing the session. Use cases:

  • Drop a large Explore agent result after extracting what you need
  • Drop old file reads that are no longer relevant
  • Drop stale tool results to make room before a large operation
  • Prune background agent returns that have been processed
3. Queryable context usage for hooks

Expose context_usage_pct in hook JSON input (PostToolUse, PreCompact):

{
  "session_id": "abc123",
  "context": {
    "used_tokens": 134000,
    "max_tokens": 200000,
    "usage_pct": 67.0
  }
}

This enables automated workflows to:

  • Auto-save session state at 70% usage
  • Trigger graceful compaction at 80% instead of waiting for forced compaction
  • Alert the user when approaching limits during AFK autonomous work
  • Log context efficiency metrics

Alternative Solutions

  • /compact only: Lossy, compresses everything, user has no control over what stays
  • /clear: Nuclear option, loses all context, requires full session reload
  • Parse transcript JSONL: Post-hoc analysis only, cannot affect live context
  • Status bar percentage: Display-only, not queryable by hooks or automation

Priority

High - Significant impact on productivity

Feature Category

CLI commands and flags

Use Case Example

  1. Running a multi-hour autonomous sprint with scheduled agents, deploy monitors, and 5 phases of code changes
  2. At phase 3, context is at 72%. A PostToolUse hook detects this and triggers /save automatically
  3. User runs /context to see what is consuming space -- discovers two large Explore agent results (18K tokens each) from phase 1 that are no longer needed
  4. User runs /context drop 4,7 to free 36K tokens
  5. Context drops to 54%, enough room to complete phases 4-5 without forced compaction
  6. Without this feature: compaction fires mid-phase-4, loses critical decisions from phase 3, user spends 30 minutes recovering context

Additional Context

  • The visual status bar already shows context percentage -- the data exists internally. This request is about making it queryable and actionable.
  • #11008 covers token/cost data in hooks (complementary, not duplicate). This covers context inspection + selective management.
  • #33088 covers PreCompact hooks (complementary). This gives users control BEFORE compaction becomes necessary.
  • #32946 covers rolling compaction (complementary). This gives users manual targeted control alongside any automatic compaction strategy.

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗