Overnight
Run AI coding agents overnight on a MacBook
An overnight agent run needs an awake Mac, power, network and the right permissions. A MacBook can do it lid closed with a timed Keep Mac Awake session.
Short answer: An overnight run of Claude Code, the Codex CLI or another coding agent needs a Mac that stays awake, AC power, a network connection, permissions that let the agent work without you, and a way to check in. A desktop Mac such as a Mac mini has no lid, so its Energy settings can keep it awake; a MacBook sleeps when you close the lid. A MacBook you already own can do the job with the lid closed if something keeps it awake, for example a timed Keep Mac Awake session that the agent stops when it finishes.
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).
- 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) and account token usage in the Codex CLI (slash commands). - A way to check in. Claude Code Remote Control, Codex in the ChatGPT app, or SSH, so you can read progress and answer questions from your 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, as long as something keeps it awake. A desktop Mac, with no lid, is simpler to leave on.
On a desktop Mac, System Settings > Energy has "Prevent automatic sleeping when the display is off" (Apple Support). For Codex, OpenAI suggests "a dedicated always-on Mac or Windows PC when you want ChatGPT to stay reachable for longer-running work" (OpenAI).
| A MacBook you own | A desktop Mac such as a Mac mini | |
|---|---|---|
| Lid | Sleeps when closed unless something keeps it awake | No lid; Energy settings can prevent automatic sleep |
| Hardware | Nothing new to buy | A second Mac |
| Setup | Projects, keys and tools are already there | Install projects, credentials and tools again |
| Heat | Closed lid; needs a hard surface with airflow | Sits on a desk |
| Battery | Wears with use | None |
| Availability | Leaves with you when you take it | Stays home and reachable |
No honest number exists for a night's battery wear. Apple says Optimized Battery Charging reduces wear by "reducing the time that your Mac spends fully charged", and on macOS Tahoe 26.4 or later you can also set a charge limit (Apple Support). More in MacBook heat and battery with the lid closed.
How do I keep a MacBook awake overnight with the lid closed?
Use an external display, turn sleep off by hand with sudo pmset disablesleep 1, or start a Keep Mac Awake session. caffeinate, 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.
With the lid closed, a display setup also needs power, a keyboard and a mouse (Apple Support). pmset disablesleep stays on until you undo it (what pmset disablesleep does). Keep Mac Awake changes that same setting for one session, then puts it back. For Claude Code, see keep Claude Code running with the lid closed.
What is a safe checklist for an overnight run?
Plug in, give the Mac airflow, and start a session sized to the task that ends on its own.
- AC power. Keep Mac Awake runs on AC by default. If the plug comes out, the session waits until power is back, and it does not wake a Mac that has fallen asleep.
- 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).
- 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.
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 task in Manual mode that needs an unapproved tool "stalls until you approve it" (scheduled tasks).
- 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). - 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'snotifysetting 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 |
For example: "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), 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. 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, Keep Mac Awake (keepmacawake.com) keeps a MacBook with Apple silicon on macOS 26 or later awake for a timed session that you or your agent start, and restores normal sleep when it ends; the Mac still needs AC power, airflow and a working network connection.
It is free for 3 days (sessions up to 8 hours, no card), then 9 USD once per Mac plus tax. See how it can keep a MacBook awake with the lid closed, and how to steer AI agents from your phone while it runs.
FAQ
Can I run Claude Code overnight on a MacBook with the lid closed?
Yes, if the Mac stays awake, on power and online. Closing the lid normally puts it to sleep, so use an external display, pmset disablesleep or a timed Keep Mac Awake session.
Is it safe to leave a MacBook running overnight with the lid closed?
Keep it on AC power and a hard, ventilated surface, never in a bag or on bedding, and use a timed session so normal sleep returns on its own.
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.
Can Claude Code turn Keep Mac Awake off when it finishes?
Yes. An agent on the Mac, running as the logged-in user, can run keepmacawake stop --json as its last step.
Sources
- Claude Code: Desktop scheduled tasks, commands, permissions, Remote Control, headless mode
- OpenAI: remote connections, Codex CLI slash commands, agent approvals and security, configuration reference
- Apple Support: Set sleep and wake settings for your Mac
- Apple Support: If your external display is dark
- Apple Support: Mac laptop operating temperatures
- Apple Support: Optimized Battery Charging and Charge Limit
- Keep Mac Awake: command-line reference, agent guide