Mac Guide

How to Keep Claude Code Running With the MacBook Lid Closed

Close the lid on a long Claude Code or Codex task and the Mac goes to sleep, and the task waits until you open it again. Why caffeinate does not prevent that, the three things that do, the exact commands, what each one risks, and when a cloud session is the simpler answer.

Short answer

Closing a MacBook's lid makes it sleep, and Claude Code's own caffeinate does not stop that. For a local session, run sudo pmset -a disablesleep 1 and undo it with 0 afterwards, use clamshell mode with power and an external display, or use a tool that blocks lid sleep only while an agent works. Cloud sessions run without the Mac.

Key points

  • caffeinate -i stops idle sleep only. Closing the lid is a separate trigger, and caffeinate -s applies only on AC power.
  • sudo pmset -a disablesleep 1 keeps a MacBook running with the lid shut, on battery too, until sudo pmset -a disablesleep 0. Left on, it can run the battery to empty and takes no notice of heat.
  • Clamshell mode is the way Apple documents, but it needs power, an external display, a keyboard and a mouse or trackpad.
  • A short script, or AgentAwake, keeps lid sleep off only for as long as the agent is working.
  • A Claude Code cloud session runs on Anthropic's infrastructure and keeps going with the laptop closed. It fits work that needs only the repository, not your Mac.

Why does a MacBook sleep with the lid closed even when caffeinate is running?

Because closing the lid is not idle sleep, and idle sleep is what caffeinate normally prevents. macOS treats "nothing has happened for a while" and "the lid just closed" as two different reasons to sleep. Apple's documentation for the idle-sleep assertion says it plainly: the system may still sleep for lid close, the Apple menu or low battery.

The caffeinate man page (checked on macOS 27) lists five kinds of assertion. -d keeps the display on, -i stops idle system sleep, -m stops idle disk sleep, -u declares the user active, and -s prevents system sleep but is "valid only when system is running on AC power". With no flag it holds an idle-sleep assertion. None of them is documented to cover the lid, and on battery -s does not apply at all.

This catches agent users out because Claude Code already runs a caffeinate while it writes a reply. With the lid open the Mac stays awake and looks protected. Close the lid and it sleeps anyway. Anthropic's documentation says the same of the desktop app's Keep computer awake setting: closing the laptop lid still puts it to sleep.

While the Mac sleeps, nothing on it runs. Claude Code keeps the session, picks it up again on wake and retries a request that sleep cut off, but none of the work gets done while the lid is shut. Two commands show what is going on:

# What is holding the Mac awake, and with which assertion
pmset -g assertions

# Is lid sleep switched off? 1 = off, 0 = normal
pmset -g | grep SleepDisabled
What happens when you close the lid on a working agent
  1. Agent workingClaude Code holds a caffeinate, so idle sleep is blocked
  2. Lid closesA separate sleep trigger the assertion does not cover
  3. Mac sleepsEvery process pauses, the agent included
  4. Lid opensThe agent picks up where it stopped; the time in between is lost

How do you keep a MacBook awake with the lid closed using pmset?

Run sudo pmset -a disablesleep 1. It turns system sleep off entirely, lid close included, on battery and on power, until you run sudo pmset -a disablesleep 0. It needs your admin password and changes a system-wide setting, not just the current session.

The flag is not in the pmset man page (checked on macOS 27), but it is widely used: pmset -g reports it as SleepDisabled, Amphetamine's Power Protect helper installs a sudo rule for exactly these two commands, and AgentAwake switches the same setting. Check it took effect before you close the lid:

# Before you close the lid
sudo pmset -a disablesleep 1
pmset -g | grep SleepDisabled    # expect: SleepDisabled 1

# When the agent has finished
sudo pmset -a disablesleep 0

What are the risks of pmset disablesleep?

The risk is that it stays on. Nothing in macOS turns it back off when the agent finishes, so a Mac you put in a bag after the job is still awake and still drawing power.

On battery it can run until the battery is empty, which also loses whatever was not saved. A closed Mac in a bag has little ventilation, the opposite of what Apple advises for a Mac laptop (a stable work surface that allows for good ventilation), and the setting itself takes no notice of temperature. And if you forget it, the Mac stays awake through every lid close afterwards, for days if nobody checks.

Each of those is fixable: undo the setting when the command ends, stop at a battery floor, and react to heat. The script in the next section does the first two for free.

Lid sleep off by hand, or only while an agent works

disablesleep 1 by hand

  • Stays on after the agent finishes
  • Runs the battery down to empty
  • Nothing reacts to heat in a bag
  • A forgotten switch stays on for days

Off only while the work runs

  • Back on when the work stops
  • Sleeps at a battery floor you set
  • Pauses or cools when the Mac runs hot
  • Restored by a watchdog if the tool stops

A safer free version: switch lid sleep off for one command

Wrap the agent in a script that turns lid sleep off, runs it, and turns lid sleep back on however the command ends. First let the three pmset commands run without a password, because sudo forgets your password after a few minutes and the undo at the end of a long job would wait for one with nobody there to type it:

# Once: allow exactly these three pmset commands without a password
sudo visudo -f /etc/sudoers.d/lid-awake
# add this one line, using your short user name (run: whoami)
yourname ALL=(root) NOPASSWD: /usr/bin/pmset -a disablesleep 0, /usr/bin/pmset -a disablesleep 1, /usr/bin/pmset sleepnow
#!/bin/bash
# lid-awake: lid sleep off for one command, back on when it ends
# or when the battery falls to FLOOR %. Usage: lid-awake codex
FLOOR=20
P=/usr/bin/pmset
sudo -n $P -a disablesleep 1 || exit 1
( while sleep 60; do
    b=$(pmset -g batt)
    pct=$(echo "$b" | grep -Eo '[0-9]+%' | head -1 | tr -d %)
    if echo "$b" | grep -q "Battery Power" && [ "$pct" -le "$FLOOR" ]; then
      sudo -n $P -a disablesleep 0 && sudo -n $P sleepnow
      exit
    fi
  done ) &
watcher=$!
disown
restore() {
  kill $watcher 2>/dev/null
  sudo -n $P -a disablesleep 0
  ioreg -r -k AppleClamshellState -d 1 | grep -q '"AppleClamshellState" = Yes' && sudo -n $P sleepnow
}
trap restore EXIT
"$@"
# Install it, then start your agent through it
chmod +x lid-awake
sudo mkdir -p /usr/local/bin && sudo mv lid-awake /usr/local/bin/
lid-awake claude
  • Lid sleep comes back when the agent exits, when you press Ctrl-C, and when the terminal window closes.
  • On battery, a watcher checks once a minute and sends the Mac to sleep at 20%. Change FLOOR to suit.
  • If the lid is still shut when the job ends, pmset sleepnow puts the Mac to sleep instead of leaving it running in the bag.
  • It cannot help after a kill -9, a crash of the shell or a power cut. Run pmset -g | grep SleepDisabled if in doubt.
  • It watches neither heat nor whether the agent is still working. An agent that sits waiting for your approval keeps the Mac awake until you answer.

Does clamshell mode work for AI agents?

Yes, if you are at a desk. Clamshell, or closed-display, mode is the way Apple documents for running a MacBook with the lid shut: connect it to power and to an external display, keyboard and mouse or trackpad, then close the lid and keep working on the external screen. Nothing is changed in the system, so there is nothing to undo.

It does not cover the case most agent users have in mind, which is closing the lid and leaving: on battery, or with nothing plugged in, closing the lid still sleeps the Mac.

Can Claude Code keep working without the Mac staying awake?

Yes, as a cloud session. Anthropic's documentation says a cloud session runs on infrastructure Anthropic manages and keeps running after you close your laptop. Start one at claude.ai/code, in the Claude app, or from the terminal with claude --cloud "<task>". Claude works on a copy of the repository in an isolated virtual machine and pushes a branch for you to review. Cloud sessions come with the Pro, Max, Ultra and Team plans and some Enterprise seats, and count toward the plan's usage limits.

It does not cover everything a local session does. A cloud session gets the repository only, without your local configuration, so work that needs your own filesystem, tools or MCP servers still has to run on the Mac. Remote Control and background sessions run on the Mac too. The same documentation says the computer has to stay on for Remote Control, and that a session is kept while the machine sleeps and reconnects when it wakes. For that work the Mac has to stay awake, and the rest of this guide applies.

How does AgentAwake keep agents running with the lid closed?

AgentAwake is a menu bar app from SyncEdge that does the pmset switch for you, only while an agent is working. An agent counts as working when Codex or opencode, once connected, report through their hooks, when a caffeinate runs inside it, as Claude Code does while it replies, or when its own process is busy on the processor. By default it watches claude, codex, gemini, aider and opencode. When none is working it waits a grace period, 2 minutes by default, and lets the Mac sleep. Work that carries on after a Claude Code reply ends, such as a background job, counts while it runs under caffeinate -i; connecting Claude Code in the app tells Claude to start such jobs that way.

It adds the safeguards the manual method lacks. On battery the Mac is allowed to sleep at a floor you choose: 10, 15, 20 or 25%. It pauses whenever macOS reports serious heat. It runs the fans: on the default Balanced profile they hold at least 40% while the lid is closed and agents work, and every fan goes to full speed if a sensor reaches 95 °C. The panel shows "Lid sleep blocked ✓" only after reading the setting back from the system, and a watchdog turns lid sleep back on within 60 seconds if the app stops.

Setup asks for your password once. The sudo rule it installs covers exactly three pmset commands: lid sleep off, lid sleep on, and sleep now. It needs macOS 14 or later on Apple silicon or Intel, works fully for 14 days without a card, then costs $9.99 a year for up to 3 Macs.

AgentAwake's safeguards
Default grace period before the Mac may sleep
2 min
Battery floor you choose, on battery power
10 to 25%
Minimum fan speed, lid closed, Balanced profile
40%
Watchdog: lid sleep back on if the app stops
60 s

Which method should you use?

Pick by where the work has to run, then by where the Mac will be. If the task needs only the repository, a cloud session leaves the Mac free to sleep. At a desk with a monitor, clamshell mode needs nothing new. For one job you will check on, the two pmset commands are enough. If you close the lid and walk away often, use something that turns the setting back on by itself: the lid-awake script above, or AgentAwake if you also want the heat pause, fan control and a watchdog.

With the lid open, none of this is needed: caffeinate -i <command> keeps a long build or test run going until it ends.

Ways to keep an AI agent running with a MacBook lid closed
MethodLid closed on batteryTurns itself back onBattery and heat
caffeinate -iNoEnds with its commandNot involved
sudo pmset -a disablesleep 1YesNo, until you run disablesleep 0Nothing watches either
The lid-awake scriptYesWhen its command endsBattery floor; no heat check
Clamshell modeNo, needs power and an external displayNothing to undoNormal macOS behaviour
Claude Code cloud sessionRuns in the cloud; the Mac can sleepNothing to undoNot involved
AgentAwakeYes, only while an agent works2 minutes after the work stops; watchdog within 60 sBattery floor, heat pause, fan control

Before you close the lid on an agent

  • pmset -g | grep SleepDisabled prints 1, or your tool says lid sleep is blocked
  • You know what turns lid sleep back on: a script's exit, an app, or you
  • On battery, something puts the Mac to sleep before the battery is empty
  • The Mac is on a hard surface, not in a bag or on a bed
  • The agent is not waiting for an approval it will never get while you are away
  • Unsaved work in other apps is saved, in case the battery runs out

Frequently asked questions

Does caffeinate keep a Mac awake with the lid closed?

Not reliably. caffeinate creates power assertions, and its man page describes them as stopping idle, display or disk sleep, or, with -s, system sleep on AC power only. Apple's documentation says the system may still sleep on lid close despite the idle-sleep assertion. To keep a MacBook running with the lid shut on battery, use sudo pmset -a disablesleep 1 or a tool that sets it.

How do I undo sudo pmset -a disablesleep 1?

Run sudo pmset -a disablesleep 0, then pmset -g | grep SleepDisabled: 0 means normal lid sleep is back. The setting stays until you change it, so undo it in the same script that set it rather than relying on memory.

Is it safe to close a MacBook lid while Claude Code is running?

It can be, if something keeps the Mac out of sleep and something watches battery and heat. Apple supports running a MacBook with the lid closed in clamshell mode, and advises using a Mac laptop on a stable surface with good ventilation. The risks to manage are a drained battery and heat building up where air cannot move, such as in a bag. Leave it on a hard surface and set a battery floor.

Does Claude Code keep running in the cloud when I close my laptop?

A cloud session does. It runs on infrastructure Anthropic manages, and Anthropic's documentation says it keeps running after you close your laptop. A session in your terminal runs on your Mac, with or without Remote Control, and waits while the Mac sleeps. Use a cloud session when the task needs only the repository, and keep the Mac awake when it needs your local files or tools.

Do I need an external display to run a MacBook with the lid closed?

Only for Apple's clamshell mode, which also needs power and a keyboard and mouse or trackpad. With pmset disablesleep 1, or a tool that sets it, a MacBook keeps running with the lid closed on battery and with no display attached.

Does this work for Codex, Gemini CLI, aider and opencode too?

Yes. pmset disablesleep works at the system level, so every process keeps running, whichever agent it is. AgentAwake recognises claude, codex, gemini, aider and opencode by default, takes start and stop reports from Codex and opencode through hooks, and lets you add other command names.

What does AgentAwake cost?

It works fully for 14 days without a card, then costs $9.99 a year for up to 3 Macs, paid inside the app through Stripe. It needs macOS 14 or later and runs on Apple silicon and Intel MacBooks.

Start with the problem

Want lid sleep to switch itself back on?

AgentAwake keeps your MacBook running with the lid closed only while Claude Code, Codex or another agent is working, then lets it sleep two minutes after the work stops. Battery floor, heat pause, fan control and a watchdog included. Free for 14 days, then $9.99 a year for up to 3 Macs.

Your business,in professional hands.