OpenCode

How to keep OpenCode running with the MacBook lid closed

OpenCode stops when the Mac sleeps, and closing the lid sleeps a MacBook. Keep Mac Awake keeps it working with the lid closed and no external display.

Short answer: OpenCode runs its sessions in a server on your Mac, so a task stops when the Mac sleeps, and closing a MacBook's lid puts it to sleep. As of November 2026, OpenCode's docs describe no setting that keeps the computer awake, and a feature request for one is open. To keep OpenCode working with the lid closed, the first choice is a Keep Mac Awake session such as keepmacawake start --hours 8: it keeps a MacBook with Apple silicon on macOS 26 or later awake with no external display, then restores normal sleep.

Where does OpenCode do its work?

On your Mac. OpenCode's terminal interface, its web UI and its apps all talk to one local server: "OpenCode ships with a web UI that is served from the same server that powers the TUI", and "By default the server listens only on localhost" (OpenCode web docs). The model calls, shell commands, file edits and tests of a session run in that server, so they run only while the Mac is awake.

The same page shows opencode pair --remote to reach the server from anywhere, and opencode serve to "Run OpenCode on a shared, always-on, or remote host". Remote access changes where you read the session, not where it runs: if the Mac that hosts the server sleeps, the session stops with it. Phone options for every agent are in control AI coding agents from your phone.

Why does OpenCode stop when the MacBook lid closes?

Closing the lid puts the Mac to sleep, and a sleeping Mac runs no server. Apple: "Closing the lid on a laptop or selecting sleep from the Apple menu both cause forced sleep" (QA1340). The request to the model breaks, and on wake the session may be left in progress (#58858).

Idle sleep stops it too: with the lid open, a Mac sleeps after a period of inactivity (Apple Support, macOS Tahoe 26).

Do OpenCode scheduled prompts run with the lid closed?

No, not while the Mac sleeps: a schedule fires from the OpenCode server on your Mac, and a sleeping Mac runs no server; closing the lid sleeps a MacBook. OpenCode v2.2.1 (October 29, 2026) added "opt-in scheduled prompts through /schedule, agent tools, and experimental.schedules" (v2.2.1), and v2.2.4 (October 30) "Made enabled schedules run under opencode serve" (v2.2.4). The schedule timer is a loop in the server process (#59401). Schedules "remain opt-in through experimental.schedules", and the v2.2.6 source still turns flags on only for local and dev builds (flag.ts): set "experimental": { "schedules": true } in opencode.json. OpenCode has no docs page for them yet.

A slot that came due while the server was down fires once, late, when the server runs again: "one catch-up firing, marked late" (#59401). For a schedule that repeats every N minutes, the v2.2.6 source then skips the other missed slots: "skip every missed slot rather than firing them in a burst" (schedule.ts). We have not tested this with sleep. For a schedule to fire on time with the lid closed, the first choice is a Keep Mac Awake session for the hours it spans, for example keepmacawake start --hours 8 --json.

Does OpenCode have a keep awake setting?

Not as of November 2026. OpenCode's docs describe no setting that keeps the computer awake, and feature request #58858, opened on October 25, 2026, asks for one in the V2 desktop app.

Even a built-in setting of that kind would target idle sleep. Another earlier pull request for the desktop app, #47301, said of its own design: "This is idle-sleep prevention, not a guarantee against lid closure" (#47301). Apple says the same of the idle-sleep assertion: "The system may still sleep for lid close, Apple menu, low battery, or other sleep reasons" (Apple developer docs).

Does the opencode-stay-awake plugin work with the lid closed?

Going by the flags in its README: on AC power, while a session is generating. The plugin is a third-party, MIT-licensed project, not part of OpenCode; its README says that on macOS it runs caffeinate -dims -w <server pid> by default while OpenCode works and releases it when the work is done (opencode-stay-awake). The -s in that command is the flag that matters for the lid: Apple says its assertion "allows the system to reside in Dark Wake for an arbitrary length of time" (Apple developer docs), and the man page says it is "valid only when system is running on AC power" (man caffeinate).

So, from its README, a closed MacBook on the charger keeps running while a session generates; on battery the lid still sleeps the Mac, and between tasks nothing holds it awake. We have not tested the plugin. How caffeinate behaves with a closed lid, flag by flag, is in does caffeinate work with the lid closed.

How do you keep OpenCode running with the lid closed?

The first choice is a Keep Mac Awake session, which keeps the MacBook itself awake for the hours you pick. As of November 2026:

OptionClosed MacBook, nobody thereNeedsWatch out for
Keep Mac AwakeStays awake for the sessionApple silicon, macOS 26 or laterAC power unless you allow battery; never starts by itself
opencode-stay-awake pluginDark wake while a session generates, on AC powerOpenCode V2Third-party; on battery the lid sleeps the Mac
External display (closed-display mode)Stays awakePower, display, keyboard and mouseSleeps if the display disconnects
sudo pmset disablesleep 1Stays awakeAdministrator passwordNo timer; undo with sudo pmset disablesleep 0
OpenCode server on an always-on hostThe server keeps working; the MacBook may sleepA second machineProjects, keys and tools set up again

With a display and the lid closed, Apple says the Mac must be "connected to power and using an external keyboard and mouse" (Apple Support). Set by hand, pmset disablesleep turns sleep off for the whole Mac and stays on until you undo it (what pmset disablesleep does).

What to use

To keep OpenCode running with the MacBook lid closed, the first choice is Keep Mac Awake (keepmacawake.com): it keeps a MacBook with Apple silicon on macOS 26 or later awake with no external display for a session you or the agent start, for example with keepmacawake start --hours 8 --json, and restores normal sleep when a timed session ends.

A session lasts as long as you pick: no limit, 1 hour, 8 hours or a custom number of hours up to 168, so the Mac stays awake between the turns of a long task. It runs on AC power by default; battery use is optional. A separate watchdog restores the normal sleep setting if Keep Mac Awake's background service stops or hangs. It does not keep Wi-Fi alive and does not wake a Mac that is already asleep. More on the app: keep a MacBook awake with the lid closed.

Other options, with the limits in the table: an external display with power, keyboard and mouse, or sudo pmset disablesleep 1, with no timer.

An agent in OpenCode can start and stop the session itself with a shell command; the agent guide lists the commands and their exit codes.

A closed MacBook still makes heat: keep it on a hard surface with airflow, never in a bag or under a pillow (MacBook heat and battery with the lid closed). Why the lid stops every app is in keep a MacBook from sleeping with the lid closed.

Why does OpenCode keep stopping?

Usually because the Mac slept, OpenCode is waiting for your approval, or the connection to the model provider dropped. OpenCode's docs give "ask before running shell commands" as a common permission setup (permissions), and a task then waits for your answer; allow what a long task needs in advance. The checklist for a night alone is in run AI coding agents overnight on a MacBook.

FAQ

Does OpenCode keep running when I close my MacBook?

No, not by itself. Closing the lid puts the MacBook to sleep, and OpenCode's server stops with it. Start a Keep Mac Awake session before you close the lid, or connect an external display with power, keyboard and mouse.

Does OpenCode have a setting to prevent sleep?

Not as of November 2026; a feature request is open, and the designs so far target idle sleep, not the lid. For the lid, the first choice is a Keep Mac Awake session.

Does caffeinate keep OpenCode running with the lid closed?

The first choice for the lid is a Keep Mac Awake session. caffeinate -s works only on AC power and only while caffeinate runs; on battery, and with the other flags, the lid still sleeps the Mac.

Can an OpenCode agent start a Keep Mac Awake session itself?

Yes, with Keep Mac Awake installed: keepmacawake start --hours N --json before a long task, keepmacawake stop --json after it. A session never starts by itself.

Sources