DEV Community

Little Pan
Little Pan

Posted on Originally published at ponymux.com

How to keep your Mac awake for Claude Code and Codex

You leave Claude Code running, lock the screen, and go to lunch. Later you check in from your phone, and the session doesn't answer: the Mac went to sleep. Locking wasn't the problem. Locked, the Mac keeps running everything. Idle sleep is what pauses the agent.

Each agent keeps the Mac awake only while it's working on a turn:

  • Claude Code: runs caffeinate -i while a turn is running. It lets go while it waits for your approval and after the turn ends.
  • Codex: off by default. Turn on Prevent sleep while running in /experimental. It also covers only a running turn.
  • Grok Build: keeps the display awake during a turn (sleep_prevention, on by default) and releases it when the turn ends.
  • Between turns: keep the Mac awake yourself with caffeinate -i, or the power setting in System Settings.
  • The catch: none of this survives closing the lid or choosing Sleep, unless the Mac runs with the lid closed on an external display.

Does locking my Mac stop Claude Code or Codex?

No. Locking the screen, or letting the display turn off, leaves every process running. An agent halfway through a task carries on.

Sleep is different. When the Mac sleeps, processes pause and the network goes down. A request that was on its way to the model can fail, and anything that connects to the Mac from outside can't reach it: Claude Code's remote control from the phone app, an SSH session, a tunnel. When the Mac wakes, the agent picks up where it paused, and you may need to tell it to continue if a request failed.

So the question isn't whether the screen is locked. It's whether the Mac is allowed to go to sleep while you're away.

When do Claude Code, Codex, and Grok keep the Mac awake on their own?

Only while a turn is running.

Claude Code starts caffeinate -i when it begins working and stops it when the turn ends or when it stops to ask for your approval. So if it's waiting on a permission prompt, the Mac can fall asleep.

Codex has the same idea as an experimental feature, off by default. Type /experimental and turn on Prevent sleep while running, or set it in ~/.codex/config.toml:

[features]
prevent_idle_sleep = true
Enter fullscreen mode Exit fullscreen mode

Grok Build keeps the display awake while the agent works and releases it when the turn ends. It's on by default; to turn it off, set this in ~/.grok/config.toml:

[ui.notifications]
sleep_prevention = false
Enter fullscreen mode Exit fullscreen mode

None of them keep the Mac awake between turns. That's exactly when you're waiting to send the next message, from the keyboard or from your phone.

How do I keep my Mac awake between turns?

Use caffeinate, which ships with macOS. -i stops idle sleep and lets the display sleep and lock as usual:

# For as long as this agent runs
caffeinate -i claude

# For two hours, in the background
caffeinate -i -t 7200 &
Enter fullscreen mode Exit fullscreen mode

The first form lasts exactly as long as the agent: when you quit Claude Code, normal sleep comes back. The same works with codex or grok.

To keep a MacBook awake whenever it's plugged in, open System Settings → Battery → Options and turn on Prevent automatic sleeping on power adapter when the display is off. That's a standing change, so remember to undo it if you only needed it for one evening.

How can I tell what's keeping my Mac awake?

Run:

pmset -g
Enter fullscreen mode Exit fullscreen mode

The sleep line names the apps holding the Mac awake right now, for example sleep prevented by caffeinate. For the full list, with what each one is preventing, run pmset -g assertions.

This is a quick way to check before you leave. If the line doesn't name anything and the agent is between turns, the Mac will sleep on its normal schedule.

What happens when I close the lid?

Closing the lid puts a MacBook to sleep, whatever caffeinate, the agents, or the power settings say. Choosing Sleep from the Apple menu does the same.

The exception is running with the lid closed on purpose: with the Mac on power and connected to an external display, keyboard, and mouse, closing the lid keeps it awake. Without that setup, keep the lid open while you need the agent reachable.

Can I keep the Mac awake with one click?

Yes. In PonyMux, click the coffee cup at the bottom of the sidebar. It turns accent-colored, and its tooltip reads Keep Awake (On).

The bottom of PonyMux's sidebar with Keep Awake on: the coffee cup is highlighted.
The bottom of PonyMux's sidebar with Keep Awake on: the coffee cup is highlighted.

While it's on, PonyMux keeps the Mac from idle sleep, including between turns and while an agent waits for you. The display can still turn off and lock. It doesn't override closing the lid or choosing Sleep, and it has no timer: it stays on until you click the cup again. Turn it off when you're done so the Mac can go back to saving energy.

For the rest of PonyMux's power settings, see Settings and Accessibility.


Originally published on ponymux.com, where the demos are interactive.

Top comments (0)