Keep AI Agents Running While You Sleep

You queue up a Claude Code session at midnight, expecting a finished branch by morning — and wake up to a stopped agent and a screen that went dark three hours in. macOS sleeps on its own schedule, and AI agent processes don't count as user activity. Keeping an overnight run alive means solving three problems at once: preventing sleep, protecting the battery, and knowing when the job actually ends.
Why the Mac Interrupts Your AI Agent at Night
macOS is built to sleep when it detects no user input — keyboard, mouse, trackpad. An AI agent running in a terminal window doesn't generate input events, so the display sleep timer fires first, then system sleep follows. The agent loses CPU scheduling and network access the moment the machine suspends, leaving the run stopped mid-task with no clean recovery.
Closing the lid is an immediate sleep trigger. By default a MacBook suspends the moment the lid shuts, regardless of what's running in the background. If you closed the laptop and went to bed, your agent almost certainly stopped within seconds. Simple tools like caffeinate -i can block idle sleep, but they don't handle the lid-close case, and they stay active indefinitely even after the job completes — which matters when you want the Mac to sleep normally once the work is done.
Battery adds a third failure mode. macOS has its own critical-battery shutdown, and it may force sleep before that threshold if it decides conditions warrant it. An agent doing heavy inference or local model work can drain a 50% battery in three to four hours. Without a defined stop point, the OS decides when the run ends — and it won't always choose a clean moment.
The Safe Pattern for Overnight Runs
The right approach is process-tied keep-awake: hold a power assertion only while the agent process is running, then release it automatically when the process exits. LidRun's Process Watch does this — it monitors the process list for tools like claude, node, cursor, or python and activates keep-awake when it detects a match. The Mac sleeps normally the rest of the time, which is exactly what you want.
For lid-closed runs, hardware placement matters. Place the Mac on a hard flat surface that allows air to circulate underneath — a desk, a laptop stand, a solid table. Not on a bed, not in a bag, not in an enclosure. The lid closing reduces the airflow path, so heat that normally vents through the keyboard deck needs somewhere else to go. Prefer running plugged in: battery discharge under sustained load generates additional heat compared to running on AC. Detailed setup steps specific to each tool are covered in the keep Claude Code running when MacBook closed and keep Cursor agent running on Mac guides.
Overnight runs are not fully set-and-forget. A well-structured job with defined exit conditions is safer than an open-ended prompt that might loop. Before you sleep, confirm the agent has a clear stopping point — a task list, a timeout, a specific file to write. Agents that can loop on errors or re-prompts will keep running and draining resources without making useful progress.
Related guideThe safety governor: why LidRun won't keep a hot or idle Mac awakeSetting Battery and Thermal Limits
A 20% battery floor is a reasonable starting point. It keeps the machine well above macOS's own emergency threshold, gives the agent time to finish typical tasks, and leaves enough charge for you to use the Mac in the morning. For lighter API-bound workloads — where the agent mostly waits on network responses — 15% can work. For local inference or heavy builds, stay at 20% or above. The key is setting a floor at all: without one, the OS decides when to stop, and it may not stop cleanly.
Thermal limits are worth configuring even if you trust the machine's own protections. macOS will throttle CPU before anything critical happens, but throttling means the agent slows to half speed for extra hours rather than stopping cleanly — and that generates more cumulative heat than a deliberate stop and restart. LidRun watches the system thermal state via Apple's ProcessInfo API and can auto-stop the keep-awake session when it reaches a threshold you set, which helps stay within safety thresholds for long unattended runs. The mac-keep-awake-safety-governor article covers how battery, thermal, and time-cap limits interact.
On Apple Silicon, user-space software monitors thermal state but cannot directly control fan speed — that is kernel territory. Think of it as a guardrail, not a dial. Combine the battery floor with the thermal limit and you have two independent auto-stop conditions: whichever threshold the run hits first pulls the brake, rather than letting the job run until the hardware or OS intervenes.
Getting Notified When the Job Finishes
Push notifications close the loop. Without them you are either waking up to check manually or guessing. LidRun supports ntfy.sh — a free, open notification relay — to send a push to your phone the moment the keep-awake session ends. That session ends when the agent process exits, so the notification is a direct proxy for the job finishing or stopping. No server-side webhook integration required.
The notification signals session end, not job success. Whether the agent completed its task, hit an error, or stopped because a battery or thermal limit fired — the notification looks the same. When it arrives, open the terminal output and the LidRun activity log. The log records which stop condition fired: process exit, battery floor, thermal limit, or manual stop. That takes thirty seconds and tells you whether to go back to sleep or get up and investigate.
Treat the notification as ready for your attention, not everything worked. A quick check of the agent's output file or commit history — even from your phone via a git hosting app — tells you what actually happened. Most runs that reach a clean process exit leave usable output. Most that stopped on a battery or thermal limit leave partial work you can resume from a known state. The difference matters, and you can see it in the log before you even get out of bed.
A feature of LidRun for closed-lid Mac.
LidRun keeps your work running with the lid closed, with battery and thermal safety built in.
Frequently asked
macOS treats AI agent processes as background work and applies its normal sleep timer regardless of CPU activity. No user input is detected, so display sleep fires first, then system sleep suspends the machine. The agent loses CPU scheduling and network access at that point, ending the run. Sleep prevention tools — tied to the agent process — keep the machine awake only for the duration of the job.
Use a keep-awake tool tied to the Claude process rather than a global always-on mode, so sleep prevention lifts automatically when the agent finishes. Set a battery floor around 20% and a thermal limit so the run stops cleanly if conditions deteriorate. Run on a hard flat surface with airflow underneath, prefer charging over battery, and enable push notifications so you know when the session ends without having to check manually.
20% works for most workloads. It stays well above macOS's own emergency shutdown threshold and gives the agent a clean stopping point before the OS takes drastic action. For lighter API-bound tasks you can drop to 15%; for local inference or heavy builds, stay at 20% or higher. The key is setting a floor at all — without one, macOS decides when to stop and may not choose a clean moment in your work.
Running lid-closed on a hard flat surface with good airflow underneath helps reduce heat risk significantly. The main danger is restricted airflow — never run overnight in a bag, on a bed, or in an enclosed space. Apple Silicon's thermal management throttles the CPU before anything critical happens, but setting a thermal auto-stop adds a second layer that keeps the run within safety thresholds and stops it cleanly if temperatures climb during extended unattended operation.