← All posts

Keep Claude Code Running on Mac: Long Session Guide

Keep Claude Code running on Mac through long agent sessions: caffeinate flags, sleep settings, lid-closed fixes, tmux, and how to resume a stopped run.

To keep Claude Code running on a Mac for a long agent session, do three things: stop the Mac from sleeping (wrap the session in caffeinate -i, or change your sleep settings), stop the terminal from being the weak link (run the session inside tmux so closing a window does not kill it), and make the session easy to pick back up (claude --continue or /resume). Lid-closed laptops are a separate problem that caffeinate does not solve. The rest of this post covers each piece and the traps that cut overnight runs short.

Why long Claude Code sessions die on a Mac

Long agent runs usually fail for boring reasons:

  • Idle sleep. macOS decides you are away because nobody has touched the keyboard, and the system sleeps. Network connections drop and the API request in flight fails.
  • Lid close. On a MacBook, closing the lid sleeps the machine unless you are in clamshell mode with power and an external display.
  • Battery. Some sleep assertions only count on AC power, so the same command behaves differently on battery.
  • The terminal window. If Claude Code is a child of a terminal tab and that tab closes, the process goes with it.
  • Usage limits. On a Pro or Max plan, a long session can hit the 5-hour or weekly window and stop until it resets.

Fix them in that order.

Option comparison
Method Stops idle sleep Survives lid close Survives closing the terminal Ends on its own
caffeinate -i claude Yes No No Yes, when Claude Code exits
caffeinate -i inside tmux Yes No Yes Yes
System Settings sleep options Yes (on power) No No No, you must undo it
Clamshell mode (power + external display) Yes Yes No No
sudo pmset -a disablesleep 1 Yes Yes No No, and easy to forget
Use caffeinate the right way

caffeinate ships with macOS and needs no install. It creates a power assertion that tells the system not to sleep while it runs. The cleanest pattern is to wrap Claude Code itself:

caffeinate -i claude

The assertion lasts exactly as long as the claude process, then normal sleep resumes.

The flags that matter:

  • -i prevents idle system sleep. This is the one you want for agent runs.
  • -d keeps the display awake too, only useful if you want to watch.
  • -s prevents system sleep, but only while the Mac is on AC power. On battery it does nothing, which surprises people.
  • -w <pid> holds the assertion until a given process exits, handy if Claude Code is already running.

To attach to a session that is already going, find the PID and wait on it:

caffeinate -i -w $(pgrep -n claude)

To confirm it is working, run pmset -g assertions and look for a PreventUserIdleSystemSleep entry from the caffeinate tool.

A note on Claude Code's own behavior: users have reported in the Claude Code GitHub issues that recent macOS builds spawn a short caffeinate -i -t 300 of their own while working, and refresh it. It is not documented, there are open reports of gaps in that relay, and it does nothing for lid close. Treat it as a bonus, not a plan, and wrap long runs yourself.

Sleep settings for unattended runs

If you run agents most nights, a setting is less fragile than remembering a command. On a MacBook, open System Settings, then Battery, then Options, and turn on the option to prevent automatic sleeping on the power adapter when the display is off. On a desktop Mac the equivalent lives under Energy. The display can still turn off, which saves the panel, while the system keeps working.

The tradeoff: it applies all the time until you change it back.

The lid-closed problem

No caffeinate flag keeps a MacBook awake with the lid shut. You have two honest choices:

  1. Clamshell mode. Connect power and an external display (plus a keyboard or mouse), then close the lid. macOS treats this as a supported desktop setup.
  2. sudo pmset -a disablesleep 1. This disables sleep entirely, lid included, so a laptop in a bag stays awake and warm. Set it back with sudo pmset -a disablesleep 0 the moment the run finishes.

If you need truly unattended runs, a desktop Mac or a remote machine is the better tool. Our guide to running Claude Code over SSH covers that route.

Survive a closed terminal with tmux

A sleep assertion does not help if the process dies when you close a tab. Run long sessions inside tmux so they are detached from any one window:

tmux new -s agent
caffeinate -i claude

Detach with Ctrl-b d, close the terminal, and reattach later with tmux attach -t agent. The Claude Code docs recommend adding set -g allow-passthrough on and the extended-keys lines to ~/.tmux.conf so notifications and Shift+Enter still work inside tmux. Our tmux power-user setup walks through a full config.

Pick up where you left off

Sessions still end: a reboot, a crash, a usage window. Claude Code keeps session history locally:

  • claude --continue reopens the most recent conversation in the current directory.
  • claude --resume or /resume lets you pick an older session.
  • /rename before you stop makes the session easy to find later.

For plan limits, recent Claude Code versions can wait for a usage limit to reset and continue the interrupted task, either automatically or from /rate-limit-options. Check /usage before starting a big overnight job so you know how much of the 5-hour and weekly windows you have left.

Know when it needs you

The other half of a long session is noticing when it stops. Claude Code fires a notification when it finishes or pauses for a permission prompt. By default it sends a desktop notification in Ghostty, Kitty, and iTerm2; in other terminals you can set preferredNotifChannel to "terminal_bell" or add a Notification hook. Our Claude Code notifications guide has working hook examples.

If you run several agents at once, this is where the terminal you choose starts to matter. MOLTamp's tab system holds up to 50 tabs and puts an attention badge on any tab whose agent session needs you or has finished, so a stalled run in tab 7 does not sit unnoticed until morning. It also has session monitoring and a customizable status line. See running multiple Claude Code sessions for the workflow, and the best terminal for Claude Code round-up for how it compares.

Verdict
  • For a single long run: caffeinate -i claude inside tmux. It cleans up after itself.
  • For nightly runs on a desktop Mac: change the sleep setting once and skip the command.
  • For a closed MacBook: clamshell mode, or pmset disablesleep with a reminder to undo it.
  • For many sessions at once: a terminal that tells you which one needs attention. That is the job MOLTamp was built for, and it is free to use.
FAQ

Does Claude Code keep running when my Mac sleeps? No. When the system sleeps, the network drops and the request in flight fails. History is saved, so you can resume, but work stops until the Mac wakes. Use caffeinate -i for long runs.

What is the best caffeinate command for Claude Code? caffeinate -i claude. It prevents idle sleep for exactly as long as Claude Code runs. Add -d only if you want the display to stay on. Avoid relying on -s on a laptop, since it only applies on AC power.

Can I close my MacBook lid while Claude Code runs? Only in clamshell mode with power and an external display, or with sudo pmset -a disablesleep 1. caffeinate cannot keep a MacBook awake with the lid closed. If you use pmset, set it back to 0 when you finish.

How do I resume a Claude Code session after it stops? Run claude --continue in the same project directory to reopen the latest conversation, or claude --resume to choose from older ones. Inside a running session, /resume does the same.