Defending AI Agents
עברית Join waitlist
Menu

E3 · At scale: the same loop, ten thousand times at once

At scale: the same loop, ten thousand times at once

On a laptop the loop has one user, one model server and twenty tasks. In production it has thousands of concurrent runs that share a rate-limited API, a bill and an on-call engineer. Each piece of this module changes character at that scale. The estimates below start from the laptop measurements in the build. Replace them with your own.

Tokens become the budget line

The study gives a mean number of tokens per run (the build's table). At a million tasks a day, multiply. A loop that averages 20,000 tokens a task sends 2 \times 10^{10} tokens a day. At 1 per million prompt tokens and 5 per million completion tokens, with completion at a tenth of the total, that is about 2 \times 10^{10} \times (0.9 \times 1 + 0.1 \times 5)/10^6 = 28{,}000 a day. Lesson 6's quadratic growth is the lever. Halving the steps of a ten-step task roughly quarters its history tokens, so a change that saves one step in the median task is worth more than a cheaper model for many workloads. Per-tenant budgets follow: the same Budget with a price, enforced per customer per day by whatever runs the loop, and reported with the stop reason.

Retries become a fleet behaviour

Lesson 5's herd is one incident. At scale the herd is the normal shape of every provider blip. Three rules carry over unchanged, and two are new.

  • Full jitter, and honour Retry-After with spread (problem 7).
  • A retry budget per client, not only per call. Google's SRE book retries a request only while retries are under 10% of the client's recent requests, on top of a per-request limit of three attempts (Beyer et al., 2016, "Handling Overload"). Past that, the server is not having a blip, and retrying makes it worse. The same chapter's adaptive throttling makes the client reject requests locally before they reach an overloaded server.
  • Classify before retrying, so that a bad deploy of your adapter does not quadruple the load on someone else's API.
  • New: retrying a model call is safe; retrying a tool call is not. The model call is a read. A send_email that timed out may have sent the e-mail. Consequential tools need an idempotency key, sent with the call and checked by the tool, so that a retry is recognised as the same action. The lab's tools have none, and Module 21 lesson 7 shows the general problem: some loops must not run twice.
  • New: the model is shared. A fleet behind one provider shares that provider's rate limit. E10 puts a model gateway in front of it, with queuing, fallback to a second model and caching. Most of this module's retry logic then moves into that gateway, and the agent sees fewer, slower failures.

Parallel calls meet connection limits

A pool of four per run times ten thousand runs is forty thousand concurrent tool calls. The constraint moves from the loop to the tools: each backend has a connection limit and a latency curve, with a knee: the load past which latency climbs steeply (Module 21 lesson 2). A per-tool concurrency limit shared across runs is the scale version of max_workers.

Formats become a matrix

Two providers and three local servers need two adapters: the local servers all speak Chat Completions, the loop's own format. They still need five sets of recorded shapes, one per server, because servers that share a format differ in what they send (lesson 8's whole and fragmented calls). A provider's format change shows up as a failing adapter test in CI or as a production incident, and which one depends on whether those tests exist. Teams that run several providers usually standardise on one internal format, as this loop does, and keep the adapters in one library with its own release notes.

Stop reasons become metrics

Result.stop is a categorical outcome per run. At scale its distribution per hour is the first dashboard: the share of answer, of each limit, of error and of schema. A rise in tokens after a prompt change, or in error after a provider release, is visible within minutes. E11 turns it into traces and alerts. Module 18 reads the same distribution for a security reason: a run that hits its step limit while calling send_email repeatedly is an incident, not a cost.

What the laptop cannot show

The laptop has one model at a time, no contention and no adversary. It cannot show correlated failures across a fleet, provider rate limits, the tail latency of shared backends, or what an attacker does with a loop that retries what it is told to. The last is the rest of this course.