HTTP hook intermittently fails with ECONNREFUSED despite healthy server

Resolved 💬 5 comments Opened Mar 2, 2026 by shenyunhuan Closed Apr 7, 2026

Bug Description

UserPromptSubmit HTTP hook intermittently reports UserPromptSubmit hook error even though the hook server is confirmed healthy on the same port. The HTTP request never reaches the server.

Environment

  • Claude Code version: 2.1.63
  • OS: Windows 11 Pro (10.0.26220)
  • Shell: Git Bash
  • Hook server: Bun 1.3.10 (Bun.serve() on 127.0.0.1:62741)

Hook Configuration

"UserPromptSubmit": [{
  "hooks": [{
    "type": "http",
    "url": "http://127.0.0.1:62741/hooks/prompt-processor",
    "timeout": 30
  }]
}]

Reproduction

  1. Configure an HTTP hook for UserPromptSubmit pointing to a local Bun server
  2. Use the session normally — most prompts succeed
  3. Intermittently (roughly 1 in 10-20 prompts), the hook fails with UserPromptSubmit hook error
  4. The error occurs more frequently after /model switches, but also on regular prompts

Evidence That Server Is Not the Problem

When the error occurs:

  • curl http://127.0.0.1:62741/health returns {"status":"ok"} immediately
  • netstat confirms the server is LISTENING on the port with the expected PID
  • Server request log shows no incoming request for the failed hook call — the HTTP request never reaches the server
  • 10 concurrent curl requests all return 200 in ~2ms
  • Server PID file matches the listening process (no stale/zombie issues)

Impact: Cache Invalidation ($$$)

This is extremely expensive with large context models (1M tokens). When the hook fails:

  • Claude Code injects UserPromptSubmit hook error into the context
  • When the hook succeeds, it injects UserPromptSubmit hook success: Success
  • These are different strings, changing the prompt prefix → cache key changes → all cached tokens from that turn onward are invalidated
  • On a 1M context conversation, one hook failure caused ~$45 in recomputation costs

Expected Behavior

HTTP hooks to 127.0.0.1 should never fail with ECONNREFUSED when the server is confirmed listening and healthy on the same port. If a transient connection failure occurs, the hook client should retry at least once before reporting an error.

Suggested Improvements

  1. Retry on ECONNREFUSED: Add 1-2 automatic retries with short delays for HTTP hooks targeting localhost
  2. Silent no-op on failure: For UserPromptSubmit hooks, consider not injecting different error text into the prompt to avoid cache invalidation
  3. Consistent cache key: Use the same prefix text regardless of hook success/failure to preserve API cache

View original on GitHub ↗

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