Overnight runs fail for six reasons: lid sleep, idle sleep, network, permission prompts, battery, and heat.
Clamshell for Mac keeps the Mac awake with the lid closed while armed. No sudo, no monitor. Free for 7 days, then $9.99 once.
Download free trialOvernight agent runs on a MacBook usually fail for one of six reasons: the lid closed and the Mac slept, idle sleep kicked in, the network dropped, the agent stopped to ask for permission, the battery ran out, or the Mac got too hot. Keep the Mac awake with the lid shut, pre-approve what the agent needs, plug in on a hard surface, and log a heartbeat so you can check the run in the morning. For the lid, Clamshell for Mac is a menu bar app that keeps an Apple Silicon MacBook awake with the lid closed, without an external monitor or sudo.
Quick answer
If the job can run in the cloud
Claude Code cloud sessions and Codex Cloud: send it there. Your laptop does not need to stay awake.
If it must run locally
A CLI agent, a desktop agent on a local project, a model served by Ollama, a long build: keep the Mac awake with the lid closed.
Remove every prompt the agent could stop on
Use the tool's own non-interactive flags and the narrowest permissions that work.
Plug in, ventilate, log a heartbeat
Then run a two-minute lid-closed check before bed.
What stops an overnight run
Lid sleep
Morning sign: work stopped around the time you closed the lid. Fix: an external display setup, pmset disablesleep, or a lid-sleep tool.
Idle sleep, lid open
Morning sign: work stopped some minutes after you walked away. Fix: caffeinate -i around the command, or any lid-closed method.
Network drop
Morning sign: API errors, timeouts, a disconnected remote session. Fix: stable Wi-Fi or Ethernet; check VPN and captive portals.
Permission prompt
Morning sign: the agent waited all night on one question. Fix: pre-approve with the tool's flags or settings.
Battery
Morning sign: the Mac is off or asleep with a flat or low battery. Fix: plug in; set a low-battery cutoff.
Heat
Morning sign: slow progress, or a safety disarm. Fix: hard, open surface; fewer parallel jobs.
Agents can also stop for reasons no keep-awake tool can touch: a usage or rate limit, an API error, a crashed process, or a task the agent decided was done. The heartbeat and log below help you tell these apart from sleep.
Local or cloud: check where the agent runs
Cloud: the laptop is only a window
Anthropic's docs say a Claude Code cloud session runs on cloud infrastructure and keeps running after you close your laptop. OpenAI's docs say each Codex Cloud task has its own workspace and can keep working while your computer is asleep. For those, the laptop is only a window.
Local: the Mac must stay awake
Everything that executes on the Mac needs the Mac awake: Claude Code in your terminal or in the desktop app with Local selected, the Codex CLI, Cursor's agent working in your local editor, local models served by Ollama, Xcode or cargo builds, Docker containers, and test suites. Phone remote control, such as Claude Code Remote Control or Codex Remote, steers that local session; the work still runs on the Mac.
Keep the Mac awake with the lid closed
- Apple's closed-display mode needs an external display, keyboard and mouse, and power. It is supported and works well if you already have that desk setup.
sudo pmset -a disablesleep 1works with no hardware, but it is global, needs admin rights, has no timer, and applies on battery. The risk is forgetting it. How to check and undo it.- Clamshell changes lid behavior only while armed, shows its state in the menu bar, needs no admin rights, and never persists across a restart. Arm it from the menu, with Option held while closing the lid on Macs with a lid angle sensor, from Shortcuts, or with
clamshell://arm.
Clamshell for Mac keeps your agents running with the lid closed. Free for 7 days, then $9.99 once for one Mac.
Download free trialPre-flight checklist
- Commit or branch first. Start the run from a clean state so the morning diff shows only the agent's work.
- Decide the permissions up front. Claude Code's non-interactive mode takes
-pwith--allowedToolsor--permission-mode, and recent versions add--permission-prompts nonefor unattended runs.codex execis read-only by default;--sandbox workspace-writeallows edits in the workspace. For other tools, find their equivalent. Grant the narrowest access that lets the task finish. - Write output to a file. Terminal scrollback is not a record. Log the agent's output and a timestamp heartbeat.
- Plug in. A long run on battery is a gamble. If you must use battery, set a cutoff (see below).
- Pick the surface. A hard, flat desk with open air around the vents. Not a bed, sofa, closed drawer or bag.
- Disconnect external displays if you are relying on Clamshell. It pauses instead of arming while an external display is connected.
- Arm and confirm. Check the menu bar shows armed. A URL request or shortcut is not confirmation.
- Run a two-minute proof. Start the heartbeat, close the lid for two minutes, open it, and confirm the timestamps kept arriving.
A wrapper with a heartbeat
This zsh wrapper requests arming, writes a timestamp every minute, runs the agent, and requests disarming on exit. Swap the agent line for Codex, a build, or any other command.
#!/bin/zsh
set -e
LOG="$HOME/agent-night-$(date +%Y%m%d).log"
open "clamshell://arm"
( while true; do date -u '+beat %Y-%m-%dT%H:%M:%SZ' >> "$LOG"; sleep 60; done ) &
HB=$!
trap 'kill $HB 2>/dev/null; open "clamshell://disarm" || true' EXIT
claude -p "Run the test suite and fix any failures" \
--allowedTools "Bash,Read,Edit" >> "$LOG" 2>&1
date -u '+done %Y-%m-%dT%H:%M:%SZ' >> "$LOG"
The open calls only deliver requests; confirm the armed state in the menu before closing the lid. The trap does not run if the shell is killed. Clamshell has no agent integration and does not detect when the agent finishes; this script is what ends the session.
Power, heat and ventilation
With the lid closed, the built-in display is off, which removes a large part of a laptop's normal power draw. The rest depends on the work. A mostly idle agent waiting on API responses is light. Local builds, test suites and especially local models are heavy, and they draw the same power and make the same heat as they would with the lid open, with less room for air to move if the Mac sits on something soft.
- Plug in for overnight runs. On battery, a heavy workload can empty it well before morning.
- Leave the vents clear. Closed-lid MacBooks still need airflow. Never run a heavy job in a bag or under a blanket.
- Thermal safety is always on in Clamshell. If macOS reports serious or critical thermal pressure, Clamshell disarms toward normal lid sleep and refuses to arm until the Mac cools. That pauses your run on purpose. If it happens, cut parallel jobs or move the Mac.
- Low battery cutoff. Clamshell's Disarm on low battery is off by default. Turn it on in General settings and choose a level so an unplugged run lets the Mac sleep instead of draining flat.
Check the run in the morning
- Read the heartbeat. Timestamps a minute apart all night mean the Mac stayed awake. A gap marks when it stopped.
- Match gaps to sleep.
pmset -g loglists sleep and wake events with timestamps. A sleep entry at the gap points to sleep; no sleep entry points to the agent, the network or a limit. - Read the agent's last output in the log. A question, a permission denial, or an API error explains most stops.
- Review the diff with
git statusandgit diffbefore accepting anything. - Disarm. If the wrapper did not, disarm from the menu bar before the Mac goes into a bag.
What Clamshell does and does not do
Does
Clamshell keeps an Apple Silicon MacBook on macOS 14 or later awake with the lid closed, keeps the built-in display black, and locks the Mac when you open the lid. It works on battery and power and re-applies itself when you plug in or unplug.
Does not
It does not detect agents, stop when a task finishes, answer permission prompts, keep Wi-Fi connected, or make a sleeping Mac reachable. Keeping the Mac awake removes one cause of stalled runs; the checklist above handles the rest.
Frequently asked questions
Can I run Claude Code overnight with the MacBook lid closed?
Yes, if the Mac stays awake. A local Claude Code session pauses when lid sleep starts. Use an external display setup, pmset disablesleep, or a lid-sleep tool such as Clamshell, and pre-approve the tools the run needs so it does not wait on a prompt. Claude Code cloud sessions keep running without your laptop.
Why did my AI agent stop overnight even though the Mac was awake?
Common causes are a permission prompt nobody answered, a network drop, a usage or rate limit, an API error, or the agent deciding the task was finished. A timestamp heartbeat in a log file shows whether the Mac was awake, and the agent's last output usually shows why it stopped.
Is it safe to leave a MacBook running all night with the lid closed?
It is reasonable on a hard, ventilated surface and connected to power. Do not run heavy work in a bag or on soft furnishings. Clamshell disarms toward normal lid sleep if macOS reports serious or critical thermal pressure.
Does Clamshell know when my agent is finished?
No. Clamshell does not detect Claude Code, Codex, Cursor or any other agent. Wrap the job in a script that opens clamshell://disarm on exit, or disarm from the menu bar when you are back.
Can I run a local model with Ollama overnight on a MacBook?
Yes, with the Mac awake, plugged in and ventilated. Local models are among the heaviest workloads a laptop runs, so expect sustained heat and power draw, and check the heartbeat log in the morning.
How do I check whether my Mac slept during the night?
Run pmset -g log and look for sleep and wake entries around the time your log shows a gap. Compare them with a timestamp heartbeat written by your wrapper script.
Related guides
- Keep Claude Code running with the lid closed
- Keep Codex running when you close your MacBook lid
- Keep a MacBook awake with the lid closed on battery
- Why caffeinate does not work with the lid closed
- Compare Clamshell, Amphetamine, pmset and Apple's setup
Sources checked September 29, 2026: Anthropic's Claude Code cloud sessions and non-interactive mode docs, OpenAI's Codex Cloud and non-interactive mode docs, Apple's external display guidance for a closed laptop, and the macOS pmset(1) manual. Clamshell behavior is described from its app source and FAQ. No new hardware testing was done for this page. Clamshell is an independent app, not affiliated with Anthropic, OpenAI, Cursor or Ollama. Product names belong to their owners.