Keep Claude Code Running When Your Laptop Is Closed (2026)
Why Claude Code and Codex stop when your laptop sleeps, the exact caffeinate and pmset commands, cloud sessions vs VPS vs Mac mini, and how to recover a session after an unexpected sleep.
A coding agent that runs on your laptop stops when the laptop sleeps. The fix is to move agents somewhere that stays on. You have four options: keep the machine awake, use an official cloud session (Claude Code cloud sessions or Codex cloud), move to an always-on machine such as a VPS or Mac mini, or use a hosted agent platform.
- Best quick fix on a Mac:
caffeinatewith the lid open and the laptop plugged in. For a closed lid,sudo pmset -a disablesleep 1or Amphetamine's Closed-Display Mode can work, but expect network drops. Free. - Best zero-setup cloud option: Claude Code cloud sessions (included in Pro, Max, and Team plans) or Codex cloud. Each is tied to its own vendor's subscription.
- Best full control: a VPS or Mac mini you set up and maintain yourself. From €11.49/month for a Hetzner CPX12 instance or $899 for a current Mac mini.
- Where Cloudroom fits: a cloud sandbox for each of your agents, with your own subscriptions and an open-source core. Claude Code, Codex, and Pi Agent already work. Invite-only today.
There is no trick here. The model runs in a data center either way. The part that dies is the agent harness on your laptop: the process that calls the model, runs commands, and edits your files.
Why does Claude Code stop when I close my laptop?
Claude Code running locally stops because macOS puts the machine to sleep, and sleep pauses ordinary local execution. Closing the lid is the trigger. It is not Claude Code's fault, and the same problem hits Codex and every other terminal-based agent.
People describe this pain in real terms. One r/ClaudeCode user wrote that they "literally can't close my laptop, not even the screen because I'm afraid it will stop working," and that they shower quickly so they can answer the agent's questions. Another shipped a fix after "keeping your laptop half-open" became a meme with a claimed 5 million views.
The short version: a laptop is the wrong home for work that must keep running. The relevant question is which machine is actually running the agent and its tools.
Can I just keep my Mac awake with the lid closed?
There are three macOS tools, and they are not the same. Two of them handle a closed lid. One of them does not.
caffeinate is built into macOS at /usr/bin/caffeinate. The common command is:
caffeinate -dimsuThe flags mean: -d prevents display sleep, -i prevents idle sleep, -m prevents disk sleep, -s prevents system sleep (on AC power only), and -u declares the user active. This is the 13-upvote answer on the r/ClaudeCode thread "Best way to keep a Mac awake while Claude Code runs?"
But caffeinate prevents idle sleep. Closing the lid still triggers sleep unless the MacBook is in clamshell mode, which needs power plus an external display. One guide is explicit: to actually keep it running, run sudo pmset -a disablesleep 1 before closing the lid. Codex's desktop app has the same limitation. Its "Keep this Mac awake" setting runs caffeinate -dims, and GitHub issue #23294, opened May 18, 2026, reports a Mac connected to power still slept and dropped off the network.
pmset is the tool that actually stops lid-close sleep:
sudo pmset -a disablesleep 1Restore it later with:
sudo pmset -a disablesleep 0This needs sudo, and one guide warns that raw disablesleep can leave the internal display running at full brightness under a closed lid, draining the battery for hours.
Amphetamine is a free menu bar app. Its Closed-Display Mode keeps the Mac awake with the lid closed. A 33-upvote comment on the Codex lid thread is simply: "Did you try amphetamine? Solved problem." The App Store page notes that closed-display sessions can fail if you connect or disconnect the display mid-session.
From my own experience, I would not rely on Amphetamine. The biggest struggle is keeping the laptop connected to the internet, and the overall instability. I tried a hotspot and a regular WiFi connection, and both failed on me. On paper it reads like the clean fix. In practice it feels gimmicky, and I would rather run caffeinate with the lid open and the laptop plugged in.
All of this works until reality interrupts. The Laurel founder explained the counter-argument on r/codex: "Caffeinate and Amphetamine work until the battery dies or the wifi drops." Heat is a second limit. Do not put a closed MacBook running an agent at full load in a bag. It needs airflow, power, and ideally a cool surface.
Windows has the same toggle under Power Options: set "When I close the lid" to "Do nothing," or change it from an elevated prompt with:
powercfg /setacvalueindex SCHEME_CURRENT SUB_BUTTONS LIDACTION 0
powercfg /setdcvalueindex SCHEME_CURRENT SUB_BUTTONS LIDACTION 0
powercfg /setactive SCHEME_CURRENTOn Linux, set HandleLidSwitch=ignore in /etc/systemd/logind.conf and restart systemd-logind.
Does Claude Code on the web keep running after I close the laptop?
Yes, and this is the closest thing to a zero-setup answer. Anthropic's documentation states directly: a cloud session "keeps running after you close your laptop, and you can check on it or steer it from any device."
Cloud sessions run on Anthropic's infrastructure, not your machine. You can start one from claude.ai/code in the browser, the Code tab in the Claude mobile app, the desktop app by selecting Cloud instead of Local, the terminal with:
claude --cloud "Fix the authentication bug in src/auth/login.ts"or as a scheduled routine. You can pull a cloud session back into your terminal with --teleport, and send a follow-up from any machine with claude -p "message" --cloud <session-id>.
Cloud sessions are available on Pro, Max, and Team plans, and for Enterprise users with premium seats or Chat + Claude Code seats. When the feature went generally available this month, the launch post drew nearly 13,000 likes, and existing subscribers got a one-time credit: $100 on Pro, $250 on Max, with a claim deadline of October 7, 2026. There is no separate compute charge for the cloud VM, and the credits get used before your plan's token allowance.
The downsides are real and users say them out loud:
- Cloud sessions clone from GitHub. They do not run against your local files. To pull from the cloud back to your terminal you must authenticate to the same account.
- Repository cloning and pull request creation require GitHub. You can send a non-GitHub repo as a local bundle with
CCR_FORCE_BUNDLE=1, but then the session cannot push results back to that remote. - Rate limits are shared across your whole account, so parallel cloud sessions drain your allowance faster.
- An 18-upvote comment on r/ClaudeAI reads "IT USES THE CREDITS BEFORE YOUR PLAN'S TOKEN ALLOWANCE." Users also report network constraints inside the containers and missing local CLIs and MCP servers.
Does Codex cloud keep running without my laptop?
Yes. Codex cloud runs tasks in isolated cloud environments and is built for background work. OpenAI's docs list "you are away from your development machine" as a reason to use it: you start and review work from the web or the Codex CLI.
You connect GitHub or GitLab (GitLab is in beta), create an environment with the dependencies and variables for a repo, and hand off tasks. You can start several tasks in parallel and review each diff when it is ready. Work can also be triggered from GitHub pull requests and issues, GitLab merge requests, Linear issues, and Slack.
The tradeoff is the same as Claude Code cloud: it is a disposable isolated environment, not your machine. You configure the environment per repository, and code moves through a review-and-merge flow rather than living in your usual local workspace.
One limit cuts across both official clouds: each is tied to its own vendor. Claude Code cloud sessions run on your Claude plan, and Codex cloud runs on your ChatGPT account. You cannot use your Claude subscription inside Codex cloud, or the reverse.
Should I move my agent to a VPS or a Mac mini?
If you want the agent to run on your own always-on hardware, yes. The crowd answer under the cloud sessions launch is exactly this, and it is the top comment with 274 upvotes:
"Buy a minipc. Install Linux. Dont turn it off. Problem solved."
One commenter describes "Refurbished mac-mini M1 for €380 and tailscale," and levelsio describes doing "about half the time" of their coding from a phone with Termius into a VPS. A current Mac mini starts at $899 (M6) or $1,699 (M5 Pro) as of the August 25, 2026 lineup. On the cloud side, Hetzner's CPX12 shared instance (1 vCPU) lists at €11.49/month excluding VAT. The "$15 a month" figure is one user's anecdote, not a market price.
The catch is everything after the easy part. One r/ClaudeAI post titled "Everyone's putting Claude Code on a VPS this week. The setup is the easy part, here's the 8 walls you hit after" frames it well. The walls users actually report:
- Security. One user calls an exposed Claude Code install "the single biggest attack vector you'll ever be able to create."
- Runaway loops. A loop that goes sideways overnight can eat budget quietly.
- Setup and upkeep. Tailscale boot auth, tmux, mosh, dropped remote sessions, and keeping it all alive yourself.
- Remote development. Codex's desktop app has a remote development feature request (openai/codex#10450, now closed) that drew 179 comments.
A VPS only has the files you clone or copy to it. Your laptop's files do not come along automatically. If you prefer a managed middle ground, DigitalOcean opened Managed Agents in public preview around September 22, 2026; it runs Claude Code and Codex CLI, and a session with two vCPUs at 25% average utilization and a 4 GB memory peak bills about $0.060 for an hour of CPU and memory, against $0.126 for a full hour of that capacity.
If you want to skip most of this setup, Cloudroom is the open-source alternative. You can host it on any machine that runs Linux, a spare laptop, a Mac mini at your office, or a random VPS, and connect your own machine to it. The Rust core is open sourced at github.com/davidondrej/cloudroom-core. Cloudroom manages agent execution and session history while your own subscriptions connect to their providers directly. It already runs Claude Code, Codex, and Pi Agent.
Does Claude Code Remote Control work when the laptop is closed?
No. Remote Control is a window into a session that still runs on your laptop. Anthropic's docs are explicit: "your computer has to stay on and the claude process has to keep running." Code execution and filesystem access stay on the host machine.
Remote Control is great for steering from a phone, and the docs say an interactive session reconnects on its own when the machine comes back online. It does not run anything while the machine is off. If your laptop sleeps, the session sleeps with it. Users say this plainly: "Claude Code Remote is great until your dev machine goes to sleep," and one r/ClaudeCode thread is literally titled "Claude Code needs real remote control from mobile."
There is a second limit worth knowing. In server mode (claude remote-control), if the machine is awake but can't reach the network, Claude Code gives up after roughly 10 minutes and the process exits. You run claude remote-control again to start a new session. An interactive session retries for as long as the outage lasts instead.
So Remote Control solves the wrong half of the problem. It moves the screen, not the execution.
What happens if the laptop sleeps mid-task, and how do I recover?
This is where most guides stop, and it is the part worth getting right. A restored connection does not automatically mean the agent resumed its turn.
The Cloudroom team ran disconnect experiments on this before building the product, published as the Immortal Agents project. The results show that the same outage leaves different agents in different states:
- 21 minutes offline: Claude Code and Pi needed a continuation prompt in one test. Codex recovered on its own. A finished control session stayed idle. Experiment 0016
- 92 minutes offline: a separate test with Codex 0.153.2 connected directly to OpenAI had one target complete after reconnecting while another was still recovering, with no continuation prompt sent. Experiment 0017
- Stale DNS: after a roughly 10 minute outage, connectivity checks passed and a continuation prompt was sent, but the system resolver still failed to resolve the model provider's hostname. The recovery signal was too broad: general internet was back, but the agent's path was still broken. Experiment 0009
- Version and path matter: an earlier Codex 0.149 test through a local proxy stopped after about 5 minutes offline, while the later 0.153.2 test recovered after much longer. Earlier test
The practical rules that came out of this: check the provider's connection before trying to revive work, and verify the agent actually resumed instead of assuming it did. A reconnecting session should be allowed to reconnect. A finished session should stay finished.
To resume work after a crash or sleep:
- Claude Code:
claude --continuereopens the most recent conversation in the current directory.claude --resumeopens the session picker, andclaude --resume <session-id>resumes a session by ID from any directory.claude --resume <name>resumes a named session. Inside a session,/resumeswitches conversations. - Codex:
codex resumelists past sessions in the current repo,codex resume --lastreopens the most recent one, and--allincludes sessions from other directories.
Before trusting a resumed session, look at the workspace. Run git status and git diff to see what actually changed. Before retrying a side-effecting action like opening a pull request, check whether it already happened. Repeating an action that already ran can create a duplicate. A saved chat alone does not prove the work is safe to continue.
Do not blindly restart a stopped agent. A crash needs recovery. A completed task, a deliberate stop, or a question waiting for your input needs something different. Restarting every stopped session can repeat work or skip an intended pause.
Comparison: which method actually keeps an agent running
| Method | Runs while the laptop is closed | Your files | Cost | Setup | Main failure mode |
|---|---|---|---|---|---|
| caffeinate -dimsu (and Codex "Keep this Mac awake") | No (prevents idle sleep, not lid-close sleep) | Yes | Free | Minutes | Lid close still sleeps; battery, wifi |
| pmset disablesleep 1 or Amphetamine Closed-Display Mode | Yes | Yes | Free | Minutes | Battery dies, heat, unstable, requires internet connection |
| Claude Code cloud sessions | Yes | No, clones from GitHub | Included in plan; shared rate limits | Minutes | No local files or CLIs; GitHub needed to push |
| Codex cloud | Yes | No, isolated environment | Uses your ChatGPT account | Minutes | Disposable env, review and merge flow |
| VPS or Mac mini + tmux/SSH | Yes | Only what you clone or copy | €11.49/mo Hetzner instance or $899 Mac mini | Hours to days | You run security, loops, and upkeep |
| Cloudroom | Yes | A copy of your repo in each agent's cloud sandbox | Not announced | Waitlist | Invite-only for now |
The pattern: everything that is free or zero-setup loses either your local files or the guarantee it will still be running hours later. Everything that keeps your files and keeps running puts the maintenance on you. That gap is why hosted agent platforms exist.
How Cloudroom approaches this
Cloudroom is a platform for running coding agents in the cloud, each in its own sandbox. You bring your own model accounts and subscriptions. The harness connects to its provider directly, so Cloudroom does not replace your inference provider or bill you for its model calls.
The workflow: hand a task to Claude Code, Codex, or Pi Agent in the Cloudroom desktop app, close the laptop, and come back to the result and the saved conversation. Claude Code already works today with your own Claude subscription, both on your Mac and in cloud sandboxes.
Cloudroom is invite-only while we test it. Join the waitlist to get an invite code. The independent core is designed to run independently on a Linux VM with PostgreSQL and is working internally, but the polished self-hosting packaging is still being prepared.
Every option on this page trades one of three things: your time, your files, or your money. Pick which one you want to spend.
A FEW DETAILS
Questions, answered.
Does closing the lid kill Claude Code?
A locally running Claude Code session pauses when the Mac sleeps, and closing the lid usually triggers sleep. It does not "kill" the session file, but the work stops and may not resume cleanly. To keep working, run the session on a machine that stays awake or in a Claude Code cloud session.
Can Codex run with the MacBook lid closed?
Yes, but caffeinate and Codex's "Keep this Mac awake" setting are not enough, because they prevent idle sleep, not lid-close sleep. sudo pmset -a disablesleep 1 or Amphetamine's Closed-Display Mode can keep a closed MacBook awake, but the network connection can still drop, so do not count on it for long runs. For work that must survive the laptop being off entirely, use Codex cloud, which runs tasks in isolated cloud environments you can review later.
Does Claude Code on the web cost extra?
No separate compute charge for the cloud VM. Cloud sessions are included in Pro, Max, and Team plans, and this month existing subscribers got a one-time $100 (Pro) or $250 (Max) launch credit that is used before the plan's token allowance. The credits are claimed by October 7, 2026.
Will my agent automatically resume when the laptop wakes up?
Sometimes. In Cloudroom's Immortal Agents tests, Codex recovered on its own after 21 minutes offline while Claude Code and Pi needed a continuation prompt, and a 92 minute Codex outage ended with one task done and another still recovering. Check that the provider connection works and that the session actually resumed before assuming it did.
Can I run Claude Code on a cheap VPS with my subscription?
Yes. Install the Claude Code CLI on the VPS and log in with /login using your claude.ai credentials, which connects your Pro or Max plan subscription to Claude Code. One caveat: if an ANTHROPIC_API_KEY environment variable is set, Claude Code uses the API key instead of your subscription and you get billed for API usage. Usage limits are shared across Claude and Claude Code. A Hetzner CPX12 instance lists at €11.49/month, and one user reports running everything on a $15/mo VPS.
Is it safe to disable sleep permanently?
No, not without caveats. sudo pmset -a disablesleep 1 can leave the display on under a closed lid, drain the battery, and generate heat. Never leave a closed MacBook running an agent inside a bag or another enclosed space. For overnight runs, keep the laptop plugged in and prefer the shortest sleep override that covers your task.