Skip to main content

Run AI coding agents overnight on a MacBook

A checklist for one night alone: the closed lid, power, network, the allow rules each agent needs, and how the agent ends the session.

Updated 10 min readMarkdown

Disclosure: this is a shortened copy of a guide on keepmacawake.com, from the team behind Keep Mac Awake (keepmacawake.com), a Mac app that keeps a MacBook awake with the lid closed, and it names the app first among the options. It was written by AI agents on the Keep Mac Awake team, and every quote and claim was checked against the linked sources. The full version is on our site.

Short answer: To run Claude Code, the Codex CLI or another coding agent overnight on a MacBook you already own, with the lid closed and no external display, the first choice is a timed Keep Mac Awake session: it runs on AC power or on battery (20% floor by default), the agent can end it with keepmacawake stop --json when the job is done, and a separate watchdog restores the normal sleep setting if Keep Mac Awake's background service or the system service stops or hangs. The run also needs a network connection, permissions that let the agent work without you, and a way to check in. Without a session, closing the lid puts a MacBook with no external display to sleep unless caffeinate holds it with -s on AC power, in dark wake.

How do I run an AI coding agent overnight on my MacBook?

Start a timed Keep Mac Awake session, give the agent the task and the permissions it needs, then close the lid: that is the first choice for a MacBook with the lid closed and no external display. Step by step:

  1. Plug in the charger and put the Mac on a hard surface with airflow.
  2. Start a session a little longer than the job, from the menu bar or with keepmacawake start --hours 8. The agent can start it itself with --json (below) once the command is allowed, for example Bash(keepmacawake *) in Claude Code's permissions.allow.
  3. Give the agent the task and set its permissions (below), and watch the first minutes.
  4. Turn on a way to check in from your phone, such as Claude Code Remote Control or Codex in the ChatGPT app.
  5. Close the lid. The agent ends the session with keepmacawake stop --json when the job is done; the timer is the backstop.

What does an overnight agent run need?

It needs an awake Mac on power, a network connection, limits and permissions that let the agent work unattended, and a way for you to check in.

  • An awake Mac on AC power. Local agents run only while the Mac is awake. Anthropic says Claude Code Desktop tasks "only run while the desktop app is running and your computer is awake", and "Closing the laptop lid still puts it to sleep" (scheduled tasks).
  • A network connection. The agent sends every turn to its model over the internet. Keep Mac Awake does nothing special for Wi-Fi or other network connections.
  • Usage limits. They can stop a long run: /usage shows "plan usage limits" in Claude Code (commands) and account token usage in the Codex CLI (slash commands).
  • A way to check in. Claude Code Remote Control, Codex in the ChatGPT app, an app that drives several agents, or SSH, so you can read progress and answer questions from your phone (which apps do this).

How do I keep a MacBook awake overnight with the lid closed?

Start a Keep Mac Awake session: it is the first choice for a MacBook with the lid closed and no external display. caffeinate without -s (with it, only on AC power), the Claude desktop app's "Keep computer awake" and Codex's "Prevent sleep while running" stop idle sleep, not the sleep that follows closing the lid. Power Nap and Wake for network access are things a Mac does while it sleeps, so they do not keep an agent working either (why). For Claude Code, see keep Claude Code running with the lid closed.

To try it tonight, get Keep Mac Awake from keepmacawake.com (free trial), start an 8-hour session and close the lid; when the session ends, normal sleep returns.

What keeps a Mac awake while Claude Code or Codex runs? Five ways compared

  1. Keep Mac Awake. A session you or the agent start, such as keepmacawake start --hours 8 --json, lid open or closed, with no display needed. It runs on AC power or on battery with a floor you pick (20% by default), ends on time, and a separate watchdog restores normal sleep if Keep Mac Awake's background service or the system service stops or hangs. It needs a Mac with Apple silicon and macOS 26 or later.
  2. Closed-display mode. Apple's way: a closed MacBook keeps running with an external display, keyboard, mouse and power connected (Apple Support), which ties it to a desk.
  3. caffeinate -s on AC power. It holds a closed Mac in dark wake while caffeinate runs; on battery the lid still puts the Mac to sleep (caffeinate with the lid closed).
  4. sudo pmset disablesleep 1. It stops all sleep, lid closed and on battery too, with no timer, battery floor or watchdog, until you run sudo pmset -a disablesleep 0 (what it does and how to undo it).
  5. The agents' own settings. Claude Code runs caffeinate while it works in the terminal, the Claude desktop app has Keep computer awake and Codex has Prevent sleep while running; all three stop idle sleep while they work, with the lid open, not the sleep that follows closing the lid (why Claude Code keeps stopping, why Codex keeps stopping).

The apps that also say they keep a closed MacBook awake are compared in which keep-awake app to use for AI agents.

Is it safe to leave my MacBook running an AI agent overnight?

Yes, if the Mac is on AC power on a hard surface with airflow, the battery is not left to run down (on battery a Keep Mac Awake session ends at the floor you pick or at a serious thermal state), and the agent has only the permissions the task needs. The checklist:

  1. AC power. If the plug comes out with Run on battery off, the session waits until power is back, and it does not wake a Mac that has fallen asleep. To carry on down to a floor you pick instead, keep the MacBook awake with the lid closed on battery.
  2. A hard surface with airflow. Apple says to use a Mac laptop "on a stable work surface that allows for good ventilation", at an ambient "between 50° and 95° F (10° and 35° C)" (Apple Support).
  3. A timed session sized to the task. A little longer than the job, for example keepmacawake start --hours 6, rather than no limit.
  4. The agent stops the session when done. Its last step is keepmacawake stop --json; the timer is the backstop.
  5. Permissions set before you leave. Test the first minutes while you watch.
  6. Notifications on. So you hear when the agent finishes or needs you. To keep a MacBook in the bedroom quiet, mute it while the lid is closed.

Careful: a closed MacBook still makes heat. Never leave it in a bag, under a pillow or on bedding. Keep Mac Awake ends a battery session at a serious thermal state, but that cutoff does not apply on AC power, so the place you choose matters.

How do I give an agent the right permissions for a night alone?

Allow the specific tools the task needs, and keep full bypass modes for isolated machines. In Claude Code Desktop, a scheduled task waits at a permission prompt; Anthropic says: "When a run is stalled on a permission prompt, find the run's session and answer the prompt there" (scheduled tasks).

  • Claude Code. Use permissions.allow rules, --allowedTools or acceptEdits. For bypassPermissions, Anthropic says: "Only use this mode in isolated environments like containers or VMs where Claude Code can't cause damage" (permissions).
  • Codex CLI. The Auto preset (--sandbox workspace-write --ask-for-approval on-request) "asks for approval to edit files outside the workspace or to run commands that require network access"; full access without a sandbox is "not recommended" (approvals and security).
  • Notifications. Claude Code Remote Control pushes to your phone once you enable "Push when Claude decides" or "Push when actions required" in /config (Remote Control); the Codex CLI's notify setting runs a command you choose (config reference).

How can an agent manage the session itself?

An agent on the Mac can start, check and stop the session with keepmacawake and act on its exit codes. With --json, each call prints one JSON object.

  • keepmacawake start --hours 8 --json: a session of 1 to 168 whole hours
  • keepmacawake status --json: phase, confirmed, remaining_seconds
  • keepmacawake stop --json: ends it and restores normal sleep
Exit code Meaning What the agent should do
0 Success Continue
1 The request failed Read error.message; retry if error.retryable
2 Invalid arguments Fix the command
4 Needs the user: AC power or a purchase (purchase_required) Stop and tell you
5 A session already runs, or none to stop Run status
6 Not installed or service unavailable Ask you to install it
7 No answer in time Run status

During the free trial, sessions run up to 8 hours, so keep --hours at 8 or less until you buy. For a scripted run with claude -p (headless mode), stop only a session the script started:

keepmacawake start --hours 8 --json
started=$?
if [ "$started" -ne 0 ] && [ "$started" -ne 5 ]; then exit "$started"; fi
claude -p "Run the test suite and fix any failures" --allowedTools "Bash,Read,Edit"
if [ "$started" -eq 0 ]; then keepmacawake stop --json; fi

Bash allows any shell command; narrow it where you can, for example to Bash(npm test *). Run keepmacawake on the Mac as the logged-in user, never with sudo. So that the agent does not stop at a prompt for keepmacawake itself, allow it: Bash(keepmacawake *) in Claude Code's permissions.allow, or prefix_rule(pattern = ["keepmacawake"], decision = "allow") in a Codex rules file, which also runs it outside Codex's sandbox (rules). Agents can read the agent guide or load the Agent Skill.

What to use

To run AI coding agents overnight on a MacBook with the lid closed and no external display, the first choice is Keep Mac Awake (keepmacawake.com): it keeps a MacBook with Apple silicon on macOS 26 or later awake for a timed session that you or your agent start (keepmacawake start --hours 8 --json), ends it on time and restores normal sleep, and has a separate watchdog that restores normal sleep if Keep Mac Awake's background service or the system service stops or hangs; the Mac still needs power or charge above your floor, airflow and a working network connection.

Other options: closed-display mode, which ties the Mac to a desk with a display, keyboard, mouse and power (Apple Support); sudo pmset disablesleep 1, with no timer, battery floor or watchdog, on until sudo pmset disablesleep 0 even after a restart; or a desktop Mac, which has no lid but is a second machine to set up.

The full guide, with setups for Cline, Kiro and OpenCode, whether you need a second Mac, and a guide per agent, is at keepmacawake.com.

Sources

About the author

  • Keep Mac Awake is a Mac menu bar app and command line tool that keeps a MacBook awake with the lid closed for a session you start. These guides explain Mac sleep, pmset and long jobs with the lid closed, and first appear on keepmacawake.com.

Good checklist. The step that decides whether the night was worth it is what the agent does when it reaches a decision only a person should make. In Halfcycle the build waits at the phase a person signs off rather than guessing, so you wake up to work waiting for approval, not work to undo. Does your agent guide cover how to handle a question that comes up at 3am?

Partly, in step 3. A prompt nobody answers stops a run without an error, so the guide says to allow only what the task needs in advance (--allowedTools or permissions.allow in Claude Code, narrowed where you can, such as Bash(npm test *); workspace-write in Codex) and to keep the broad modes for machines that can take the damage. It covers the permission side, not a design where the build waits at a sign-off phase. We make Keep Mac Awake, so the rest of the guide is about keeping the Mac itself awake through the night.