Keep AI coding agents running overnight on a MacBook.

By Matyas Rathonyi. Updated September 29, 2026. Guides

Overnight runs fail for six reasons: lid sleep, idle sleep, network, permission prompts, battery, and heat.

Six causesLid, idle, network, prompts, battery, heat
Cloud firstCloud sessions need no awake laptop
Plug inHard surface, vents clear
HeartbeatA timestamp in the log every minute

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 trial
A night from lid close to morning. The closed MacBook stays awake and a heartbeat keeps arriving every minute until morning. Lid closes 23:00 Heartbeat every minute Work done 07:00
Close the lid at night. Find the work done in the morning.With the Mac kept awake, plugged in and pre-approved, the agent keeps working and a heartbeat in the log proves it did.

Overnight 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

Clamshell for Mac keeps your agents running with the lid closed. Free for 7 days, then $9.99 once for one Mac.

Download free trial

Pre-flight checklist

  1. Commit or branch first. Start the run from a clean state so the morning diff shows only the agent's work.
  2. Decide the permissions up front. Claude Code's non-interactive mode takes -p with --allowedTools or --permission-mode, and recent versions add --permission-prompts none for unattended runs. codex exec is read-only by default; --sandbox workspace-write allows edits in the workspace. For other tools, find their equivalent. Grant the narrowest access that lets the task finish.
  3. Write output to a file. Terminal scrollback is not a record. Log the agent's output and a timestamp heartbeat.
  4. Plug in. A long run on battery is a gamble. If you must use battery, set a cutoff (see below).
  5. Pick the surface. A hard, flat desk with open air around the vents. Not a bed, sofa, closed drawer or bag.
  6. Disconnect external displays if you are relying on Clamshell. It pauses instead of arming while an external display is connected.
  7. Arm and confirm. Check the menu bar shows armed. A URL request or shortcut is not confirmation.
  8. 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.

Check the run in the morning

  1. Read the heartbeat. Timestamps a minute apart all night mean the Mac stayed awake. A gap marks when it stopped.
  2. Match gaps to sleep. pmset -g log lists 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.
  3. Read the agent's last output in the log. A question, a permission denial, or an API error explains most stops.
  4. Review the diff with git status and git diff before accepting anything.
  5. 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

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.