claude-code - 💡(How to fix) Fix Feature request: Expose context usage to hooks for autonomous session handoff [1 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#54580Fetched 2026-04-30 06:41:45
View on GitHub
Comments
0
Participants
1
Timeline
3
Reactions
1
Author
Participants
Timeline (top)
labeled ×2subscribed ×1

Root Cause

With these three pieces, users could write a single hook script that:

  1. Watches context usage on every Stop
  2. At a threshold, derives a continuation prompt from project memory/progress files
  3. Fires start vscode://anthropic.claude-code/open?prompt=<...>&autoSubmit=true
  4. New tab opens and continues autonomously

This unlocks genuinely autonomous long-horizon work without losing the safety properties of fresh-session context.

Fix Action

Fix / Workaround

Current workaround: parse the session's .jsonl transcript file size as a proxy. This is brittle and inaccurate.

Workaround context

Code Example

{
    "context": {
      "input_tokens": 145000,
      "output_tokens": 8200,
      "max_tokens": 200000,
      "usage_percentage": 76.6
    }
  }
RAW_BUFFERClick to expand / collapse

Use case

When working on long-running, multi-phase projects (e.g. codebase migrations, multi-step feature implementations), a single Claude Code session's context eventually fills up. Today the workflow is:

  1. User notices context filling up (or Claude proactively warns)
  2. User asks Claude to write a "continuation prompt" summarizing state
  3. User manually opens a new tab and pastes the prompt
  4. New session reads MEMORY.md / PROGRESS.md and continues

This works but the handoff is manual. With a few small additions, the entire loop could be automated via hooks + the existing vscode:// URI handler.

What's missing

1. No context-usage metrics in hook inputs. The Stop and SessionEnd hook input JSON does not expose input_tokens, output_tokens, or a context_percentage field. This makes it impossible for a hook to decide "we're at 85%, time to hand off."

Current workaround: parse the session's .jsonl transcript file size as a proxy. This is brittle and inaccurate.

2. No official PreCompact hook. Hooks reference lists Stop, SessionEnd, UserPromptSubmit, SessionStart, etc., but no hook fires before automatic compaction kicks in. A PreCompact event would be a natural place to trigger handoff logic.

3. URI handler doesn't auto-submit. vscode://anthropic.claude-code/open?prompt=...&session=... pre-fills the prompt but the user must press Enter. Understandable for security, but an opt-in &autoSubmit=true (perhaps gated by a settings.json flag) would close the loop for trusted automations.

Proposed additions

  • Add context object to Stop / SessionEnd / new PreCompact hook input:
    {
      "context": {
        "input_tokens": 145000,
        "output_tokens": 8200,
        "max_tokens": 200000,
        "usage_percentage": 76.6
      }
    }
  • Introduce PreCompact hook event, fired when auto-compaction is about to run.
  • Optional &autoSubmit=true parameter on vscode://anthropic.claude-code/open, guarded by a settings.json opt-in like "allowAutoSubmitFromURI": true.

Why this matters

With these three pieces, users could write a single hook script that:

  1. Watches context usage on every Stop
  2. At a threshold, derives a continuation prompt from project memory/progress files
  3. Fires start vscode://anthropic.claude-code/open?prompt=<...>&autoSubmit=true
  4. New tab opens and continues autonomously

This unlocks genuinely autonomous long-horizon work without losing the safety properties of fresh-session context.

Workaround context

Today I'm building this with .jsonl size as a token proxy and manual Enter in the new tab. It works but the metric is approximate and the manual step breaks unattended runs.

extent analysis

TL;DR

To automate the handoff process for long-running Claude Code sessions, add a context object to hook inputs, introduce a PreCompact hook event, and allow optional auto-submit for the vscode://anthropic.claude-code/open URI handler.

Guidance

  • Add input_tokens, output_tokens, max_tokens, and usage_percentage fields to the Stop and SessionEnd hook input JSON to enable accurate context usage tracking.
  • Introduce a PreCompact hook event to trigger handoff logic before automatic compaction occurs.
  • Modify the vscode://anthropic.claude-code/open URI handler to accept an optional &autoSubmit=true parameter, gated by a settings.json flag, to enable automated submission.

Example

{
  "context": {
    "input_tokens": 145000,
    "output_tokens": 8200,
    "max_tokens": 200000,
    "usage_percentage": 76.6
  }
}

Notes

The proposed additions require updates to the existing hook system and URI handler. The current workaround using .jsonl file size as a proxy is brittle and inaccurate.

Recommendation

Apply the proposed additions to enable autonomous long-horizon work without losing the safety properties of fresh-session context, as they provide a more accurate and automated solution.

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 usage to hooks for autonomous session handoff [1 participants]