How to Run Claude Code Overnight: Goals, Loops and Routines

Run Claude Code while you sleep with a measurable goal, a machine that stays awake, cloud routines and a progress file you read at breakfast

Leaving Claude working overnight sounds like a trick. In practice it comes down to three boring things: a finish line Claude can check by itself, a machine that stays on, and a written record you can read with your coffee. Get those right and you wake up to finished work. Miss one and you wake up to a session that stopped at 11:40 pm and waited for you all night.

First decide where it runs

Claude Code gives you three places to run unattended work, and the choice decides everything else.

  • An open session on your Mac. /goal and /loop run here. Full access to your local files and tools. Your Mac has to stay awake.
  • A Desktop scheduled task. Set up in the Claude desktop app. Runs on your machine at set times, but only while the app is open and the computer is awake. If the Mac sleeps through the scheduled time, the run is skipped.
  • A cloud routine. Runs on Anthropic's infrastructure, so your laptop can be closed. It works on a fresh clone of your GitHub repo plus any connectors you add. It cannot see files that only exist on your computer.

Local files or a local toolchain means your Mac. Everything else can go to the cloud.

Option 1: a goal with a real finish line

/goal sets a condition, and Claude keeps working turn after turn until a separate model confirms the condition holds, judges it impossible, or hits an error you need to fix. It is the best overnight tool for a single big task.

/goal every page in src/pages has a passing test in tests/pages, npm test exits 0,
no existing test file is modified, or stop after 40 turns

Three things make a goal work. One measurable end state. A stated check, like "npm test exits 0". And a cap, so a stuck task does not burn your whole plan. The evaluator only sees what Claude shows in the conversation, so ask for proof it can print, like a test run. "Make the app better" is not a goal. Claude cannot grade it, and neither can you.

A goal does not change your permission mode. In Manual mode Claude will stop and wait at the first command you have not allowed. For an overnight run, use auto mode, with deny rules for anything you never want to happen.

Run it headless and keep the Mac awake

For a run you start before bed, I prefer a single terminal command over an interactive window:

cd ~/code/my-app
git switch -c overnight/page-tests
caffeinate -i claude -p "/goal every page in src/pages has a passing test, npm test exits 0, or stop after 40 turns" \
  --permission-mode auto --output-format stream-json --verbose > overnight.jsonl

caffeinate -i is built into macOS. It stops idle sleep for exactly as long as the command runs, then lets go. It does not stop sleep when you close the lid, so leave the laptop open and plugged in. The stream JSON output writes every step to a file as it happens, so you can see what happened at 3 am instead of one summary at the end.

I built a small Mac app for this, Nightwatch, which keeps the Mac awake while agents run.

Option 2: a loop for work that keeps arriving

/loop reruns a prompt on an interval inside an open session. Use it when the work is not one task but a stream: new issues, CI results, a folder that fills up during the night.

/loop 1h check the inbox folder for new CSV exports, clean each one with the
cleanup script, move it to processed/, and append a line to PROGRESS.md

A loop repeats on a timer. It does not chase a finish line the way a goal does. Loops live inside the session, need the machine on, and expire after seven days.

Option 3: a routine in the cloud

For recurring work that only needs GitHub and connectors, use a routine. Create one at claude.ai/code/routines or by typing /schedule in Claude Code, for example /schedule every weekday at 6am, triage new issues and draft replies. Times use your local time zone. Claude pushes its changes to branches that start with claude/, so nothing lands on your main branch without a pull request. Routines are in research preview and count against a daily run allowance, so check the routines docs for current limits.

Option 4: split a big batch across subagents

When the night's work is fifty similar items, ask Claude to split it: "Use subagents to process the files in batches of ten, in parallel, and merge the results." This works when the pieces are truly independent. If two agents might touch the same file, split by folder, or give each subagent its own git worktree. More agents means more usage, so only fan out work that is actually parallel.

The progress file

This one habit decides whether a long run holds together. On a long session, older context gets summarized to make room, and small decisions get lost. A file on disk does not. Anthropic's engineering team describes the same approach, a progress log plus git history, in their write up on harnesses for long running agents. Add this to any overnight prompt:

Keep PROGRESS.md in the repo root. Before each unit of work, read it.
After each unit, append: what you did, what you decided and why, what is next.
Commit after each finished unit with a clear message.
If you get stuck on something for more than three attempts, write it under
"Blocked" in PROGRESS.md and move to the next item.

The "Blocked" line matters. Without it, one hard item can eat the whole night.

Before you go to bed

  1. Commit or stash everything and start a fresh branch.
  2. Write the finish line as something a test or command can prove.
  3. Add a cap: a turn limit in the goal, or a clear stopping rule.
  4. Switch to auto mode and check your deny rules cover pushing, deleting and sending.
  5. Ask for the progress file.
  6. Plug in, keep the lid open, and use caffeinate or a keep awake app.

In the morning

Read PROGRESS.md first, then git log, then the diff. Run the tests yourself. Claude's summary tells you what it believes it did. The diff tells you what it actually did. Those are usually the same, and the morning review is how you keep it that way.

Start with one small overnight goal this week, something you can verify in ten minutes the next morning. Once that feels routine, move to bigger batches and the cloud. The goal docs and scheduling docs cover every option in more detail.

More in Agents

← All guides