Keep your Mac or PC awake while a coding agent runs
Stop a Mac or Windows PC from sleeping while a coding agent runs a long task, using caffeinate or the sleep settings, then put your settings back afterwards.
To keep a Mac awake while a coding agent runs, open Terminal, run caffeinate -i and leave the window open until the task ends. On Windows 11, set the plugged-in sleep time in Settings to Never, then set it back afterwards. Keep a laptop on its power adapter with the lid open.
This guide covers macOS and Windows 11, but not running a laptop with the lid closed. Mono by Reunified can keep a Mac awake while an agent works, but these steps work with any agent.
1. Know what sleep does to an agent session
Sleep pauses every program, the agent included, and drops its network connections. The session may not recover cleanly.
- Display sleep only turns the screen off. Programs keep running.
- Idle sleep stops the computer after a while without input. A busy agent doesn’t count as input.
- Closing a laptop’s lid normally puts it to sleep, whatever the idle settings say.
- Shutting down or restarting ends the session.
Don’t count on your phone to wake a computer that’s asleep or off.
Made-up example: before lunch, you leave an agent upgrading dependencies and running tests in your “tidy-recipes” project. It’s about 40 minutes of work on a MacBook Air connected to power.
2. Keep a Mac awake for one task with caffeinate
caffeinate is built into macOS. With -i, it prevents idle sleep while it runs. When it stops, your usual settings apply again.
| Command | Effect |
|---|---|
caffeinate -i |
Awake until you stop it with Control-C |
caffeinate -i <command> |
Runs the command, awake until it exits |
caffeinate -i -t 3600 |
Awake for 3,600 seconds (an hour) |
caffeinate -i -w <pid> |
Awake until that process exits |
caffeinate -d |
Keeps the display on |
The tidy-recipes agent runs in a desktop app, so you can’t start it through caffeinate. Instead, you run caffeinate -i -t 3600, an hour for 40 minutes of work, and leave that Terminal window open. Activity Monitor’s PID column gives the ID for -w, and man caffeinate lists every option.
3. Change Mac sleep settings for longer sessions
If agents run for hours or every day, change a setting instead. Apple’s guide to sleep and wake settings shows where:
- Laptop: System Settings > Battery > Options, turn on “Prevent automatic sleeping on power adapter when the display is off”.
- Desktop: System Settings > Energy, turn on “Prevent automatic sleeping when the display is off”.
Apple notes that preventing sleep may increase power consumption. “Wake for network access” only lets the Mac wake briefly for shared services. It won’t keep an agent running.
In Mono on a Mac, Settings > Code has “Prevent sleep while an agent works”, on by default. It holds off idle sleep until the agent finishes its turn. Closing the lid still puts the Mac to sleep. “Keep working while locked”, also on by default, lets open sessions carry on while the screen is locked. Mono has no sleep setting on Windows.
4. Keep a Windows PC awake
In Windows 11, open Start > Settings > System > Power & battery > Screen, sleep, & hibernate timeouts, as Microsoft’s guide to power settings shows. On a desktop, the page may be called Power.
Under “Make my device sleep after”, write down the plugged-in value, then set it to Never or to a time well beyond the task.
The next day, you run a similar 40-minute task on “den-pc”, a Windows desktop that sleeps after 30 minutes. That’s shorter than the task, so you write down 30 minutes and choose Never.
To set the timer back from a terminal later, powercfg takes the value in minutes: powercfg /change standby-timeout-ac 30.
5. Prepare a long task you’ll leave running
Fill this in before you leave.
| Check | MacBook Air | den-pc (Windows) |
|---|---|---|
| Power and lid | Adapter in, lid open | Desktop, plugged in |
| What keeps it awake | caffeinate -i -t 3600 |
Sleep set to Never |
| Value to restore | None | 30 minutes |
| Updates installed, no restart waiting | Yes | Yes |
| Agent running, alerts tested, phone charged | Yes | Yes |
Watch the agent’s first step, so it doesn’t stop at a sign-in prompt or a missing tool. Set up coding agent notifications for when it finishes or needs approval, and use the remote coding guide to follow it from your phone.
6. Put your settings back when the task ends
- Stop
caffeinatewith Control-C, or let its time run out. - Turn off any Mac sleep option you turned on.
- On Windows, restore the sleep time you wrote down (30 minutes on den-pc).
Then check that nothing still keeps the computer awake. On a Mac, run pmset -g assertions in Terminal and look under “Listed by owning process”. caffeinate shouldn’t be there, though audio or system entries are normal. On Windows, run powercfg /requests in a terminal opened as administrator. It lists the apps and drivers that keep the display on or the computer awake.
Last, review the agent’s changes before you commit anything. Mono isn’t available yet, but you can join the waitlist.
