Codex or another AI coding agent stops when you close the MacBook lid because closing the lid puts the Mac to sleep, and a local task cannot continue on a sleeping computer. What helps: keep the lid open or slightly open, block lid-close sleep with a tool, or run the task in Codex cloud.
A MacBook without an external display always sleeps when the lid closes. No normal setting prevents that, and neither do caffeinate or the app setting "Prevent sleep while running". Only a system change can, such as the closed-lid option in Amphetamine or sudo pmset -a disablesleep 1.
What are the options?
-
Lid open, idle sleep covered. Start the CLI with
caffeinate -i codex, or use the desktop app setting described below. - Lid 12 mm open with a lid spacer. WakeClip, a small clip for the MacBook lid, stops the lid before it closes. Nothing to install.
- Amphetamine and similar apps. They keep the Mac awake. Some can also prevent sleep with the lid closed, and the lid is then fully shut.
-
sudo pmset -a disablesleep 1. Turns sleep off system-wide, including when the lid closes. Needs admin rights and must be reset withdisablesleep 0. - External display or HDMI dummy plug. Closed, on power, with keyboard and mouse. A dummy plug imitates a display.
- Codex cloud or a second computer. The task does not run on the MacBook.
Where is your Codex task running?
The model name does not tell you. Ask which computer changes the files, runs the commands and runs the tests.
- On this MacBook. You work in a local project through the app, the Terminal or an editor. The task depends on this Mac staying awake.
- In Codex cloud. OpenAI provides separate environments for this. A task started there does not need your Mac as its host. See the Codex cloud documentation.
- On another computer. Check that the commands really run there and what happens to the session when your MacBook disconnects.
For a cloud task you can close the MacBook normally. Everything below is about local tasks.
How do you cover idle sleep for Codex?
Idle sleep is the sleep that starts after a period without input. It is separate from lid-close sleep, and software handles it well.
In the Terminal, start Codex like this:
caffeinate -i codex
caffeinate ships with macOS. With -i it prevents idle sleep for as long as the command after it runs. When you quit Codex, normal behaviour returns by itself.
In OpenAI's desktop app (the documentation calls it the ChatGPT desktop app), open the settings and look under General for "Prevent sleep while running". OpenAI documents this option in the app settings.
Both cover idle sleep only. Neither keeps the Mac awake once the lid is shut.
What keeps Codex running when you lower the lid?
You either stop the lid from closing or switch off lid-close sleep in the system.
A lid spacer
WakeClip keeps the lid 12 mm open. Your MacBook does not go to sleep from closing the lid, and whatever is running keeps running.
The clip slides onto the front edge of the case, to the left of the trackpad, and the lid rests on it. No installation, no admin rights, no Terminal command, and nothing to reset later. That also works on a company Mac where you may not install anything. Unlike a fully closed lid, the gap leaves air above the keyboard, so heat can escape.
Amphetamine or pmset
If the lid has to be fully shut, use an app such as Amphetamine with its closed-lid option, or sudo pmset -a disablesleep 1. With pmset, set it back to 0 when the task is done. Otherwise the Mac stays awake in your bag as well.
External display
At a fixed desk, the setup Apple intends is a closed MacBook on power with an external display, keyboard and mouse.
What happens when you leave Wi-Fi range?
If you carry the MacBook out of Wi-Fi range, it loses the connection. That can interrupt a Codex task whether the Mac is awake or not, because a local task still sends requests to the model over the network.
No keep-awake method changes this, including a clip. If you carry the MacBook between rooms, stay within reach of the same network. If you have to leave it, expect the task to pause or fail and check its state when you are back online.
When does WakeClip fit, and when does something else fit better?
WakeClip fits when the task needs this MacBook and you want to lower the lid: to carry it in your hand to another room, to keep it from standing open on the desk, or because you close the lid out of habit and do not want that to end the run. Hold the MacBook by the base and do not press on the lid.
Something else fits better in these cases:
- Transport in a bag. WakeClip is made for carrying in your hand, not for a bag. Remove the clip and close the MacBook normally before you stow it. A working MacBook does not belong in a bag or sleeve.
- Unattended overnight work. Codex cloud, a server or a desktop computer is the better host.
-
A fully closed lid. Choose Amphetamine,
pmsetor an external display.
Why does Codex stop even with the lid open?
Then the lid is not the cause. Check whether Codex is waiting for an approval or an answer, whether it shows a connection error, and whether the MacBook ran out of battery. The guide MacBook sleeps despite keep-awake settings lists the causes in order. If you use Claude Code as well, see Claude Code stops when you close your MacBook. All ways side by side are in Keep a MacBook awake with the lid closed: 7 ways compared.
Short answer
For a local Codex task, cover idle sleep with caffeinate -i codex or the app setting, and either keep the lid from closing or switch off lid-close sleep, for example with Amphetamine or pmset. If a 12 mm gap suits your way of working, pick the WakeClip version for your MacBook model.
WakeClip is an independent accessory and is not affiliated with Apple or OpenAI.
