BroadRouter
Get started

How to run coding agents unattended overnight, safely

By the BroadNet Inference team · 5 min read · Published · Updated

Run the agent in a container or VM with only the repository mounted, on its own git branch, committing after every completed step. Give it a written task file, a step cap and a wall-clock deadline, then review the branch in the morning. A flat plan session, such as BroadNet's $60 Full Moon, fixes the worst-case bill.

Coding agents are good at long, well-defined, tedious work: adding tests to fifty modules, migrating every call site to a new API, fixing a thousand lint warnings. That kind of work does not need you watching. It needs a safe place to run, a clear task, guards against getting stuck, and a way to review the result over coffee.

This is a checklist for setting that up.

1. How do you sandbox an unattended agent?

An unattended agent runs shell commands nobody approves. Assume it will eventually run something you would not have: delete the wrong directory, install a package, push to a remote. Contain it.

  • A container or VM, not your laptop’s main user account. Docker’s security documentation covers the controls; at minimum run as a non-root user and drop capabilities you do not need.
  • Mount only the repository. No home directory, no SSH keys, no cloud credentials, no ~/.config full of tokens.
  • No production secrets. Use test databases and dummy API keys. If the agent needs a real service, give it a scoped, revocable key.
  • Limit the network. The agent needs the model API and maybe a package registry. It rarely needs anything else.
  • No push rights. Let it commit locally. You push after review.

The model API key is the one credential the agent must have. Use a key made for this purpose, and if your provider supports it, restrict it to the sandbox’s egress IP. BroadNet keys support IP allow-lists of up to 32 entries, so a leaked key is useless from anywhere else.

2. Why give every agent its own branch?

Work on a dedicated branch, never on main. If you run more than one agent, give each its own working directory with git worktree:

git worktree add ../agent-tests-api -b agent/tests-api
git worktree add ../agent-lint-fixes -b agent/lint-fixes

Each worktree is a separate checkout of the same repository, so agents do not overwrite each other’s files, and each branch shows exactly what one agent did.

Then commit after every completed step. Aider does this by default. For other agents, add it to the instructions or wrap the loop so a commit happens whenever tests pass. A history of small commits turns “the agent broke something at 3 a.m.” into “commit 14 broke it; keep 1 to 13”.

3. What goes in the task file?

A one-line prompt that works interactively fails overnight, because there is nobody to answer the agent’s first question. Write a task file:

# Task: add unit tests to src/billing/

## Goal
Every public function in src/billing/ has at least one unit test.

## Done when
- `pytest tests/billing` passes
- coverage for src/billing/ is reported (no target)
- no changes outside tests/billing/ except fixing obvious bugs, each in its own commit

## Rules
- Do not change public function signatures.
- If a function is untestable without a refactor, list it in NOTES.md and move on.
- Run `pytest tests/billing -x` after each new test file.

## Notes
Append decisions and skipped items to NOTES.md as you go.

The “done when” section is the important part. It gives the agent a way to know it is finished, and gives you a way to check. The notes file doubles as memory if the run restarts or the context gets compacted; see context management for long-running agents.

4. Which guards stop a stuck agent?

Unattended means nobody notices a stuck agent until morning. Four guards:

  1. Step cap. A fixed maximum number of model calls per task.
  2. Deadline. Stop at a wall-clock time, for example 07:00, whatever the state.
  3. Repetition detection. Stop when the same tool call returns the same result three times, or no file has changed for a set number of steps.
  4. Per-tool timeouts. A hung test suite should not hang the night.

When a guard trips, the agent should write why to the notes file and exit cleanly. Why step caps and retry caps matter for cost is covered under cap steps per task in the coding-agent cost guide.

Aider is easy to run this way: its scripting docs show the --message option to run one instruction and exit, and --yes-always to accept prompts automatically. Wrap that in a shell loop over a task list, with timeout around each run, and you have a simple overnight queue.

5. Is a plan session or per-token billing cheaper overnight?

Overnight runs change the cost question. Nobody is watching the meter, and a stuck agent can loop for hours.

On a per-token key, cost depends on how much the agent sends. Agents resend context on every step, so an eight-hour run adds up. Prompt caching helps a lot (cached input on DeepSeek V4.1 Flash is $0.0055 per 1M tokens against $0.175 new), but output and reasoning tokens are billed in full. You need a token budget per run and a spend limit on the account.

On a plan session, there is no token meter. A Full Moon session is $60 a month for unlimited DeepSeek V4.1 Flash, and that is the ceiling however long the agent runs or however much it loops. For overnight work that is the main attraction: the worst-case bill is known before you go to bed.

Per-token key Plan session
Worst-case cost of a stuck night Grows with tokens Fixed at the plan price
Parallel agents Up to 4 requests in flight per pay-as-you-go key One request at a time per session
Models All chat models DeepSeek V4.1 Flash only
Guards still needed Yes, for cost and time Yes, for time

Two practical notes on sessions. A session runs one request at a time, so two agents sharing a session take turns; tasks on one session run in sequence, which is often fine overnight. If you want agents running side by side, buy a session per agent. Parallel agents on a flat-rate API covers the arithmetic.

At typical speed of about 100 tokens per second, one session can produce about 259 million output tokens in a month of continuous use. Whether that beats per-token pricing for you depends on volume; BroadNet’s worked example of a full-time coding agent comes to about $65.40 a month on pay-as-you-go. Run your numbers in the flat-rate vs per-token calculator. Long unattended runs also need context discipline and retries that survive a crash: see timeouts, retries and checkpoints.

6. How do you review the run in the morning?

The value of an overnight run is only realised at review time. Make it fast:

  1. Read NOTES.md first. What did the agent finish, skip and get stuck on?
  2. Check the stop reason. Done, step cap, deadline or repetition?
  3. Run the tests yourself. Do not trust the agent’s report of green tests.
  4. Read the diff by commit. Small commits make bad changes easy to spot and drop.
  5. Squash or rebase what you keep, then open a normal pull request.

Most mornings you will keep most of the work and fix a few commits by hand. On bad mornings you delete a branch. Either way, nothing touched main without you.

How do you try it safely the first night?

Start with one well-specified task, one agent and one session. A Meteor hour ($1) is enough to check that the sandbox, branch and guard setup work before you leave anything running all night. For the agent itself, the Aider guide has the exact connection settings, the best open model for coding agents helps you pick a model, and how plan sessions work covers keys and sessions.

Questions

Is it safe to let an agent run shell commands unattended?

Only inside a sandbox you are happy to throw away. Mount the repository, keep secrets and production credentials out, and limit network access to what the task needs.

How many overnight tasks can one plan session handle?

A session runs one request at a time, so tasks on one session run one after another. Queue them in sequence on one session, or buy a session per agent you want running in parallel.

What if the agent breaks the build overnight?

Nothing reaches your main branch without review. With a commit per step you can see exactly where it went wrong and keep the good commits before that point.

Which tasks suit overnight runs?

Well-specified, testable work such as migrating call sites to a new API, adding tests to untested modules, fixing lint across a codebase, upgrading dependencies. Open-ended design work is better done with a human in the loop.

Read as MarkdownAsk ChatGPTAsk ClaudeAsk Perplexity