Keep Claude Code running with your MacBook lid closed
Claude Code stops when you close your MacBook, because the Mac goes to sleep and takes the session with it. The fix: keep the Mac awake only while Claude works, and let it sleep right after.
Updated
Why closing the lid stops Claude Code
A sleeping Mac pauses every process and drops its network connection. Claude Code is waiting on the model halfway through a task, the connection is gone, and the task stops. You open the lid an hour later to the same half-done refactor.
caffeinate doesn't solve it. It keeps an idle Mac awake, but closing the lid still puts a MacBook to sleep unless you're in clamshell mode with an external display.
Keeping the Mac awake forever is the wrong fix
You can switch lid-closed sleep off with sudo pmset -a disablesleep 1 (see the three ways to keep a MacBook awake). But then the Mac also keeps running long after Claude is done, draining the battery in your bag. What you want is awake while Claude works, asleep the minute it stops.
How Owl Mode does it
Claude Code has hooks: small commands it runs when you send a prompt and when it finishes. Owl Mode adds four of them to your Claude Code settings. Each one only marks the session as “working” or “done”.
- You send a prompt: Owl Mode keeps the Mac awake, lid open or closed.
- Claude keeps working through its tools: Owl Mode stays on.
- Claude stops: about a minute later Owl Mode lets go, and with the lid closed the Mac goes to sleep on the spot.
Your code, prompts and output are never stored or sent anywhere. If you like, Owl Mode texts your phone when the task is done, so you know before you open the lid.
Codex, Cursor and Gemini CLI too
The same trick works for the other agents that have hooks:
- Codex: Owl Mode adds hooks to
~/.codex/hooks.json. Codex asks you to approve new hooks once: type/hooksin Codex. - Cursor: hooks in
~/.cursor/hooks.json, picked up automatically. - Gemini CLI: hooks in
~/.gemini/settings.json.
Run several at once and the Mac stays awake until the last one is done.
Set it up in two minutes
- Install Owl Mode and allow it once with your password.
- In the welcome window, keep “Stay awake automatically while AI agents work” switched on.
- Start a task, close the lid, and go.
One more thing: heat
Long agent runs can keep the processor busy. Owl Mode lets the Mac sleep if macOS reports it is running hot, but give it some air: a desk or an open bag, not a zipped sleeve. See is it safe to run a MacBook with the lid closed?