Hook subsystem: session metrics in payloads + warm-reset primitive
Feature request: turn count / token usage / latency in hook payloads + in-place session-reset primitive
Context
Building a SaaS multi-tenant agent platform on Claude Code. Owner-mode (single user, technical operator) handles long-conversation performance degradation by manually restarting the session when slowness becomes obvious. SaaS subscribers will not do this. Subscribers expect always-on behaviour. "Claude is slow today" reads as "the product is broken" — direct churn signal.
What we tried
Wanted to build a hook that:
- Detects long-conversation symptoms (turn count + cumulative tokens + per-turn latency).
- At threshold, fires a save-state protocol (flush memory / close handoffs / write diary line).
- Triggers an in-place session reset that preserves cwd + state pointers for the new session to read.
- Surfaces to subscriber as "Refreshing your assistant — new session ready in 3 seconds."
What's blocked
Per Claude Code hooks reference (https://code.claude.com/docs/en/hooks.md):
- No turn count exposed to hooks. Stop / SessionStart / PreToolUse payloads don't include cumulative turn metrics.
- No token usage exposed. Same.
- No per-turn latency exposed. Same.
- No in-place session-reset primitive. Stop hooks can
claude logout(kill the process) and let the user re-launch — but this loses the working directory, breaks the subscriber UX, and risks data loss if save-state hasn't run yet.
Workaround we shipped (acknowledged as fragile)
A Stop hook (monitor-length.ps1) that uses transcript-size-on-disk as a proxy for token count + line count as a proxy for turn count. Suggests a restart when thresholds cross. NEVER auto-restarts. NEVER kills the session. Surfaces via additionalContext from a UserPromptSubmit hook.
This works for owner mode (operator sees the suggestion, decides whether to restart). It does NOT work for SaaS subscribers — they'd just see "system suggests restart" and either ignore it (slowness continues) or click "restart" and lose context.
What we need
Three additions to Claude Code's hook subsystem:
1. Conversation metrics in hook payloads
Add to Stop / SessionStart / PreToolUse / UserPromptSubmit payloads:
{
"session_metrics": {
"turn_count": 47,
"cumulative_input_tokens": 142000,
"cumulative_output_tokens": 28000,
"cumulative_total_tokens": 170000,
"last_turn_latency_ms": 8420,
"session_start_iso": "2026-05-07T16:42:00+08:00",
"context_window_size": 200000,
"context_utilisation_pct": 85
}
}
This unblocks accurate threshold detection. The context_utilisation_pct is the killer metric for SaaS — at 85%, the next compaction is imminent and likely expensive.
2. In-place session-reset primitive
A hook output schema that lets a Stop hook trigger:
{
"hookSpecificOutput": {
"hookEventName": "Stop",
"sessionAction": "warm_reset",
"warmResetCarryover": {
"preserve_cwd": true,
"carry_state_files": ["MEMORY.md", "session-diary.md"],
"user_facing_message": "Refreshing your assistant — new session ready in 3 seconds."
}
}
}
Semantics: same Claude Code process, same cwd, but conversation context is reset to empty + state files pointed to in carry_state_files are auto-read at the new SessionStart.
3. Optional: opinionated long-conversation-mitigation as platform feature
Even with the above two primitives, every SaaS-grade Claude Code deployment will need to build the same thing. Worth Anthropic shipping a platform-level "auto-mitigate long-conversation degradation" toggle that uses #1 + #2 internally and lets the operator just say "yes, autopilot." Reduces the build surface from N teams each implementing it to one.
Priority justification
- For a single-tenant deployment: medium. The fragile workaround is acceptable while owner-mode dominates.
- For a SaaS roadmap: hard blocker. Cannot take a paying subscriber across a degradation event without #1 and #2 in place.
- For Anthropic: any team building agent platforms on Claude Code hits this exact wall the moment they try to support always-on subscribers. Shipping Managed Agents at $0.08/session-hour with per-subscriber-per-session-hour billing makes long-conversation handling an Anthropic-side concern, not just a customer-side one.
How we'd help
Happy to share:
- Empirical metrics from owner-mode sessions — when the perceived-slow threshold actually lands (turn count, transcript size, etc.).
- Design sketches for the warm-reset semantics (carry_state_files, cwd preservation, sub-second user-facing message).
- A sample SaaS subscriber UX flow showing where the platform boundary should land.
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗