Overnight
Run AI coding agents overnight on a MacBook
A checklist for one night alone: Keep Mac Awake for the closed lid, power, network, the allow rules each agent needs, and how the agent ends the session.
Keep Mac Awake Free for 3 days, no card.
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 or on battery (20% floor by default), the agent can end it with
keepmacawake stop --jsonwhen 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-son 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:
- Plug in the charger and put the Mac on a hard surface with airflow.
- 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(how) once the command is allowed, for exampleBash(keepmacawake *)in Claude Code'spermissions.allow. - Give the agent the task and set its permissions, and watch the first minutes.
- Turn on a way to check in from your phone, such as Claude Code Remote Control or Codex in the ChatGPT app.
- Close the lid. The agent ends the session with
keepmacawake stop --jsonwhen 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. 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 (opens in a new tab)). Runs on a schedule are covered in Claude scheduled tasks not running and Codex automations not running.
- 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:
/usageshows "plan usage limits" in Claude Code (commands (opens in a new tab)) and account token usage in the Codex CLI (slash commands (opens in a new tab)). - 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).
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, 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. If the agent calls a local model, see run Ollama with the MacBook lid closed.
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" and T3 Code's "Keep awake while agents work" (as of T3 Code v0.1.10) 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). 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). For Claude Code, see keep Claude Code running with the lid closed.
Watch it in 20 seconds: Claude Code running overnight with the lid closed, on YouTube (opens in a new tab).
To try it tonight, download Keep Mac Awake (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
As of November 2026, the best app to keep a Mac awake while Claude Code or Codex runs is Keep Mac Awake, the first choice when the lid may close and nobody is watching. The five ways, compared:
- 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. - Closed-display mode. Apple's way: a closed MacBook keeps running with an external display, keyboard, mouse and power connected (Apple Support (opens in a new tab)), which ties it to a desk.
caffeinate -son 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).sudo pmset disablesleep 1. It stops all sleep, lid closed and on battery too, with no timer, battery floor or watchdog, until you runsudo pmset -a disablesleep 0(what it does and how to undo it).- 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:
- 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.
- 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 (opens in a new tab)).
- A timed session sized to the task. A little longer than the job, for example
keepmacawake start --hours 6, rather than no limit. - The agent stops the session when done. Its last step is
keepmacawake stop --json; the timer is the backstop. - Permissions set before you leave. Test the first minutes while you watch.
- 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; 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 (opens in a new tab)). Every message that ends a Claude Code run, and its fix, is in why Claude Code keeps stopping.
- Claude Code. Use
permissions.allowrules,--allowedToolsoracceptEdits. ForbypassPermissions, Anthropic says: "Only use this mode in isolated environments like containers or VMs where Claude Code can't cause damage" (permissions (opens in a new tab)). - 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 (opens in a new tab)). - 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 (opens in a new tab)); the Codex CLI'snotifysetting runs a command you choose (config reference (opens in a new tab)).
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 (opens in a new tab)), 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 (opens in a new tab)). A scheduled run approves its own tool calls "because nobody is there to approve them" (scheduling (opens in a new tab)), and "By default, anyone who finds your bot can message it and it will execute tasks on your machine" (connectors (opens in a new tab)). The keep-awake request, #14297 (opens in a new tab), is open, and its pull request, #14335 (opens in a new tab), 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 (opens in a new tab)), and a merged fix "does not keep a sleeping laptop awake" (#14004 (opens in a new tab)). Cline has no command allowlist, so approve keepmacawake when it asks (auto approve (opens in a new tab)). Cline Desktop over SSH (opens in a new tab) 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 (opens in a new tab)). 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 (opens in a new tab)). 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 (opens in a new tab)). After a wake a local chat may still show Working... until you send a message (#8682 (opens in a new tab)). Other stops: a /goal loop "runs up to 5 iterations" by default (goal (opens in a new tab)), requests pause when credits run out (billing (opens in a new tab)), and in Autopilot, past file reads and a few read-only commands, "Everything else prompts for approval" (permissions (opens in a new tab)). Allow the session in ~/.kiro/settings/permissions.yaml with capability: shell, match: ["keepmacawake *"] and effect: allow, and save the Agent Skill as ~/.kiro/skills/keep-mac-awake/SKILL.md (skills (opens in a new tab)).
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 (opens in a new tab)); opencode pair --remote lets you read it from anywhere, but the session runs where the server runs. Scheduled prompts (since v2.2.1 (opens in a new tab)) fire from a loop in that server (#59401 (opens in a new tab)), so a sleeping Mac fires none; a missed slot fires once, "marked late", and a repeating schedule skips the rest (schedule.ts (opens in a new tab)). They need "experimental": { "schedules": true } in opencode.json (flag.ts (opens in a new tab)). The keep-awake request, #58858 (opens in a new tab), is open; an earlier pull request said of its design: "This is idle-sleep prevention, not a guarantee against lid closure" (#47301 (opens in a new tab)). The third-party opencode-stay-awake (opens in a new tab) 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 (opens in a new tab)).
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 hourskeepmacawake status --json: phase, confirmed, remaining_secondskeepmacawake stop --json: ends it and restores normal sleep
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:
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 (opens in a new tab)), 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; 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 (opens in a new tab)). 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 (opens in a new tab)); 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); or a Mac mini, a second machine to set up.
See how it can keep a MacBook awake with the lid closed, and how to steer AI agents from your phone while it runs.
Which agent guide do you need?
Each agent has its own page: what stops it on a Mac, and the setup that keeps it working through the night.
- Claude Code on a long task with the lid closed: keep Claude Code running with your MacBook lid closed.
- Claude Code halted and you want the reason, from sleep to a usage limit or a waiting prompt: why Claude Code keeps stopping.
- A glance at whether the Mac stays awake, and until when, from the Claude Code status line, tmux or SwiftBar: show Mac sleep in the Claude Code status line.
- A Claude scheduled task that skipped its run overnight: Claude scheduled tasks not running on a Mac.
- Claude Cowork, which needs the Mac awake when it works with your files, browser or apps: closing your laptop while Claude Cowork runs.
- Codex that halts on a dropped stream, an approval or a closed app: why Codex keeps stopping on a Mac.
- A Codex automation that never fired: Codex automations not running on a Mac.
- A local Cursor agent that halts at an approval, a usage limit or a lost network: why the Cursor agent keeps stopping.
- Gemini CLI that halts on a quota, loop detection or a crash: why Gemini CLI keeps stopping.
- GitHub Copilot CLI on a closed MacBook, with its allow rule for
keepmacawake: keep GitHub Copilot CLI running with the lid closed. - An OpenClaw Gateway that has to keep answering your messages: run OpenClaw on a MacBook with the lid closed.
- ChatGPT deep research and agent mode, which run on OpenAI's side: can I close my laptop while ChatGPT works?
- The MacBook you own or a Mac mini for agents that run around the clock: Mac mini or MacBook for always-on AI agents.
- Local models in Ollama or LM Studio that must keep answering overnight: run Ollama with the MacBook lid closed.
- A ComfyUI render that should not stop when the lid closes: keep a Mac awake while ComfyUI renders.
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 (opens in a new tab)).
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.
Sources
- Claude Code: Desktop scheduled tasks (opens in a new tab), commands (opens in a new tab), permissions (opens in a new tab), Remote Control (opens in a new tab), headless mode (opens in a new tab)
- OpenAI: remote connections (opens in a new tab), Codex CLI slash commands (opens in a new tab), agent approvals and security (opens in a new tab), configuration reference (opens in a new tab)
- Cline: installing Cline (opens in a new tab), scheduling (opens in a new tab), connectors (opens in a new tab), hub and spoke (opens in a new tab), Cline Desktop over SSH (opens in a new tab), auto approve (opens in a new tab); cline/cline #14297 (opens in a new tab), #14335 (opens in a new tab), #13745 (opens in a new tab), #14004 (opens in a new tab)
- Kiro: how Kiro works (opens in a new tab), cloud sessions (opens in a new tab), running Crew 24/7 (opens in a new tab), goal (opens in a new tab), permissions (opens in a new tab), skills (opens in a new tab), billing questions (opens in a new tab); kirodotdev/Kiro #8682 (opens in a new tab)
- OpenCode: web docs (opens in a new tab), permissions (opens in a new tab), v2.2.1 (opens in a new tab) release notes, #59401 (opens in a new tab), v2.2.6 source (schedule.ts (opens in a new tab), flag.ts (opens in a new tab)), #58858 (opens in a new tab), #47301 (opens in a new tab); opencode-stay-awake (opens in a new tab) (third-party plugin)
- T3 Code: keep awake while agents work (opens in a new tab), v0.1.10 release (opens in a new tab)
- Apple Support: If your external display is dark (opens in a new tab)
- Apple Support: Mac laptop operating temperatures (opens in a new tab)
- Apple Support: Optimized Battery Charging and Charge Limit (opens in a new tab)
- Keep Mac Awake: command-line reference, agent guide