How to keep Claude Code running with the lid closed
Five ways to keep a Claude Code session alive after you close your MacBook, what each one costs you, and which to pick.
By Harsh Soni, maker of Latte · Updated
A MacBook sleeps the moment you close the lid, and Claude Code stops with it. To keep it running you need one of these: clamshell mode (power plus an external display), the pmset disablesleep setting, a keep-awake app with a closed-lid mode, or a tool like Latte that holds the Mac awake only while the agent is working.
Why closing the lid stops Claude Code
Claude Code runs on your Mac, so when the Mac sleeps, it stops. Recent versions of Claude Code already hold off idle sleep while they work, so a Mac left open on your desk keeps going. Closing the lid is different: macOS treats it as a direct instruction to sleep, and ordinary keep-awake tools can't override it.
Your session isn't lost while the Mac sleeps. It just stops, and anything that needed the network (API calls, package installs, tests against a server) may fail or time out when it wakes.
Option 1: Clamshell mode
Apple supports running a MacBook closed if it's connected to power, an external display, and an external keyboard and mouse or trackpad. Close the lid and the Mac carries on, using the monitor.
- Good for: a desk setup where the laptop already lives docked.
- Doesn't help: on battery, in a bag, or anywhere without a monitor.
Option 2: pmset disablesleep
This system setting stops the Mac from sleeping at all, including when you close the lid:
sudo pmset -a disablesleep 1 # never sleep, even with the lid closed
sudo pmset -a disablesleep 0 # back to normalIt works, but it's a blunt tool:
- It doesn't know when Claude Code finishes, so the Mac stays awake until you remember to turn it off.
- A closed MacBook running in a bag can get hot and drain its battery completely.
- If you forget to undo it, your Mac stops sleeping for good.
Set a reminder to run the second command, keep the Mac on a hard surface, and check the battery before you leave. Run pmset -g to see the current setting.
Option 3: caffeinate
macOS includes caffeinate, which can tie a keep-awake hold to a single command, so it ends when that command exits:
caffeinate -i claudeThat keeps idle sleep away for the whole session. It doesn't stop a closed lid from sleeping the Mac (unless you're in clamshell mode), and -s only works on power. More in caffeinate vs Latte.
Option 4: A keep-awake app with a closed-lid mode
Some keep-awake apps, such as Amphetamine, offer a closed-display mode. They're well made and easy to use. The catch for coding agents is that they keep the Mac awake for a session or a trigger you set, not for the agent's actual work, so the Mac either stays awake long after the run ends or sleeps in the middle of it. More in Amphetamine vs Latte.
Option 5: Latte
Latte is a menu-bar app built for this exact problem (I make it). It hooks into Claude Code, Codex and Grok, so it knows when an agent is actually working:
- Keeps the Mac awake through idle sleep, the lock screen and a closed lid, but only while an agent is working.
- Lets the Mac sleep on its usual schedule the moment the work is done.
- Lid closed on battery is off until you turn it on, and then it sleeps at your battery floor (always at 7%) and pauses agents when the Mac gets hot.
- Sends Claude Code's permission prompts and questions to your phone, so the run doesn't sit waiting for a “yes”.
- If Latte itself stops responding, its helper restores normal sleep within 30 seconds.
Which one should you use?
| Your situation | Use |
|---|---|
| Laptop docked to a monitor, on power | Clamshell mode |
| Lid stays open, Claude Code only | Claude Code's own keep-awake, or caffeinate -i claude |
| A one-off overnight run, and you'll remember to undo it | pmset disablesleep |
| You close the lid and leave, often, with several agents | Latte |
Don't forget the prompts
Keeping the Mac awake is only half of it. A long run often stops on a permission prompt and waits there until you're back. See how to approve Claude Code prompts from your phone.
Common questions
- Does Claude Code keep running when I close my MacBook?
- No. Closing the lid puts the Mac to sleep and Claude Code stops until it wakes. It keeps running only in clamshell mode (power plus an external display), with the pmset disablesleep setting, or with a tool such as Amphetamine's Closed-Display Mode or Latte.
- Does caffeinate keep a Mac awake with the lid closed?
- No. caffeinate holds off idle sleep, but closing the lid still sleeps the Mac unless it is in clamshell mode with power and an external display.
- Is it safe to run a MacBook with the lid closed in a bag?
- Only with limits. A closed MacBook that never sleeps can overheat and drain its battery. Use something that sleeps the Mac at a battery floor and at high heat, as Latte does, and turn any always-awake setting off afterwards.
Latte keeps Claude Code, Codex and Grok working with your MacBook closed, sends approvals to your phone, and lets the Mac sleep the moment the work is done.