Keep Claude Code running with your MacBook lid closed
Close the lid mid-task and macOS puts the Mac to sleep, and the agent stops wherever it was. Here's why that happens, the ways around it, and what each one costs you.
Why closing the lid stops the agent
Apps can hold off idle sleep, the kind that kicks in after a few minutes with nobody at the keyboard. Closing the lid is a separate trigger, and ordinary sleep assertions don't block it. When the Mac sleeps, everything sleeps with it: Claude Code stops making tool calls, its network requests stall, and nothing moves until you open the lid again. The same goes for Codex, Gemini CLI, Cursor or anything else running in a terminal.
Option 1: clamshell mode
A MacBook stays awake with the lid closed when it's on power and connected to an external display. Good at a desk. No use in a bag, on a train, or anywhere without a monitor.
Option 2: caffeinate
caffeinate ships with macOS. Wrap the agent in it and the Mac stays up for as
long as that process runs:
caffeinate -i claude
That stops idle sleep while the lid is open. It doesn't stop lid sleep: close the lid and the Mac sleeps anyway. It also holds for the whole session, including the time Claude Code sits at a prompt waiting for you.
Option 3: switch sleep off with pmset
You can turn system sleep off altogether, lid included:
sudo pmset -a disablesleep 1
It works, and it keeps working until you run sudo pmset -a disablesleep 0.
Forget, and the Mac stays awake in your bag all night, getting hot and draining its
battery. It's a global switch with no idea whether an agent is doing anything.
Option 4: HoldMyCode
HoldMyCode holds the lid for you, but only while an agent is actually working. Claude Code reports each task starting and finishing through its lifecycle hooks. HoldMyCode blocks sleep, lid included, from the first and hands it back at the second. A session sitting idle at a prompt gets no hold, so the Mac sleeps normally once the work is done.
- Works with the lid closed and no external display, through a small helper macOS asks you to allow once.
- Hooks install themselves for Claude Code, Codex, Gemini CLI and Cursor. Cline asks first, since its config sits in
~/Documents. - A battery threshold where the hold gives up, an on-power-only option, and Low Power Mode respected.
A note on heat
A Mac working hard in a closed bag gets warm. HoldMyCode ends a hold under thermal pressure, but for a long run, a desk or a bag with some air beats a tight sleeve.
Weighing up Amphetamine instead? See HoldMyCode vs Amphetamine vs caffeinate.
Close the lid. The agent keeps going.
Free, about 4 MB, macOS 14 Sonoma or later. No account, no licence key.