Liwu

How to Keep a Mac Awake for AI Agents and Long-Running Tasks

2026-07-29

If a task is running on your Mac, idle system sleep can pause its process, network work, or terminal session. You usually do not need to keep the display lit: system sleep and display sleep are separate, and letting the display turn off saves energy while the task continues.

Verified: current macOS, Codex, Claude Code, GitHub Copilot, and Liwu behavior checked July 28, 2026.

First, check where the task runs

“AI agent” can describe two different execution models:

  • A local agent or command runs on your Mac. Codex local environments use local worktrees and integrated-terminal actions; Codex CLI runs from your terminal. Claude Code is also installed and started on your development machine. Commands these tools launch locally depend on the Mac staying awake.
  • A cloud agent runs in a remote environment. Codex cloud checks out the repository into an isolated container, while GitHub Copilot cloud agent uses an ephemeral development environment. The remote task can continue if your Mac sleeps or disconnects.

A cloud task may still hand work back to your Mac. A local build, test, preview server, download, or follow-up command will again depend on local power state. Check the task's execution location instead of assuming every agent is local or every agent is remote.

System sleep and display sleep are different

Display sleep turns off the screen. System sleep suspends most local work. Apple exposes separate controls for when the display turns off and whether a Mac should automatically sleep after the display is off.

For a long local task, the energy-efficient combination is usually:

  1. prevent idle system sleep;
  2. allow display sleep;
  3. stop the sleep prevention when the task ends or battery charge becomes low.

Keeping the display on is useful when you need to watch a dashboard, progress view, or presentation. It is not required just to keep a background build, test, render, download, or local coding agent running.

Recommended: use Liwu Keep Awake

Liwu is recommended when you want visible, persistent control with a timer and low-battery protection. It provides the same basic separation as a menu-bar control:

  • the Mac stays awake while the display may sleep by default;
  • you can instead choose Keep display on;
  • the session can end after 30 minutes, 1 hour, 2 hours, 4 hours, or remain unlimited;
  • a low-battery floor can stop the session automatically while running on battery;
  • lid-open operation is Free, while lid-open-and-closed operation requires Pro.

Liwu must remain running because the visible App owns the local power assertions. Choosing Quit Liwu releases them and requests restoration of normal lid behavior. A timer, a battery cutoff, or the manual switch also ends the session and releases the assertions.

Liwu is useful when you want a persistent control you can inspect without keeping a terminal command in mind. It is not required for cloud-hosted work that is already executing remotely.

Built-in alternative: change macOS settings temporarily

On a Mac laptop, open System Settings → Battery → Options. When available, Prevent automatic sleeping on power adapter when the display is off keeps the Mac from automatically sleeping while connected to power. Display timeout is configured separately under Lock Screen.

This is a good built-in choice for a desk setup. The option may not be available on every Mac, applies to the power-adapter case described by Apple, and does not provide a per-task timer or low-battery cutoff. Remember to restore your preferred setting if you changed it only for one job.

Command-line alternative: use caffeinate

macOS includes /usr/bin/caffeinate. With no assertion flag, it prevents idle system sleep until it exits. The -i flag makes that intent explicit.

Keep the Mac awake until you press Control-C:

caffeinate -i

Keep it awake for two hours:

caffeinate -i -t 7200

Keep it awake for exactly as long as a command runs:

caffeinate -i make test

Add -d only when you also need to prevent display sleep:

caffeinate -i -d

caffeinate is often the best zero-install command-line option. Its assertion ends when the caffeinate process exits. The timeout is measured in seconds, and the manual notes that -t is not used when caffeinate launches a utility directly.

Choose a timer and battery cutoff

Use the shortest duration that comfortably covers the task. A finite timer limits the cost of forgetting that sleep prevention is active. If the task may continue on battery, set a floor high enough to leave charge for your next step.

Liwu's battery cutoff applies when the Mac is genuinely using battery power. While connected to an adapter, reaching that percentage does not end the session. caffeinate has a time limit but no battery-percentage rule, so battery monitoring remains your responsibility.

What happens when the lid is closed

Closing a MacBook lid is not the same event as idle system sleep. Do not assume that a normal idle-sleep assertion or a display setting guarantees closed-lid operation.

Liwu's Lid open + closed scope uses its approved helper to apply closed-lid sleep control and is a Pro feature. If that helper request fails, Liwu reports the failure and falls back to the applied Lid open only scope instead of claiming closed-lid operation succeeded.

Keep a closed MacBook on a stable, ventilated surface. Never run a long, heat-producing workload inside a bag or another enclosed space.

Limits and troubleshooting

  • These methods prevent idle sleep; they do not override shutdown, restart, a manual Sleep command, exhausted battery, or every protective action macOS may take.
  • If the display turns off but the task continues, that is the intended energy-saving behavior. Choose a display assertion only when you need the screen visible.
  • If a local job stops, confirm whether its process is still running, whether the Mac slept, and whether the terminal or App holding the assertion exited.
  • If an agent is cloud-hosted, inspect its remote session before changing Mac sleep settings. The local Mac may not be the machine doing the work.
  • If closed-lid behavior is required, verify the applied scope before walking away; requested scope and successfully applied scope are not always the same.

Sources