[Bug] macOS: background agents re-send full context on every DarkWake after lid close (81 requests sent, 29 completed overnight; usage consumed while asleep)
What's wrong?
Background subagents were still running when I closed my MacBook lid on battery. For the next 6.5 hours, Claude Code re-sent each agent's full conversation (400–580K tokens) on almost every macOS DarkWake / Sleep Service wake. Those wakes last only a few seconds, so each new request was frozen again almost immediately.
Overnight result, from the extension's --debug log and the session transcripts (23:30–06:00 UTC+7, 2026-10-14/15):
/v1/messages requests sent |
81 |
| Requests that completed (in transcripts) | 29 |
| macOS sleep/wake cycles | 26 (about every 15 min, dasd Sleep Service) |
StreamSuspended retries |
19 |
404 No thread state was found → [tether] forcing create for retry: replay_not_found |
11 |
Completed calls that rewrote the whole prompt cache (cache_read=0, cache_creation 400–580K) |
15, about 7.1M tokens |
| Output tokens produced all night | about 7K |
There was no human input in that window. My subscription's weekly usage went from 20% at 23:30 to 56% at 06:00. I can't tell locally how much of that came from requests that never completed, so I'm asking about billing below.
Sequence (repeats on every wake)
- 23:24 local: lid closed on battery →
Entering Sleep state due to 'Clamshell Sleep'. Two background subagents (requestShape: background) are mid-stream. - About 15 min later, macOS does a brief DarkWake for Sleep Service. Claude Code wakes up, sees the frozen stream and aborts it:
[byte-watchdog] aborting: late=-179678ms slept=922031ms (sleep/suspend) Stream connection closed (StreamSuspended) after 1 block(s) yielded — finalizing partial response [event-loop-stall] ... clock jump 922031ms ... [likely sleep] - It immediately re-issues requests (several per agent):
[API REQUEST] /v1/messages x-client-request-id=fd29ae39-cb5b-4541-ace1-67c648c821d9 source=agent:builtin:general-purpose [API REQUEST] /v1/messages x-client-request-id=1b948740-3483-4957-b053-dbe5544dd9bd source=agent:builtin:general-purpose API error (attempt 1/11): undefined Connection error. [API REQUEST] /v1/messages x-client-request-id=7738e348-3689-44ee-8b58-ea5fafd872fb source=agent:builtin:general-purpose API error (attempt 1/11): 404 {"type":"not_found_error","message":"No thread state was found for the requested `previous_message_id`. Replay the full ..."} [tether] forcing create for retry: replay_not_found [API REQUEST] /v1/messages x-client-request-id=f818ab29-8358-4e2a-967f-8625662b0352 source=agent:builtin:general-purpose Stream started - received first chunk - The Mac goes back to sleep within seconds (
Entering Sleep state due to 'Sleep Service Back to Sleep'), freezing the new stream. Go to step 2.
Because each gap (about 15 min) is longer than the subagents' prompt-cache lifetime, every request that does complete pays a full cache write of the whole conversation. When a stream was cut after partial output, the agent was then told "Your response above was cut off mid-stream … Write the complete response again", so that output was paid for twice.
(An MCP connector was also stalling: no response or progress for 724–1091s (idle timeout 300s), which made the gaps longer.)
Expected behavior
While the machine is asleep or only in a DarkWake, Claude Code should not repeatedly re-send full-context requests. Suggestions:
- When the byte-watchdog detects
sleep/suspend, defer the retry until a full user wake, or until the network has stayed up for N seconds. - If consecutive retries are themselves interrupted by sleep, back off exponentially (or pause the background task) instead of retrying at every wake.
- Optionally pause background agents on clamshell sleep and resume on wake.
Related
- #100353: once a stream error occurs, keep-alive stays disabled and later stalls turn into full-conversation re-sends (
replay_not_found/ "already continued"). My log also showsStale connection — disabling keep-alive for retry. This sleep loop is a reliable trigger for that path. - #85575 (closed/locked): sleep-inhibitor gap. This report is different: the Mac slept because the lid was closed, which no inhibitor can prevent. The issue is what Claude Code does on each wake afterwards.
Question for the team
Were the requests that were aborted or never completed billed against my plan usage? If so, could someone look at this case for a usage adjustment? The request IDs above are from one wake cycle (00:17:39 UTC+7). I can provide more from my debug log.
Environment
- Claude Code 2.1.296 (native binary, darwin-arm64), via the VS Code extension in a VS Code-based IDE
- macOS 26 (Darwin 25.6.0), Apple Silicon MacBook, on battery (56%), lid closed
- Model: Opus 5.5 with 1M context; permission mode
auto - Two long-running sessions, each with one background
general-purposesubagent active overnight pmset:powernap 1,tcpkeepalive 1,standby 1
The billing question at the end is the right place to start, because the token spend is the smaller consequence. The larger one is that a wake is being treated as a new turn.
A DarkWake for Sleep Service is not a user event. It lasts seconds, it runs on a timer, and nothing about it changes what the session was doing. When the client answers it by re-sending a full 400 to 580k token prompt, three things compound:
-
The re-send rewrites the prompt cache. 15 of the completed calls show
cache_read=0, which means those requests paid the full context price again instead of reading a cached prefix. That is the mechanism behind 20% to 56% of a weekly budget with no human input in the window. -
The request cannot finish in the window it is given. A wake lasting a few seconds cannot complete a large streaming request, so the same turn starts again on the next wake. 81 requests against 29 completions is that ratio.
-
The retry path is forcing a fresh thread.
replay_not_foundleading to[tether] forcing create for retrymeans the retry does not resume the interrupted request, it begins a new one with the same content. Without that, a second attempt would be a continuation rather than a second full send.
The behavior I would argue for:
- A wake should first ask whether the in-flight request can be resumed within the remaining wake budget, and defer if it cannot. Starting a 400k token request with a few seconds of run time is a request that is known to fail before it is sent.
- Cap wake-initiated retries per sleep cycle. 26 wakes producing 81 requests is about three attempts per wake, which is a retry policy sized for a momentary blip applied to a device that goes back to sleep in seconds.
- Persist the request identity so a retry reuses the server-side thread. The forcing-create path is what turns a transient interruption into a duplicate full-context send.
- Give sleeping its own session state so background subagents can be paused rather than held mid-stream across a window where no progress is possible.
On billing, the fact that 52 of 81 requests produced nothing and the client cannot say locally whether they were charged is the part worth raising, because the user currently has no way to tell a client retry loop apart from legitimate work.
For anyone hitting this before there's a fix: the expensive part is the mixed state — asleep, but woken for a few seconds by DarkWake, long enough to re-send but not to finish. Making the Mac either fully awake or fully asleep avoids it:
- Keep it awake while agents run: on battery with the lid closed, assertions (
caffeinate) are ignored, so the only switch issudo pmset -a disablesleep 1— set it back to0when the agents are done, or it'll drain the battery in your bag. - Or let it sleep cleanly: stop/pause background agents before closing the lid. Turning off Power Nap on battery (
sudo pmset -b powernap 0) may reduce how often DarkWake happens, but it doesn't eliminate maintenance wakes, so I wouldn't rely on it.
Disclosure: I build a menu bar app, Lidnight, that automates the first option — it keeps a lid-closed MacBook awake only while Claude Code's hooks show a turn running (including background sessions) and lets it sleep once they're all quiet: https://lidnight.app/en/
One free mitigation not mentioned yet: the rewrites here came from background subagents, and Anthropic's prompt caching docs give subagents a five-minute cache even on a subscription. Your wakes were about 15 minutes apart, so every one missed the cache. Adding "subagentPromptCacheTtl": "1h" to ~/.claude/settings.json (Claude Code 2.1.242+, or CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL=1h) should turn most of those 15 full rewrites into cache reads. It does not stop the re-send loop, and 1h writes cost 2x base input.
For context: 2.1.290 fixed "Agent stalled" and Workflow subagents restarting from their prompt on wake, but I don't see a changelog entry through 2.1.306 that adds a backoff for repeated sleep cut-offs, so "fully awake or fully stopped" is still the reliable fix. /usage (2.1.251+) shows the main-conversation cache hit ratio if you want to watch it.
I make Clamshell, a paid app that keeps a MacBook awake with the lid closed. Notes on the cache and sleep interaction: https://clamshell.dev/guides/claude-code-usage-limit-mac-sleep
The 81 vs 29 gap is the hard part to see: the requests that never finished only showed up in the debug log, not the transcripts.
If you want that count without --debug, a local proxy in front of the API sees every request on the wire. I build one, Torana (open source): each request shows up in its live feed with input, cache read, cache write and output tokens, and a stream that dies mid-response is still logged with an error code. On a night like this you'd see each wake's full-context cache write as it happens.
I've only tested it with the CLI, not the VS Code extension. https://github.com/torana-edge/torana-edge