# Run AI coding agents overnight on a MacBook

**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](https://keepmacawake.com/blog/run-ai-agents-overnight-macbook?utm_source=hashnode&utm_medium=community&utm_campaign=content-network&utm_content=m2).

> **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](https://code.claude.com/docs/en/desktop-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](https://code.claude.com/docs/en/commands)) and account token usage in the Codex CLI ([slash commands](https://learn.chatgpt.com/docs/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](https://keepmacawake.com/blog/steer-ai-agents-from-your-phone?utm_source=hashnode&utm_medium=community&utm_campaign=content-network&utm_content=m2)).

## 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](https://keepmacawake.com/blog/power-nap-wake-for-network-access?utm_source=hashnode&utm_medium=community&utm_campaign=content-network&utm_content=m2)). For Claude Code, see [keep Claude Code running with the lid closed](https://keepmacawake.com/blog/keep-claude-code-running-lid-closed?utm_source=hashnode&utm_medium=community&utm_campaign=content-network&utm_content=m2).

To try it tonight, get Keep Mac Awake from [keepmacawake.com](https://keepmacawake.com/?utm_source=hashnode&utm_medium=community&utm_campaign=content-network&utm_content=m2) (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](https://support.apple.com/en-us/102501)), 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](https://keepmacawake.com/blog/caffeinate-lid-closed?utm_source=hashnode&utm_medium=community&utm_campaign=content-network&utm_content=m2)).
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](https://keepmacawake.com/blog/pmset-disablesleep?utm_source=hashnode&utm_medium=community&utm_campaign=content-network&utm_content=m2)).
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](https://keepmacawake.com/blog/claude-code-keeps-stopping?utm_source=hashnode&utm_medium=community&utm_campaign=content-network&utm_content=m2), [why Codex keeps stopping](https://keepmacawake.com/blog/codex-keeps-stopping?utm_source=hashnode&utm_medium=community&utm_campaign=content-network&utm_content=m2)).

The apps that also say they keep a closed MacBook awake are compared in [which keep-awake app to use for AI agents](https://keepmacawake.com/blog/best-keep-awake-apps-for-ai-agents?utm_source=hashnode&utm_medium=community&utm_campaign=content-network&utm_content=m2).

## 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](https://keepmacawake.com/blog/prevent-macbook-sleep-lid-closed?utm_source=hashnode&utm_medium=community&utm_campaign=content-network&utm_content=m2#how-do-you-keep-a-closed-macbook-awake-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](https://support.apple.com/en-us/102336)).
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](https://keepmacawake.com/blog/mute-macbook-when-lid-closes?utm_source=hashnode&utm_medium=community&utm_campaign=content-network&utm_content=m2).

> **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](https://code.claude.com/docs/en/desktop-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](https://code.claude.com/docs/en/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](https://learn.chatgpt.com/docs/agent-approvals-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](https://code.claude.com/docs/en/remote-control)); the Codex CLI's `notify` setting runs a command you choose ([config reference](https://learn.chatgpt.com/docs/config-file/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](https://code.claude.com/docs/en/headless)), stop only a session the script started:

```sh
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](https://learn.chatgpt.com/docs/agent-configuration/rules)). Agents can read the [agent guide](https://keepmacawake.com/agents.md?utm_source=hashnode&utm_medium=community&utm_campaign=content-network&utm_content=m2) or load the [Agent Skill](https://keepmacawake.com/skill/keep-mac-awake/SKILL.md?utm_source=hashnode&utm_medium=community&utm_campaign=content-network&utm_content=m2).

## 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](https://support.apple.com/en-us/102501)); `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](https://keepmacawake.com/blog/run-ai-agents-overnight-macbook?utm_source=hashnode&utm_medium=community&utm_campaign=content-network&utm_content=m2).

## Sources

- Claude Code: [Desktop scheduled tasks](https://code.claude.com/docs/en/desktop-scheduled-tasks), [commands](https://code.claude.com/docs/en/commands), [permissions](https://code.claude.com/docs/en/permissions), [Remote Control](https://code.claude.com/docs/en/remote-control), [headless mode](https://code.claude.com/docs/en/headless)
- OpenAI: [Codex CLI slash commands](https://learn.chatgpt.com/docs/cli/slash-commands), [agent approvals and security](https://learn.chatgpt.com/docs/agent-approvals-security), [configuration reference](https://learn.chatgpt.com/docs/config-file/config-reference), [rules](https://learn.chatgpt.com/docs/agent-configuration/rules)
- Apple Support: [If your external display is dark](https://support.apple.com/en-us/102501), [Mac laptop operating temperatures](https://support.apple.com/en-us/102336)

