Why your Mac falls asleep in the middle of an AI agent run
Updated 14 November 2026 · Who measures this.
You start a long task in Claude Code, step away, and come back to find the agent stopped halfway because the Mac went to sleep. We measured why in our lab, on 2 Macs.
Short answer. While it works, Claude Code keeps the Mac awake with caffeinate -i -t 300, and according to its code it replaces that command every 240 seconds. In our tests the Mac fell asleep about 245 seconds after Claude Code started, in all 8 runs: 5 on battery and 3 on a power adapter. In print mode (claude -p) Claude Code did not keep the Mac awake at all. With the display off and nothing running, staying awake cost 0.73 W on the MacBook Pro M1 Pro, about 1.4% of its battery an hour, and no more than 0.18 W, 0.3% an hour, on the MacBook Air M5.
How we tested
Each run gave Claude Code (version 2.1.322, Haiku 5.5, low effort) 1 task: start a small script 4 times and wait for each to finish, 9 minutes each. The script did no work. It wrote a timestamp every 30 seconds, so a gap in its log is a gap in the Mac's day.
For the test we set the Mac to sleep after 3 minutes of inactivity, with the display turning off sooner. Nobody touched the Macs during the runs, except to close and open the lid where a run called for it. 3 separate records told us whether a Mac slept: the macOS power log, the difference between wall-clock time and the time the Mac was actually running, and the script's own timestamps. Every 30 seconds we also saved the list of processes holding the Mac awake, with the process that started each one.
Twice on each Mac, on battery, we let it sit with nothing running. It slept after about 3.3 minutes every time, so nothing else was keeping it awake.
What Claude Code does
In interactive mode, as soon as a task starts, a caffeinate -i -t 300 appears whose parent is claude, a command that asks macOS not to sleep from inactivity for the next 300 seconds and that Claude Code is meant to renew well before those 300 seconds run out.
| Mac | Power | Runs | Fell asleep, seconds after Claude Code started |
|---|---|---|---|
| MacBook Pro M1 Pro | battery | 2 | 246, 245 |
| MacBook Air M5 | battery | 3 | 246, 245, 245 |
| MacBook Air M5 | power adapter | 3 | 245, 245, 245 |
The display was already off each time. None of the tasks finished in its time: the script stopped while the Mac slept and carried on after it woke.
Here is 1 run up close, on the MacBook Pro. The first caffeinate was still there at our last check before the sleep. The power log then says the Mac entered sleep "due to Idle Sleep". When the Mac woke 41 minutes later, a different caffeinate, with a new process number and the same claude parent, was holding it. We did not record the replacement itself, since we checked every 30 seconds. The timing matches a report in the Claude Code repository (#81832): every 240 seconds Claude Code stops the old caffeinate before it starts the new one, and in that report the Mac went to sleep 5 seconds after the old one stopped.
That is the whole bug. Our sleep timer was 3 minutes, so the Mac was ready to sleep as soon as nothing held it. On your Mac, if you have been away for longer than your sleep setting and the display is off, the next replacement can put it to sleep.
Print mode holds nothing
claude -p runs a task and prints the answer, which is how scripts and automations use Claude Code. In 4 runs on both Macs, no caffeinate came from Claude Code, and the Mac fell asleep 193 to 207 seconds after Claude Code started, in the middle of the first 9 minute job. Nothing held it.
What keeps the Mac awake
| What you run | Result | Runs |
|---|---|---|
| Nothing (control, battery) | sleeps after about 3.3 min | 4 of 4 slept |
| Claude Code, interactive | kept awake until about 245 s | 8 of 8 slept (5 on battery, 3 on power) |
claude -p (battery) | not kept awake | 4 of 4 slept |
caffeinate -i -w <pid> on a plain script (battery) | kept awake for all 30 min | 0 of 4 slept |
caffeinate -s (battery) | not kept awake | 4 of 4 slept |
sudo pmset disablesleep 1 and caffeinate -i, lid closed (battery) | kept awake for about 20 min | 0 of 1 slept, MacBook Air only |
caffeinate -s is meant for a Mac on a power adapter. On battery the Mac still listed it as holding the system awake, and slept anyway.
What staying awake costs
With the display off and nothing running, measured for 27 minutes after the first 3, 3 times on each Mac:
| Mac | Awake, display off | Asleep | Battery |
|---|---|---|---|
| MacBook Pro M1 Pro | 0.728 W, 1.4% an hour | about 0.2 W in 2 of 3 runs | 52.2 Wh |
| MacBook Air M5 | 0.178 W, 0.3% an hour | not reported | 58.0 Wh |
Sleep was cheaper, as it should be. On the MacBook Pro, 2 of the 3 sleep readings came to about 0.2 W. The third, 1.15 W, included a full wake in the middle of the sleep. So staying awake cost about 0.5 W more than sleeping, about 1% of the battery an hour. On the MacBook Air our 3 sleep readings disagreed with each other, so we do not report one; staying awake cost no more than 0.18 W.
The lid
With sleep disabled (sudo pmset disablesleep 1) and the lid closed for about 20 minutes, the MacBook Air stayed awake and its battery went from 20.5 °C to 20.7 °C. That was with nothing running; a heavy job in a closed bag is a different case, and we did not test it. disablesleep is not in the pmset manual, so treat it as unsupported; on our Mac it did what it says. Set it back with sudo pmset disablesleep 0 when you are done.
We also wanted to know whether closing the lid puts the Mac to sleep while Claude Code is holding it. In every one of those runs the Mac had already fallen asleep, before we closed the lid, so we have no answer to that yet.
What to do
- For a long run, hold 1 assertion for the whole task:
caffeinate -i -w <pid>, where<pid>is the agent's process number. The report in #81832 suggests the same. We tested this form on a plain script, where it held in all 4 runs, not next to a running agent. - Do not rely on
caffeinate -son battery. - Expect
claude -pin scripts and automations to let the Mac sleep unless something else holds it.
The Macs
| Mac | Chip | Memory | macOS | Battery | What ran on it |
|---|---|---|---|---|---|
| MacBook Pro 14″ | M1 Pro | 16 GB | 27.0.1 | 72% of design, over 1,700 cycles | 13 runs on battery |
| MacBook Air 13″ | M5 | 16 GB | 27.0.1 | 13 cycles | 13 runs on battery, 4 with the agent on power or with the lid, 1 with the lid closed |
Claude Code 2.1.322 on Haiku 5.5 at low effort. The rules for reading the results were written before the runs.
Related: why short messages use so much of an agent's limit · battery drain in sleep · what Claude costs
WattMate shows what your agents cost in watts
It does not keep your Mac awake.
10 days free, no card · Requires macOS 14 or later · Apple silicon