Why your Mac stops long AI agent runs
Idle sleep, the lock screen, a closed lid, update restarts and a prompt nobody answers: the five ways a run dies, and the fix for each.
By Harsh Soni, maker of Latte · Updated
AI coding agents like Claude Code, Codex and Grok can work for an hour or more on their own. A MacBook isn't built for that: it's built to save power the moment you step away. Here are the five things that stop a long run, and how to deal with each.
1. Idle sleep
Leave the Mac alone for a few minutes and macOS sleeps it, even mid-task, unless something holds it awake. Claude Code does this for itself while it works on recent versions. For anything else, check what's holding the Mac awake right now:
pmset -g assertionsLook for a PreventUserIdleSystemSleep line from your agent or terminal. If there isn't one, wrap the command with caffeinate -i.
2. The lock screen
Locking the Mac on its own doesn't stop your agent, but it often comes with the screen going dark and the Mac drifting into sleep shortly after. As long as idle sleep is held off, work carries on behind the lock screen.
3. Closing the lid
This is the one that catches everybody. Closing the lid sleeps the Mac straight away, and keep-awake holds like caffeinate don't stop it. The exceptions are clamshell mode (power plus an external display) and the pmset disablesleep setting. The full list of options is in how to keep Claude Code running with the lid closed.
4. Overnight update restarts
macOS can install updates and restart overnight. That ends every session in your terminal. If you leave agents running overnight, either turn off automatic macOS updates while they run, or use a tool that defers them only during a run (Latte does this and puts your setting back afterwards).
5. A prompt nobody answers
The quiet one. The Mac is awake, the agent is fine, and it's been sitting on “Allow this command?” for an hour because you're at dinner. The fix is getting the prompt to your phone: see how to approve Claude Code prompts from your phone.
Once you fix all of this, the risk flips: a Mac that never sleeps, running hot in a bag with a draining battery. Whatever you use, make sure something lets the Mac sleep again when the work is done.
Summary
| What stops the run | Quick fix | With Latte |
|---|---|---|
| Idle sleep | caffeinate -i <command> | Held while an agent works |
| Lock screen | Hold off idle sleep | Held while an agent works |
| Closed lid | Clamshell mode or pmset disablesleep | Held, on battery too if you turn it on |
| Update restart | Turn off automatic updates | Deferred during a run, then restored |
| Unanswered prompt | Remote Control or a phone bridge | Prompt sent to Telegram or ntfy |
Common questions
- Why does my AI coding agent stop when I walk away from my Mac?
- Usually because the Mac went to sleep: idle sleep after a few minutes, a closed lid, or an overnight update restart. The other common cause is a permission prompt waiting for your answer.
- How do I see what is keeping my Mac awake?
- Run pmset -g assertions in Terminal. A PreventUserIdleSystemSleep entry means something is holding off idle sleep, and the line names the process.
- Will my agent's work be lost if the Mac sleeps?
- Usually not. The process is frozen and resumes on wake, but network requests in flight can fail or time out, which may end the run with an error.
Latte keeps Claude Code, Codex and Grok working with your MacBook closed, sends approvals to your phone, and lets the Mac sleep the moment the work is done.