How to keep your Mac awake while Claude Code and Codex run
· By Mert Düzgün
From Agent Caffeine, the Mac app that keeps your Mac awake while AI coding agents work, also with the lid closed.
Agent Caffeine is a Mac app that keeps your Mac awake while Claude Code, Codex and other AI coding agents work, also with the lid closed, and lets it sleep when they are done. This guide shows that way and the free built-in ways, so you can pick the one that fits your run.
The short answer: for one session at your desk, start the agent as caffeinate -i claude (or caffeinate -i codex). For a long or overnight run, plug the Mac in and hold the closed lid too, with caffeinate -is on power or with an app that knows when the agent is done.
Why the Mac sleeps in the middle of a task
An agent can work for an hour without any input from you. macOS does not see that work as activity, so three things can stop it:
- Idle sleep. After the timers in System Settings run out, the Mac sleeps, and the agent stops where it is.
- A closed lid. Closing a MacBook's lid is its own sleep. The idle timers and most
caffeinateflags do not stop it. - A low battery. On battery, a long run can empty it, and the Mac sleeps hard.
Claude Code tries to help with the first one. On our Mac, Claude Code sessions (version 2.1.312) started caffeinate -i -t 300 as a child process and renewed it, so they blocked idle sleep five minutes at a time. Not every open session had one, and it does nothing for a closed lid. We did not see this in Codex. Check what holds your Mac awake with:
pmset -g assertions
Way 1: caffeinate, built into macOS
caffeinate comes with every Mac. Put it in front of the agent:
caffeinate -i claude
caffeinate -i codex
The Mac stays awake while the session is open, and sleeps as usual when you quit the agent. Our guide to the caffeinate command has every flag.
To also hold a closed lid, add -s. It works only on the power adapter:
caffeinate -is claude
An agent that already runs can be covered too. Give caffeinate its process ID:
caffeinate -i -w "$(pgrep -n -x codex)"
For an overnight run, start it inside tmux, so closing a terminal window does not end it:
tmux new-session -d -s agent 'caffeinate -is claude'
The limits: the Mac stays awake for as long as the session is open, also while the agent sits idle or waits for your answer; you must remember it for each session; and on battery no flag holds a closed lid.
Way 2: Agent Caffeine, in the menu bar
Agent Caffeine finds the agents on your Mac by itself, so there is nothing to type per session.
- It detects more than 40 coding agents. Claude Code, Codex and Grok Build report their own state, working, waiting for you or idle, and Agent Caffeine reads it.
- It keeps the Mac awake while an agent works, for 5 minutes when one waits for your answer, and then lets the Mac sleep.
- A closed lid works on the power adapter from the start. On battery it needs a small helper that you allow once in System Settings.
- It lets the Mac sleep when the battery reaches 20% (you can change it) or the Mac gets too hot, and says why in a notification.
- It can notify you when an agent waits for your answer or finishes.
Install it with Homebrew, or download it from caffeinagent.com:
brew install --cask vinenastudio/tap/agent-caffeine
Way 3: the agent-caffeine command, for builds and scripts
A plain build, a render or a test run is not an agent, so the app cannot see it. The agent-caffeine command holds the Mac awake for exactly that command, and ends with its exit code:
agent-caffeine hold -- npm run build
agent-caffeine hold --reason "Release build" -- xcodebuild -scheme App archive
Or hold for a time, and release when the work is done:
agent-caffeine hold --for 2h --reason "Overnight eval"
agent-caffeine release
"Set up for agents" in Settings links the command as ~/.local/bin/agent-caffeine and adds a skill to your agents, so Claude Code or Codex puts agent-caffeine hold -- in front of a long command by itself. More in keep your Mac awake while a command runs.
Way 4: the MCP server, so the agent asks for itself
agent-caffeine mcp is an MCP server on stdio with three tools: hold_awake (a number of minutes, 1 to 720, and a reason), release and status. A hold made through it ends when the agent's session ends, so nothing is left over.
Add it to Claude Code:
claude mcp add --scope user agent-caffeine -- "/Applications/Agent Caffeine.app/Contents/MacOS/agent-caffeine" mcp
Add it to Codex in ~/.codex/config.toml:
[mcp_servers.agent-caffeine]
command = "/Applications/Agent Caffeine.app/Contents/MacOS/agent-caffeine"
args = ["mcp"]
Settings > Agents > MCP server in the app adds it to other AI apps.
With the lid closed
| Case | Built in | Agent Caffeine |
|---|---|---|
| Lid open | caffeinate -i |
Works |
| Lid closed, on power | caffeinate -s |
Works from the start |
| Lid closed, on battery | sudo pmset -a disablesleep 1, which you must turn off yourself |
Works with the helper, allowed once |
| External display, keyboard and mouse | Closed-display mode | Works |
On battery, Agent Caffeine's helper turns on the SleepDisabled setting of macOS only while it holds the Mac awake, and turns it off again after. Our guide on keeping a Mac awake with the lid closed explains each built-in method with a test.
A closed laptop that stays awake keeps using power, and in a bag it has little air. Do not put an awake laptop in a bag and forget it.
A checklist for an overnight agent run
- Plug the Mac in.
- Start the agent under
caffeinate -is, inside tmux, or let Agent Caffeine hold the Mac awake. - If you want to reach the agent from your phone (Claude Code Remote Control, Codex remote), keep the Mac awake also while the agent waits: Agent Caffeine's "Stay reachable" keeps it awake for 1 to 8 hours or 1 day. A message cannot wake a Mac that sleeps.
- Check with
pmset -g assertionsthat something holds the Mac awake before you leave.
Which one to use
| Your run | Use |
|---|---|
| One session at your desk, lid open | caffeinate -i claude |
| Lid closed, on power, you remember to start it | caffeinate -is claude |
| Many sessions, lid closed on battery, or you want the Mac to sleep when the agents are done | Agent Caffeine |
| A long build or script, also from an agent | agent-caffeine hold -- <command> |
| The agent should ask by itself | The MCP server or the skill |
Agent Caffeine has a 7-day free trial, then costs $9 once, and needs macOS 13 or later. If caffeinate covers your run, use it.
Questions
Does Claude Code keep my Mac awake by itself?
Partly. In our test with Claude Code 2.1.312 on macOS 26, sessions ran caffeinate -i -t 300 and renewed it, which blocks idle sleep five minutes at a time. Not every open session had one, and it does not keep a closed MacBook awake. Do not count on it for a long unattended run.
Can I close the MacBook lid while Claude Code runs?
Only if something holds the closed lid. On the power adapter, caffeinate -s does it. On battery, no caffeinate flag does; you need sudo pmset -a disablesleep 1, which you must turn off yourself, or an app such as Agent Caffeine with its helper.
How do I keep my Mac awake for an overnight agent run?
Plug the Mac in, then start the agent under caffeinate -is, inside tmux so a closed window does not end it. Or let Agent Caffeine hold the Mac awake while the agent works and let it sleep when the agent is done.
Is there an MCP server that keeps a Mac awake?
Yes. Agent Caffeine has one: agent-caffeine mcp runs an MCP server on stdio with the tools hold_awake, release and status. A hold that an agent makes through it ends when the agent's session ends.
Does keeping the Mac awake drain the battery?
Yes, an awake Mac uses more power than a sleeping one, and a laptop in a bag gets warm. Keep it awake only while the agent works, plug it in for long runs, and use a tool that stops at a low battery.
Sources
man caffeinate(caffeinate(8), macOS) andman pmset(pmset(1), macOS)- Our tests on macOS 26.7.1, October 29, 2026: the
caffeinatechild process of Claude Code 2.1.312, read withpmset -g assertions - Apple Support: If your external display is dark or low resolution (closed-display mode requirements)
- Codex: MCP configuration (the
mcp_serverstable inconfig.toml)