How to stop your Mac sleeping during a long Claude Code run
A long agent run stops when the computer sleeps. Why it happens, how to see what keeps a Mac or PC awake, the built-in fixes, and what closing the lid does.
In short
- Computers sleep after a set time without keyboard or mouse input, and disk or CPU activity does not reset that timer, so a long agent run can be put to sleep mid-task.
- Anthropic says Claude Code retries a request that sleep interrupted, and replying continue picks up a reply that sleep cut off.
- On a Mac, caffeinate -i holds off idle sleep and pmset -g assertions shows what is holding it; on Windows, PowerToys Awake and powercfg /requests do those jobs.
- Closing the lid is a separate rule that caffeinate does not change, and a Windows laptop follows its own lid setting.
- Dictate Safe, a dictation app, has a Keep Awake button with a five-minute warning, a release at 20% battery and a countdown, and it leaves the lid alone.
Why does a computer sleep while a program is still running?
Because the sleep timer counts the time since you last used the keyboard or mouse, and a busy program does not reset it. Microsoft’s SetThreadExecutionState page says “activities that are not automatically detected include disk or CPU activity,” and an Apple developer note describes idle sleep as what happens “when the machine is unused for a specific period of time.” An agent that spends an hour editing files and running tests looks unused.
| What happens | What you see | Does the task keep running? |
|---|---|---|
| Display sleep | The screen goes dark | Yes |
| Screen lock | A password prompt | Yes, while the computer stays awake |
| System sleep | Nothing responds until you wake it | No, programs stop until it wakes |
Microsoft’s page on system power states confirms the first row: “Whether the screen is on or off, the device is in a full running state.”
What happens to a Claude Code run when the computer sleeps?
It pauses, since programs stop while a computer sleeps. Anthropic’s error reference says Claude Code retries a request that sleep broke, as it does any dropped connection. If sleep cut off a reply, it shows “Your computer went to sleep mid-response. The response above may be incomplete.” It keeps every block Claude finished, and replying continue picks up from the last one.
Windows laptops with Modern Standby can stay online while asleep, yet Microsoft says “Desktop apps are stopped by the Desktop Activity Moderator (DAM)”, and a terminal is a desktop app.
The Claude Desktop app’s Keep computer awake setting (Settings, Desktop app, General) prevents idle sleep, says its scheduled tasks page, though “closing the laptop lid still puts it to sleep.” A Remote Control session you follow from your phone reconnects on its own once the computer is back online.
How do you see what is keeping a computer awake?
On a Mac, open Terminal (press Command and Space, type Terminal, and press Return), type pmset -g assertions and press Return. It lists each program holding the Mac awake with its kind of hold: PreventUserIdleSystemSleep stops idle sleep, and PreventUserIdleDisplaySleep keeps the screen on. Without Terminal, Activity Monitor’s Energy tab has a Preventing Sleep column that answers the same question app by app.
On Windows, open Command Prompt as administrator and run powercfg /requests. The powercfg reference says it lists the power requests apps and drivers have made, which “prevent the computer from automatically powering off the display or entering a low-power sleep mode.”
A one-minute test with caffeinate
caffeinate changes no setting and lets go when it ends, so it is safe to try.
- In Terminal, type
caffeinate -i -t 120and press Return. The cursor waits on the next line while the Mac stays out of idle sleep for 120 seconds. Control and C ends it sooner. - Open a second Terminal window with Command and N and run
pmset -g assertions.
Trimmed output from macOS 15.7 on 3 October 2026:
PreventUserIdleSystemSleep 1
pid 49128(caffeinate): [0x000e0e8100019deb] 00:00:05 PreventUserIdleSystemSleep named: "caffeinate command-line tool"
Details: caffeinate asserting for 120 secs
Timeout will fire in 115 secs Action=TimeoutActionReleaseIn the first line, 1 means something is holding off idle sleep. The caffeinate entry counts down its 120 seconds, and two minutes later it is gone from the list.
How do you stop a Mac sleeping with built-in tools?
Put caffeinate in front of the command. It comes with macOS and needs no administrator password, and its manual page (man caffeinate, also on ss64) covers these forms:
caffeinate -i claudestarts Claude Code and holds off idle sleep until it exits;caffeinate -i npm testdoes the same for a test run.caffeinate -i -t 14400holds for 14,400 seconds, which is four hours.caffeinate -i -w 4242holds until process 4242 exits, for a job already running (Activity Monitor shows the number in its PID column). Add-dto keep the display on too.
For a switch instead, use the sleep settings: on a laptop, Prevent automatic sleeping on power adapter when the display is off, under System Settings, Battery, Options; on a desktop Mac, the matching switch under Energy.
How do you stop a Windows PC sleeping with Microsoft's tools?
We could not test these on Windows for this post, so try them on your PC first.
- In Windows 11, open Settings, System, Power & battery, Screen, sleep, & hibernate timeouts, and lengthen Make my device sleep after for When plugged in. Set it back afterwards.
- In Command Prompt,
powercfg /change standby-timeout-ac 240sets the plugged-in sleep timer to 240 minutes. - PowerToys Awake, part of Microsoft PowerToys, keeps the PC awake without changing your power plan, indefinitely, for an interval or until a set time. Displays still turn off unless you switch on Keep screen on.
Microsoft says PowerToys Awake “doesn’t work when the lock screen is displayed,” because user-mode apps cannot keep their power requests there, and suggests changing the power plan for a PC that must stay awake while locked.
What does closing the lid do?
On a Mac laptop, closing the lid causes what Apple’s developer note (updated in 2014) calls forced sleep, which caffeinate -i does not hold off. With the lid shut and an external display, Apple says the Mac should be “connected to power and using an external keyboard and mouse.” Amphetamine, a menu bar app we have not tested, lists a lid-closed option that needs a separate download on Apple silicon.
On Windows 11, the lid follows a setting: open Control Panel, then System and Security, and under Power Options select Choose what the power buttons do; set When I close the lid for On battery and Plugged in, then Save changes. The choices are Do Nothing, Sleep, Hibernate and Shut Down, and a program holding the PC awake does not override them: SetThreadExecutionState “cannot be used to prevent the user from putting the computer to sleep.”
What does Keep Awake add, and when is caffeinate enough?
If you start long jobs from a terminal, caffeinate -i in front of the command is a better habit than any timer, ours included, because the hold ends the moment the job does.
Keep Awake is a button in Dictate Safe, a dictation app for macOS and Windows. On a Mac, pmset -g assertions lists its hold as “Dictate Safe is keeping this Mac awake”. It adds:
- Lengths from 30 minutes to 24 hours, or until you turn it off, with two hours as the default; one click on the floating icon’s cup button reuses your last choice.
- A notification five minutes before the end, Add another hour in the menu bar menu (the notification area on Windows), and a countdown there and beside the floating icon.
- On battery, a warning at 25% and a release at 20%.
- The screen kept on by default, and on a Mac the screen saver and automatic lock held off. On Windows, Microsoft says the call Keep Awake uses does not stop the screen saver.
What should you check before you walk away?
- Plug the laptop in.
- Leave the lid open, unless the Mac has an external display, keyboard and mouse connected or the Windows lid setting for Plugged in is Do Nothing.
- Start a hold that outlasts the job, and confirm it with
pmset -g assertionsorpowercfg /requests. - Give the agent the permissions it needs, since a run that stops to ask waits for you.
- On Windows, if the PC will lock while you are away, lengthen the power plan’s sleep timer, as Microsoft suggests for a locked PC.
Questions people ask
Does locking the screen stop a long agent run?
Not while the computer stays awake: a locked or dark screen leaves programs running, and system sleep is what stops them. On Windows, Microsoft says PowerToys Awake does not work at the lock screen.
What does Claude Code do if the computer sleeps mid-task?
Anthropic's error reference says Claude Code retries a request that sleep interrupted, as it does any dropped connection. If sleep cut off a reply, it shows 'Your computer went to sleep mid-response', and replying continue picks up from the last block Claude finished.
Can a MacBook stay awake with the lid closed?
Yes, with an external display. Apple says a Mac laptop used with its lid closed should be connected to power and using an external keyboard and mouse. Otherwise closing the lid causes forced sleep, which caffeinate -i does not prevent.
Does Dictate Safe's Keep Awake work with the lid closed?
No. Keep Awake in Dictate Safe, a dictation app, stops the computer falling asleep on its own and leaves the lid alone, so closing it does what it would without the app: forced sleep on a Mac laptop with no external display, and whatever When I close the lid says on Windows.
Keep reading
- Industry
Robots don't come with keyboards: how a robot hears you
Robot brains read text, so a speech recogniser decides what they hear. Why robots hear worse than phones, and what a misheard command can cost near people.
- Guides
How to dictate prompts to Claude, ChatGPT and Cursor
A practical guide to dictating long prompts to AI assistants: what to say, how to get names and code right, and how to keep your voice on your own computer.