Skip to content

[Feature Request] health-monitor: channel-level reconnect should preserve Anthropic prompt cache #57569

Description

@serhiizghama

Problem

When a channel (Discord, Telegram, etc.) goes stale, the OpenClaw health-monitor correctly restarts only the channel connector (via stopChannel + startChannel). However, this still invalidates the Anthropic prompt cache, which is process-level.

Real-world impact

  • Normal operation: ~500 new tokens per turn (~99% cache hit rate)
  • After any channel restart: ~120,000 tokens sent in full (0% cache hit for 2-3 turns)
  • Cost difference per restart: ~240x token usage spike

Incident log (2026-03-30)

12:35:45 — health-monitor: restarting (reason: stale-socket)  [discord:default]
12:35:46 — discord gateway: WebSocket closed with code 1005
13:22:45 — discord gateway: Attempting resume with backoff 1000ms

The channel reconnect itself is fine — Discord handles it gracefully. The problem is that the cache warm-up after reconnect costs a full context re-send.

Root Cause

Anthropic's prompt cache is tied to the model inference session context on the Anthropic API side. Any interruption in the streaming connection — including a channel-level reconnect — forces a cold cache start on the next message. The agent's system prompt (~120k tokens) has to be re-sent in full.

Proposed Solution

Option A — Reconnect without stopping (preferred)

Instead of stopChannel → startChannel, try a lighter reconnect() path when the reason is stale-socket. Discord's own library already supports client.ws.reconnect() — this resumes the WebSocket session without destroying the client, which means the next Anthropic call can potentially reuse the cache warmup from before the disconnect.

Option B — Cache pre-warming after restart

After a channel restart, immediately send a lightweight keep-alive message to the active agent sessions to re-warm the Anthropic cache before the user sends their next message.

Option C — Config flag

Add gateway.channelStaleRestartMode:

{
  "gateway": {
    "channelStaleRestartMode": "reconnect" // "reconnect" | "stop-start" (default: "stop-start" for compat)
  }
}

Code Location

The relevant logic is in:

  • src/gateway/channel-health-monitor.tsrunCheck() calls stopChannel + startChannel
  • src/gateway/server-channels.tsChannelManager interface

Current Workaround

Disabling the health-monitor per-channel avoids the cache bust, since channels reconnect themselves:

{
  "channels": {
    "discord": { "healthMonitor": { "enabled": false } }
  }
}

This works because Discord (and most providers) implement their own reconnect logic. The health-monitor restart is only needed when a channel is truly dead (not just momentarily disconnected).

Why This Matters

With 25k+ OpenClaw users running Anthropic models with long system prompts, every stale-socket event silently multiplies token costs by ~240x for 2-3 turns. This is invisible to users and looks like random cost spikes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions