---
title: "Run coding agents overnight - sandbox, git, cost caps"
description: "A practical setup for unattended coding agent runs: sandboxing, git checkpoints, task files, step limits, morning review, and flat plan sessions overnight."
url: https://broadrouter.com/blog/overnight-coding-agent-runs/
lang: en
updated: 2026-11-14
publisher: BroadRouter (a BroadNet site); API by BroadNet Inference, https://inference.broadnet.ai
---

# How to run coding agents unattended overnight, safely

By the BroadNet Inference team · 5 min read · Published 2026-11-14 · Updated 2026-11-16

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.

## Key takeaways

-   Sandbox first: a container or VM with only the repo mounted and no production credentials.
-   One branch or git worktree per agent, with a commit after each step, makes every change reviewable and reversible.
-   A task file with a clear definition of done beats a one-line prompt.
-   Step caps, a deadline and a repetition guard stop stuck runs before morning.
-   A plan session has no token meter, so a night of agent work costs the plan price whatever the token count.

**On this page**

1.  1\. How do you sandbox an unattended agent?
2.  2\. Why give every agent its own branch?
3.  3\. What goes in the task file?
4.  4\. Which guards stop a stuck agent?
5.  5\. Is a plan session or per-token billing cheaper overnight?
6.  6\. How do you review the run in the morning?
7.  How do you try it safely the first night?

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](https://docs.docker.com/engine/security/) 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](https://broadrouter.com/guides/api-keys-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](https://git-scm.com/docs/git-worktree):

```bash
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:

```markdown
# 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](https://broadrouter.com/blog/long-running-agent-context-management/).

## 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](https://broadrouter.com/blog/cut-coding-agent-llm-costs/) in the coding-agent cost guide.

Aider is easy to run this way: its [scripting docs](https://aider.chat/docs/scripting.html) 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](https://broadrouter.com/models/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](https://broadrouter.com/blog/run-parallel-coding-agents-flat-rate/) 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](https://broadrouter.com/tools/flat-rate-vs-per-token-calculator/). Long unattended runs also need context discipline and retries that survive a crash: see [timeouts, retries and checkpoints](https://broadrouter.com/blog/long-running-agents-timeouts-retries/).

## 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](https://broadrouter.com/guides/aider-unlimited-deepseek/) has the exact connection settings, [the best open model for coding agents](https://broadrouter.com/blog/best-open-model-for-coding-agents/) helps you pick a model, and [how plan sessions work](https://broadrouter.com/guides/unlimited-llm-api-sessions/) covers keys and sessions.

Free tools for this

-   [Agent run cost estimator](https://broadrouter.com/tools/agent-run-cost-estimator/) Context growth per step, total tokens and cost of one agent run, with a DeepSeek session comparison.
-   [LLM batch job planner](https://broadrouter.com/tools/llm-batch-job-planner/) Run time, binding limit, cost per model and the flat sessions needed to finish a big job by a deadline.
-   [Flat-rate vs per-token LLM cost calculator](https://broadrouter.com/tools/flat-rate-vs-per-token-calculator/) Break-even between an unlimited plan and paying per token, per agent.
-   [LLM rate limit and concurrency calculator](https://broadrouter.com/tools/llm-rate-limit-calculator/) Which limit binds first: RPM, TPM or concurrency, with Little's law.

[All free tools](https://broadrouter.com/tools/)

## Terms in this article

[Token](https://broadrouter.com/glossary/token/)

A token is the unit of text a language model reads and writes: a word, part of a word, a punctuation mark or a byte sequence produced by the model's tokenizer. LLM APIs count, limit and usually bill usage in tokens.

[Pay-as-you-go](https://broadrouter.com/glossary/pay-as-you-go/)

Pay-as-you-go is a pricing model in which you pay only for what you use, metered per unit such as tokens, requests or audio minutes, with no subscription or minimum commitment. Most LLM APIs bill this way, from a prepaid balance or a periodic invoice.

[AI coding agent](https://broadrouter.com/glossary/coding-agent/)

An AI coding agent is a software tool in which a language model works on a codebase in a loop: it reads files, edits them, runs commands such as tests, reads the output and decides the next step until the task is done or a limit is reached.

[API key](https://broadrouter.com/glossary/api-key/)

An API key is a secret string that identifies and authorizes a client calling an API. LLM APIs usually expect it in an Authorization: Bearer header, and anyone who holds the key can make requests that are billed to the account that owns it.

[Cached input tokens](https://broadrouter.com/glossary/cached-input-tokens/)

Cached input tokens are prompt tokens that an LLM provider has recently processed and can reuse from a cache instead of computing again, usually a repeated prefix such as a system prompt or document. Providers bill them at a lower rate than new input tokens.

[Concurrency](https://broadrouter.com/glossary/concurrency/)

Concurrency is the number of requests a client has in flight at the same time. LLM APIs often cap it per key or plan, separately from requests per minute, because each open request holds GPU capacity until its response has finished.

[Context engineering](https://broadrouter.com/glossary/context-engineering/)

Context engineering is the work of choosing and maintaining the set of tokens a language model sees on each request, including system prompt, tool definitions, history, retrieved documents and notes, so the model gets the smallest set of high-signal information it needs to do the task well.

[finish\_reason](https://broadrouter.com/glossary/finish-reason/)

finish\_reason is a field on each choice in a chat completion response that says why the model stopped generating. In the OpenAI format the values are stop, length, tool\_calls, content\_filter and the deprecated function\_call. Anthropic's Messages API reports the same idea as stop\_reason.

## 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.

## Keep reading

-   [10 ways to cut coding agent LLM costs, with estimated savings](https://broadrouter.com/blog/cut-coding-agent-llm-costs/)
-   [How to run parallel coding agents on a flat-rate LLM API](https://broadrouter.com/blog/run-parallel-coding-agents-flat-rate/)
-   [Context management for long-running agents with a 1M window](https://broadrouter.com/blog/long-running-agent-context-management/)
-   [Flat-rate vs pay-per-token LLM API: the break-even math](https://broadrouter.com/blog/flat-rate-vs-pay-per-token-llm-api/)
-   [Prompt injection in coding agents: defences that actually hold](https://broadrouter.com/blog/prompt-injection-coding-agents/)
-   [Claude Code with open models: proxies, native endpoints and limits](https://broadrouter.com/blog/claude-code-with-openai-compatible-models/)
-   [OpenClaw and Hermes Agent on a cheap model: setup and token math](https://broadrouter.com/blog/openclaw-hermes-agent-cheap-model/)
-   [Guide: Run Aider on unlimited DeepSeek V4.1 Flash](https://broadrouter.com/guides/aider-unlimited-deepseek/)
-   [Guide: How the unlimited LLM API plans work for agents](https://broadrouter.com/guides/unlimited-llm-api-sessions/)
-   [Guide: Lock API keys to your servers with IP allow-lists](https://broadrouter.com/guides/api-keys-ip-allow-lists/)

Try it on your real workload. New accounts start with $5 of credit for Gemma and transcription. DeepSeek, GLM and Qwen3.8 need a top-up.

[Get started](https://inference.broadnet.ai/account/?utm_source=broadrouter&utm_medium=referral&utm_campaign=blog-overnight-coding-agent-runs&utm_content=page-cta) ·

**Sources**

-   [Docker docs, Docker Engine security](https://docs.docker.com/engine/security/)
-   [Git documentation, git-worktree](https://git-scm.com/docs/git-worktree)
-   [Aider docs, scripting aider](https://aider.chat/docs/scripting.html)
-   [BroadNet Inference unlimited plans](https://inference.broadnet.ai/plans?utm_source=broadrouter&utm_medium=referral&utm_campaign=blog-overnight-coding-agent-runs&utm_content=sources)
