# Automation at scale

Unattended runs, loops and parallel sessions multiply every cost driver, and nobody is watching to stop them; budgets and early exits are the brakes.

> Automation multiplies the cost drivers: every unattended run or loop iteration re-reads its context, and parallel sessions each pay for their own. Without a person watching, a budget has to be the stop condition.

Source: https://ai-sw-factory.mellicci.dev/cost/automation-at-scale

**Diagram:** A loop runs, checks, and continues or stops; the budget is the one box that guarantees it ends.

- Run — one iteration of work
- Check — anything new? goal met?
- Stop condition: budget — iterations, time, tokens
- Continue — next iteration, same loop
- Centre: Every pass re-reads its context

When you work in a session, you notice when it gets slow, long or expensive. When an agent runs headless, in a loop or in several sessions at once, nobody notices. The mechanics from the earlier pages still apply, but they repeat without a person to interrupt them.

This page is about what that repetition does to the bill and which limits you set in advance.

## Why it matters

A loop multiplies every driver you have already met. Each iteration re-reads its context ([Context growth](https://ai-sw-factory.mellicci.dev/cost/context-growth)), and a run that starts cold pays to write its cache before it can reuse it ([Prompt caching](https://ai-sw-factory.mellicci.dev/cost/prompt-caching)). Multiply a modest per-iteration cost by dozens of iterations a night and a small habit becomes the largest line in the bill.

The worse case is a loop with no stop condition. It keeps running, and keeps paying, until something outside it stops it. On nights when there is nothing to do, it may still do thirty rounds of nothing.

## How it works

| Driver | Attended session | Unattended run, loop or parallel |
|---|---|---|
| Context re-read | You notice a long session and start fresh | Each iteration re-reads whatever it carries; nobody trims it |
| Cache write | Paid once, then warm while you keep working | Paid again on every cold start, for every run |
| Stopping | You stop when the answer is good enough | Only a stop condition stops it |
| Parallel sessions | One session, one context | Each session builds and re-reads its own conversation |

Separate sessions do not reuse each other's conversation context. Ten parallel sessions are ten contexts, not one context used ten times. Only an identical start, such as the same instructions and tools, may be served from a shared cache, and whether it is depends on the provider.

## Example

The `ticket-service` team's nightly test-triage loop (see [Loops](https://ai-sw-factory.mellicci.dev/fundamentals/loops)) looks at the test results, investigates new failures and reports. It had no budget, and on nights with nothing new it still ran up to 30 iterations, re-reading its context each time. The team added two limits: a maximum of 5 iterations, and an early exit when no test has started failing since yesterday. The figures below are illustrative relative units.

| | Without a budget | Max 5 iterations + early exit |
|---|---|---|
| Quiet night: iterations | up to 30 | 1 |
| Quiet night: tokens (relative) | about 30 units | about 1 unit |
| Busy night: iterations | up to 30 | at most 5 |

On quiet nights the early exit ends the run after one check. On busy nights the cap bounds the worst case, and the team reads the cap as a signal: if a night keeps hitting 5, the task needs a better-scoped run, not a higher number.

## What helps, what hurts

Helps:

- Set a maximum number of iterations or turns, a time limit or a token budget on every unattended run. A budget is a stop condition that does not depend on the model's judgment ([Loops](https://ai-sw-factory.mellicci.dev/fundamentals/loops)).
- Check for new work first and exit early. A run that finds nothing new should cost one check, not a full pass.
- Keep each run's context small and focused. Every iteration re-reads it ([Context growth](https://ai-sw-factory.mellicci.dev/cost/context-growth)).
- Start each headless run with only what it needs, and know that it pays its cache write ([Headless execution](https://ai-sw-factory.mellicci.dev/fundamentals/headless-execution), [Prompt caching](https://ai-sw-factory.mellicci.dev/cost/prompt-caching)).

Hurts:

- A loop with no stop condition, or one that only stops when the model decides it is done.
- Carrying a growing log of past iterations into every new one.
- Running many parallel sessions on overlapping work, so each pays for the same reading.
- Setting a cadence faster than new work arrives, so most runs find nothing.

## Trade-offs

- **Too tight a budget stops useful work halfway.** A run cut off at iteration 5 of 7 leaves work to redo, and an unfinished run that must be redone costs more than one that finished.
- **Early exit can miss work.** If the "anything new?" check is too narrow, a real failure waits until the next night.
- **Small context loses background.** A focused run is cheaper per iteration but may need to rediscover facts a longer context would have held.
- **Parallel is faster, not cheaper.** You trade money for time: several contexts, each read and cached on its own.

## Related

- [Loops](https://ai-sw-factory.mellicci.dev/fundamentals/loops) for stop conditions and progress notes
- [Headless execution](https://ai-sw-factory.mellicci.dev/fundamentals/headless-execution) for runs without a person
- [Context growth](https://ai-sw-factory.mellicci.dev/cost/context-growth) for why each iteration re-reads more
- [Prompt caching](https://ai-sw-factory.mellicci.dev/cost/prompt-caching) for why a cold start pays a write
- [Cache lifetime](https://ai-sw-factory.mellicci.dev/cost/cache-lifetime) for why a gap between runs lets the cache expire

## Key terms

- **Budget** - A hard limit on a run, such as a maximum number of iterations or turns, elapsed time or tokens, that ends it when reached.
- **Early exit** - A check at the start of a run that ends it immediately when there is nothing new to work on.
- **Cost runaway** - A loop that keeps iterating, and paying, because nothing ever tells it to stop.
- **Cold start** - A run that begins with no warm cache, so it pays to write its whole prefix before any reuse.
- **Parallel sessions** - Sessions running at the same time; each builds, re-reads and pays for its own conversation.
