# Run AI coding agents overnight on a MacBook

*This guide was written with the help of an AI writing program and checked against the sources listed at the end.*

*It first appeared on* [*keepmacawake.com*](http://keepmacawake.com) *as* [*Run AI coding agents overnight on a MacBook*](https://keepmacawake.com/blog/run-ai-agents-overnight-macbook?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook)*.*

> **Short answer:** To run Claude Code, the Codex CLI, Cline, Kiro, OpenCode 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 by default, the agent can end it with `keepmacawake stop --json` when the job is done, and a separate watchdog restores normal sleep if a 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.

## 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. As of October 2026, 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)). Runs on a schedule are covered in [Claude scheduled tasks not running](https://keepmacawake.com/blog/claude-scheduled-tasks-not-running?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook) and [Codex automations not running](https://keepmacawake.com/blog/codex-automations-not-running?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook).
- **A network connection.** The agent sends every turn to its model over the internet. Keep Mac Awake does not keep Wi-Fi alive or reconnect it.
- **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 such as T3 Code, Happy or Conductor, 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=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook#which-apps-steer-several-agents-on-your-mac-from-a-phone)).

## Do I need a Mac mini for Claude Code, or will a MacBook do?

You do not need a Mac mini: a MacBook you already own can run agents overnight with the lid closed, and the first choice to keep it awake is a timed Keep Mac Awake session. A desktop Mac has no lid and can prevent automatic sleep in System Settings > Energy, but it is a second Mac, with your projects, keys and tools to set up again. The full comparison, with power cuts, restarts and battery wear, is in [Mac mini or MacBook for always-on AI agents](https://keepmacawake.com/blog/mac-mini-or-macbook-for-ai-agents?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook), and a MacBook kept closed as a home server for files, backups or remote logins is in [use an old MacBook as a server with the lid closed](https://keepmacawake.com/blog/old-macbook-as-server-lid-closed?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook). If the agent calls a local model, see [run Ollama with the MacBook lid closed](https://keepmacawake.com/blog/ollama-lm-studio-lid-closed?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook).

## 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, and the same holds for the keep-awake settings proposed for Cline and OpenCode ([below](https://keepmacawake.com/blog/run-ai-agents-overnight-macbook?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook#how-do-i-keep-cline-kiro-or-opencode-running-with-the-lid-closed)). 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 Power Nap and Wake for network access do not keep agents running](https://keepmacawake.com/blog/power-nap-wake-for-network-access?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook)). 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=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook).

Watch it in 20 seconds: [Claude Code running overnight with the lid closed, on YouTube](https://www.youtube.com/watch?v=CQI4mcasxQM).

## 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](https://keepmacawake.com/blog/run-ai-agents-overnight-macbook?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook#how-do-i-give-an-agent-the-right-permissions-for-a-night-alone) the task needs. The checklist:

1. **AC power.** If the plug comes out, 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/keep-macbook-awake-lid-closed-on-battery?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook).
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=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook).

> **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; as of November 2026 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)). Every message that ends a Claude Code run, and its fix, is in [why Claude Code keeps stopping](https://keepmacawake.com/blog/claude-code-keeps-stopping?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook).

- **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 do I keep Cline, Kiro or OpenCode running with the lid closed?

Like the agents above: each runs on your Mac, none has a setting that keeps it awake through the lid as of November 2026, and the first choice is a Keep Mac Awake session started before you close it. What differs is where each runs, what it schedules and how it may start the session itself.

### Cline

Cline Desktop, the editor extensions and the CLI run the agent on your Mac ([installing Cline](https://docs.cline.bot/getting-started/installing-cline)), and so do the CLI's scheduled agents and its Telegram, Slack or WhatsApp connectors, through a hub that is a "Singleton daemon per machine" ([hub and spoke](https://docs.cline.bot/sdk/architecture/hub-spoke)). A scheduled run approves its own tool calls "because nobody is there to approve them" ([scheduling](https://docs.cline.bot/cli/scheduling)), and "By default, anyone who finds your bot can message it and it will execute tasks on your machine" ([connectors](https://docs.cline.bot/cli/connectors)). The keep-awake request, [#14297](https://github.com/cline/cline/issues/14297), is open, and its pull request, [#14335](https://github.com/cline/cline/pull/14335), would hold an idle-sleep assertion, which the lid overrides. One user slept the Mac mid-run and "came back to ~4 sessions all executing the same task" ([#13745](https://github.com/cline/cline/pull/13745)), and a merged fix "does not keep a sleeping laptop awake" ([#14004](https://github.com/cline/cline/pull/14004)). Cline has no command allowlist, so approve `keepmacawake` when it asks ([auto approve](https://docs.cline.bot/features/auto-approve)). [Cline Desktop over SSH](https://docs.cline.bot/usage/cline-desktop-ssh) runs sessions on another host, while "the chat, approvals, and live output stay in the app on your computer".

### Kiro

Kiro IDE and Kiro CLI "run the harness on your machine by default" ([how Kiro works](https://kiro.dev/docs/how-kiro-works)). Kiro Web and Mobile run it in a cloud sandbox that keeps going while the Mac sleeps, on a clone where "your local working copy is never uploaded", on a Pro plan or higher ([cloud sessions](https://kiro.dev/docs/cloud-sessions)). Kiro Crew's docs suggest an always-on host "so Slack, cron, and the task runner keep working while your laptop sleeps" ([running 24/7](https://kiro.dev/docs/crew/running-24-7)). After a wake a local chat may still show *Working...* until you send a message ([#8682](https://github.com/kirodotdev/Kiro/issues/8682)). Other stops: a `/goal` loop "runs up to 5 iterations" by default ([goal](https://kiro.dev/docs/cli/chat/goal)), requests pause when credits run out ([billing](https://kiro.dev/docs/billing/related-questions)), and in Autopilot, past file reads and a few read-only commands, "Everything else prompts for approval" ([permissions](https://kiro.dev/docs/permissions)). Allow the session in `~/.kiro/settings/permissions.yaml` with `capability: shell`, `match: ["keepmacawake *"]` and `effect: allow`, and save the [Agent Skill](https://keepmacawake.com/skill/keep-mac-awake/SKILL.md?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook) as `~/.kiro/skills/keep-mac-awake/SKILL.md` ([skills](https://kiro.dev/docs/skills)).

### OpenCode

OpenCode's terminal interface, web UI and apps talk to one server on your Mac, which listens "only on localhost" by default ([web docs](https://opencode.ai/docs/cli/web/)); `opencode pair --remote` lets you read it from anywhere, but the session runs where the server runs. Scheduled prompts (since [v2.2.1](https://github.com/anomalyco/opencode/releases/tag/v2.2.1)) fire from a loop in that server ([#59401](https://github.com/anomalyco/opencode/pull/59401)), so a sleeping Mac fires none; a missed slot fires once, "marked late", and a repeating schedule skips the rest ([schedule.ts](https://github.com/anomalyco/opencode/blob/v2.2.6/packages/core/src/schedule.ts)). They need `"experimental": { "schedules": true }` in `opencode.json` ([flag.ts](https://github.com/anomalyco/opencode/blob/v2.2.6/packages/core/src/flag.ts)). The keep-awake request, [#58858](https://github.com/anomalyco/opencode/issues/58858), is open; an earlier pull request said of its design: "This is idle-sleep prevention, not a guarantee against lid closure" ([#47301](https://github.com/anomalyco/opencode/pull/47301)). The third-party [opencode-stay-awake](https://github.com/AuroraAeon/opencode-stay-awake) plugin runs `caffeinate -dims -w <server pid>` while OpenCode works, by its README, which holds a closed MacBook only on AC power and only while a session generates; we have not tested it. Allow the session with `{ "action": "shell", "resource": "keepmacawake *", "effect": "allow" }` under `permissions` in `opencode.jsonc` ([permissions](https://opencode.ai/docs/permissions/)).

## 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

```sh
keepmacawake start --hours 8 --json
keepmacawake status --json
keepmacawake stop --json
```

| 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` |

For example, ask the agent:

```text
Start an 8-hour session with keepmacawake start --hours 8 --json,
run the test suite and fix the failures on a new branch,
then run keepmacawake stop --json and summarize.
```

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`; use over SSH has not been verified yet. 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=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook) or load the [Agent Skill](https://keepmacawake.com/skill/keep-mac-awake/SKILL.md?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook).

## 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](http://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 AC power, 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 ([what pmset disablesleep does](https://keepmacawake.com/blog/pmset-disablesleep?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook)); or a Mac mini, a second machine to set up.

See how it can [keep a MacBook awake with the lid closed](https://keepmacawake.com/?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook), and how to [steer AI agents from your phone](https://keepmacawake.com/blog/steer-ai-agents-from-your-phone?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook) while it runs.

## FAQ

### Will running agents overnight wear out my MacBook battery?

There is no honest figure for one night. Apple's Optimized Battery Charging and Charge Limit reduce the time a Mac spends fully charged, which Apple says reduces wear ([Apple Support](https://support.apple.com/en-us/102338)).

### Can I run Gemini CLI or Antigravity CLI overnight with the lid closed?

Yes, the same way: the agent runs on the Mac, so the Mac has to stay awake, on power and online. As of October 2026, Google moved free and Google AI Pro and Ultra users from Gemini CLI to Antigravity CLI on June 18, 2026, and Antigravity CLI has Remote Control for checking in from a phone. Start a timed Keep Mac Awake session before you close the lid; the details are in [Antigravity CLI and Gemini CLI remote control with the lid closed](https://keepmacawake.com/blog/antigravity-remote-control-lid-closed?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook).

## 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: [remote connections](https://learn.chatgpt.com/docs/remote-connections), [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)
- Cline: [installing Cline](https://docs.cline.bot/getting-started/installing-cline), [scheduling](https://docs.cline.bot/cli/scheduling), [connectors](https://docs.cline.bot/cli/connectors), [hub and spoke](https://docs.cline.bot/sdk/architecture/hub-spoke), [Cline Desktop over SSH](https://docs.cline.bot/usage/cline-desktop-ssh), [auto approve](https://docs.cline.bot/features/auto-approve); cline/cline [#14297](https://github.com/cline/cline/issues/14297), [#14335](https://github.com/cline/cline/pull/14335), [#13745](https://github.com/cline/cline/pull/13745), [#14004](https://github.com/cline/cline/pull/14004)
- Kiro: [how Kiro works](https://kiro.dev/docs/how-kiro-works), [cloud sessions](https://kiro.dev/docs/cloud-sessions), [running Crew 24/7](https://kiro.dev/docs/crew/running-24-7), [goal](https://kiro.dev/docs/cli/chat/goal), [permissions](https://kiro.dev/docs/permissions), [skills](https://kiro.dev/docs/skills), [billing questions](https://kiro.dev/docs/billing/related-questions); [kirodotdev/Kiro #8682](https://github.com/kirodotdev/Kiro/issues/8682)
- OpenCode: [web docs](https://opencode.ai/docs/cli/web/), [permissions](https://opencode.ai/docs/permissions/), [v2.2.1](https://github.com/anomalyco/opencode/releases/tag/v2.2.1) release notes, [#59401](https://github.com/anomalyco/opencode/pull/59401), v2.2.6 source ([schedule.ts](https://github.com/anomalyco/opencode/blob/v2.2.6/packages/core/src/schedule.ts), [flag.ts](https://github.com/anomalyco/opencode/blob/v2.2.6/packages/core/src/flag.ts)), [#58858](https://github.com/anomalyco/opencode/issues/58858), [#47301](https://github.com/anomalyco/opencode/pull/47301); [opencode-stay-awake](https://github.com/AuroraAeon/opencode-stay-awake) (third-party plugin)
- [Apple Support: If your external display is dark](https://support.apple.com/en-us/102501)
- [Apple Support: Mac laptop operating temperatures](https://support.apple.com/en-us/102336)
- [Apple Support: Optimized Battery Charging and Charge Limit](https://support.apple.com/en-us/102338)
- Keep Mac Awake: [command-line reference](https://keepmacawake.com/command-line.md?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook), [agent guide](https://keepmacawake.com/agents.md?utm_source=hashnode&utm_medium=referral&utm_campaign=syndication&utm_content=run-ai-agents-overnight-macbook)
