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: /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, 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 ownA desktop Mac such as a Mac mini
LidSleeps when closed unless something keeps it awakeNo lid; Energy settings can prevent automatic sleep
HardwareNothing new to buyA second Mac
SetupProjects, keys and tools are already thereInstall projects, credentials and tools again
HeatClosed lid; needs a hard surface with airflowSits on a desk
BatteryWears with useNone
AvailabilityLeaves with you when you take itStays 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.

  1. 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.
  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.

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.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 codeMeaningWhat the agent should do
0SuccessContinue
1The request failedRead error.message; retry if error.retryable
2Invalid argumentsFix the command
4Needs the user: AC power or a purchase (purchase_required)Stop and tell you
5A session already runs, or none to stopRun status
6Not installed or service unavailableAsk you to install it
7No answer in timeRun 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