claude-code - 💡(How to fix) Fix Hook to post-process assistant output before TUI render [1 participants]

Official PRs (…)
ON THIS PAGE

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#56428Fetched 2026-05-06 06:28:18
View on GitHub
Comments
0
Participants
1
Timeline
3
Reactions
0
Participants
Timeline (top)
labeled ×3

Code Example

{
  "hook_event_name": "AssistantMessage",
  "message": "...assistant text...",
  "transcript_path": "/path/to/transcript.jsonl",
  "session_id": "..."
}

---

{"replacement": "...new text..."}
RAW_BUFFERClick to expand / collapse

Request

New hook event (e.g. AssistantMessage or PreRender) that receives the assistant's final message text and can return a transformed string before it renders to the user.

Use case

Deterministic enforcement of output-style rules without burning tokens on Stop-hook retry loops. Examples:

  • Hard-wrap prose at N chars (terminal width preference)
  • Strip filler phrases ("Sure!", "I'll", "Let me")
  • Redact patterns (secrets, internal hostnames)
  • Inject footers / signatures
  • Convert markdown flavors

Why existing hooks don't cover it

  • Stop hook fires after the stream is rendered; it can only block + force a retry (costly, non-deterministic, adds latency, burns tokens).
  • UserPromptSubmit runs pre-model; cannot see output.
  • No PostToolUse equivalent exists for the assistant message itself.

Shape suggestion

Input JSON to hook stdin:

{
  "hook_event_name": "AssistantMessage",
  "message": "...assistant text...",
  "transcript_path": "/path/to/transcript.jsonl",
  "session_id": "..."
}

Hook returns JSON on stdout:

{"replacement": "...new text..."}

Or exits 0 with empty stdout to pass through unchanged.

Precedent

Stop hook already gates session end; this would be the symmetric mutation point one layer earlier (between model completion and TUI render).

Motivation

Personal CLAUDE.md / output-style instructions drift over long conversations regardless of how forcefully phrased. Instruction-only enforcement is unreliable. A Stop-hook retry loop works but costs tokens and adds latency on every turn. A pure-text post-processor is cheap, deterministic, and side-effect-free — ideal for formatting rules that don't require model judgment (line wrapping, regex strip, redaction).

extent analysis

TL;DR

Implement a new hook event, such as AssistantMessage, to receive and transform the assistant's final message text before rendering to the user.

Guidance

  • Define the new hook event to accept the assistant's message text and other relevant information, such as transcript path and session ID, as input JSON.
  • Implement the hook to return a transformed string as JSON output, or exit with empty stdout to pass through the original message unchanged.
  • Consider using existing hook events, such as Stop, as a reference for implementing the new hook.
  • Evaluate the feasibility of integrating the new hook into the existing system, ensuring compatibility with other components and hooks.

Example

// Input JSON to hook stdin
{
  "hook_event_name": "AssistantMessage",
  "message": "This is the assistant's message text.",
  "transcript_path": "/path/to/transcript.jsonl",
  "session_id": "example_session_id"
}

// Hook returns JSON on stdout
{
  "replacement": "Transformed message text."
}

Notes

The implementation of the new hook event requires careful consideration of the system's architecture and existing components. The proposed shape suggestion provides a starting point, but further evaluation and testing are necessary to ensure seamless integration.

Recommendation

Apply a workaround by utilizing the proposed AssistantMessage hook event, as it addresses the need for deterministic enforcement of output-style rules without incurring the costs associated with the Stop hook retry loop.

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 Hook to post-process assistant output before TUI render [1 participants]