Feature request: Expose context usage to hooks for autonomous session handoff
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:
- User notices context filling up (or Claude proactively warns)
- User asks Claude to write a "continuation prompt" summarizing state
- User manually opens a new tab and pastes the prompt
- 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
contextobject toStop/SessionEnd/ newPreCompacthook input:
``json``
{
"context": {
"input_tokens": 145000,
"output_tokens": 8200,
"max_tokens": 200000,
"usage_percentage": 76.6
}
}
- Introduce
PreCompacthook event, fired when auto-compaction is about to run. - Optional
&autoSubmit=trueparameter onvscode://anthropic.claude-code/open, guarded by asettings.jsonopt-in like"allowAutoSubmitFromURI": true.
Why this matters
With these three pieces, users could write a single hook script that:
- Watches context usage on every
Stop - At a threshold, derives a continuation prompt from project memory/progress files
- Fires
start vscode://anthropic.claude-code/open?prompt=<...>&autoSubmit=true - 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.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗