Module 095 min

Production: money, safety, sleep

A working demo and a system you can leave running overnight are separated by four things: cost control, observability, failure handling, and permissions.

After this module
  • You can estimate what an agent costs before you build it, and know what to log when it misbehaves.
  • You know the seven reliability patterns that keep a long-running task from becoming an incident.

9.1Where the money goes

For most agent systems, tokens dominate the bill — not because any single call is expensive, but because every iteration resends the context and tasks run for many iterations.

TYPICAL COST COMPOSITION · AGENT SYSTEMS 0 20% 40% 60% LLM tokens Compute Storage Network Operations 40–60% 20–30% 5–10% 5–10% 5–10% SHARE OF TOTAL COST · TYPICAL RANGE
One lever moves the biggest bar. Model selection, prompt caching and context discipline beat any amount of server tuning.
Try it · cost calculatorillustrative prices
16,000
800
200
Per task$0.0000
Per month$0.00

Remember that an agent task can be 5–20 model calls, each resending a context — multiply accordingly. Prices change; plug in current ones before promising a number to anyone.

9.2If you can't see it, you can't fix it

An agent decision is invisible unless you record it. The minimum viable observability is a trace per task: every model call with its input token count, every tool call with its arguments and result, every permission decision, and the total cost and duration.

ONE TASK · TRACE WATERFALL (SCHEMATIC) task #4821 plan search_flights read_policy model.think book_flight retry (timeout) answer 0s 14s ↑ needs approval ↑ cost spike
The trace is the debugging interface. Two of these spans — the approval gate and the retry — are where this task's money and risk actually went.

Logs answer "what happened"; metrics answer "how often"; traces answer "why this one". For agents you need all three, plus one thing most people forget: store the exact context sent on a failing call. Without it, reproducing a failure is guesswork, because the prompt is regenerated on every run.

9.3Seven patterns that keep it running

PatternWhat it preventsImplementation sketch
Timeouts everywhereA hung tool freezing the whole taskPer-tool timeout, plus an end-to-end deadline propagated through the loop
Retry with backoff + jitterThundering herd; retry storms2–3 attempts, exponential delay, jitter, only on transient error codes
Idempotency keysDouble-charging, duplicate emailsClient-generated key per intended action; the downstream service dedupes
CheckpointsLosing 40 minutes of work to a crashSerialise state after each turn; resume from the last good checkpoint
Circuit breakerHammering a dead dependencyAfter N failures, fail fast for a cool-down period
Fallback modelProvider outage taking you downSecond provider behind the same interface; degrade quality, keep serving
Dead-letter queueLosing tasks that failed permanentlyPark the task plus full trace for human review instead of dropping it

9.4The security lines that matter

The trap · the demo-to-production gap

Costs don't scale linearly with users. They scale with retries, context growth, and the long tail of hard cases. The user who pastes a 200-page contract costs a hundred times the user who asks a question. Cap per-task spend from day one, or your first viral day is also your first invoice.

9.5When a run goes wrong

The first question in an agent incident is not "which model" or "what prompt". It is which turn. A run is a sequence of decisions, and almost every failure is visible in the trace if you read it in order rather than jumping to the error.

Five checks, in order. Each one either clears a cause or names it.
CheckWhat it rules outWhat you are looking for
Where it first divergedEverything downstream of the first bad turnThe last turn whose state still matches the goal
The exact context sentPrompt-assembly bugs, stale memory, dropped instructionsWhether the constraint was actually in the prompt at that moment
The tool result it sawMisreading, truncation, silent empty responsesAn error string, an empty body, or 200k of text where 2k was expected
Permission decisionsLoops caused by denial and retryThe same action attempted repeatedly with the same refusal
Cost and latency by span"It was slow" as a vague complaintOne span holding most of the time or money — usually a retry

Two patterns cover most real incidents. The first is a silent tool failure: the API returned an error object the model treated as data, so it proceeded confidently from a false premise. The second is a loop that looks like work: the agent keeps taking a plausible action, receiving a refusal or an unchanged result, and trying again. Both are cheap to detect in a trace and nearly invisible in a chat transcript.

Whatever you find, the run should end with one addition: a check that fails next time. An eval case built from the failing trace is worth more than a runbook entry, because it runs on every future change instead of waiting for a human to remember.

Check yourselfQuestion 1 of 1

Which number should you put on a dashboard first?

60-second recap

  • Tokens are the bill — and retries plus growing context are what make it surprising.
  • Trace every task: model calls, tool calls, permission decisions, cost, duration, and the exact context of failures.
  • Assume failure: timeouts, backoff, idempotency, checkpoints, circuit breakers, fallbacks, dead-letter.
  • Least privilege, no secrets in context, egress allowlist, audit trail, per-task budget.