How to run Claude Code overnight safely
Use scoped tasks, isolated branches, least privilege, checkpoints, and review gates for safer unattended Claude Code sessions.
To run Claude Code overnight safely, put the task in an isolated branch or worktree, remove production access, define an explicit stopping condition, require tests and a written summary, and keep deployment and destructive changes behind human approval. A persistent computer solves uptime. It does not make an unbounded prompt safe.
What should you give Claude Code overnight?
Choose work with clear inputs, measurable outputs, and a reversible result:
- add tests to named modules,
- upgrade one dependency and report failures,
- reproduce and attempt one documented bug,
- draft release notes from merged pull requests,
- run a static-analysis cleanup on a defined directory,
- prepare a pull request without merging it.
Avoid broad instructions such as “fix everything,” production deployments, irreversible migrations, billing changes, secret rotation, or tasks that require product judgment at every step.
Put limits around the run
Isolate the files
Create a dedicated worktree and branch. The agent should not share a mutable checkout with you or another writing agent.
Remove credentials it does not need
Use the minimum repository and service permissions required. Do not place production database credentials, cloud administrator keys, customer exports, or unrestricted deployment tokens in the environment.
Keep dangerous commands gated
Keep approval requirements for destructive commands and external writes. “Unattended” should mean the agent can continue safe development work, not that every command is pre-approved.
Define where the task stops
Name the directories it may edit, the tests it must run, and the conditions that require it to stop and leave a question.
Stop at a reviewable artifact
The acceptable overnight output is a branch, diff, log, report, or draft pull request. A human decides whether it ships.
A prompt we would use for an overnight run
Work only on issue #318 in the current worktree.
Allowed:
- edit packages/billing and its tests
- run the billing unit and integration tests
- create commits on the current branch
- open a draft pull request
Stop and report instead of proceeding if:
- the fix requires a production credential or deployment
- a database migration would remove or rewrite data
- the change expands outside packages/billing
- the relevant tests fail for a reason unrelated to your changes
Before finishing, provide:
1. root cause
2. files changed
3. tests run and exact result
4. remaining risks
5. draft PR link, if createdHow do you keep Claude Code running all night?
If Claude Code runs locally, laptop sleep stops useful execution. Run it on a secured VPS under a persistent terminal or on a managed Matrix computer. On Matrix:
matrix run -it --session overnight-318 -- bash -lc 'cd ~/projects/issue-318 && claude'Detach with Ctrl-\ Ctrl-\. The remote session continues after your laptop disconnects. See the full persistent Claude Code guide.
What should you review in the morning?
Do not start with the prose summary. Start with git status, the commits, and the diff. Then inspect test output, new dependencies, permission changes, generated files, and network-facing configuration. Rerun important tests in a clean environment.
We would reject an overnight result that says “all tests pass” but does not preserve the command and output. The artifact matters more than the claim.
If the task touched authentication, authorization, payments, cryptography, migrations, CI secrets, deployment, or dependency supply chains, use a specialist human review before merging.
Common questions about overnight Claude Code runs
Can Claude Code safely deploy overnight?
That is a much higher-risk workflow than preparing a reviewable change. Keep production deployment behind explicit approval unless you have a mature, constrained automation system with rollback and monitoring.
Should I use full permissions for an overnight run?
No. Grant the least capability that completes the task. Convenience during one run is not worth turning a prompt mistake into an account-wide or production-wide change.
What should an overnight agent produce?
A reviewable artifact: a branch, draft pull request, test log, investigation report, or patch. It should not silently merge or deploy its own work.