Loop Instrumentation
Produces an event schema, a small set of derived metrics, and thresholds calibrated from your own data.
An event schema for agent sessions, a small set of derived metrics, and thresholds calibrated from your own data rather than borrowed from a publication.
Before raising any deployment above A1, and in the first thirty days of any program regardless of stage. It is the cheapest diagnostic available and most organizations have none of it.
25.1 The Minimum Event Set #
Six events. Anything less cannot answer why a run cost what it cost or why it stopped.
| Event | Required fields | Answers |
|---|---|---|
session.start | agent identity, autonomy tier, work class, model and version, invoking human, task reference, scope | Who authorized this, against what, under which model |
turn.complete | turn index, input tokens, output tokens, elapsed, tool calls issued | Where cost accumulates within a run |
tool.call | tool name, target, parameters hash, outcome, duration | What the agent actually did, and what was denied |
context.compact | turn index, tokens before and after | When instructions may have been summarized away |
session.end | typed termination reason, total cost, turns, artifacts produced | Why it stopped, which is the highest-value field in the schema |
escalation | trigger, turn index, resolution | Whether the agent can and does decline |
Termination must be typed. Published taxonomies distinguish success, maximum turns exceeded, budget exceeded, execution error, and structured-output retry exhaustion.130 Add two of your own: halted by policy, and aborted by anomaly detection. An organization recording only “the agent finished” has discarded its best diagnostic.
Record the compaction boundary. As the context window fills, older messages are replaced with a summary, and the vendor documentation warns explicitly that “specific instructions from early in the conversation may not be preserved.”130 If a session compacted and then violated a rule you thought was in force, the boundary event is the explanation.
25.2 Derived Metrics #
Termination distribution. The share of sessions ending in each reason, trended. A population that is entirely success means the taxonomy is not wired up, not that nothing fails.
Cost as a distribution, never a mean. Runs on the same task can differ by up to thirtyfold in total tokens, so budget against a percentile.154 Report p50 and p95 cost per task by work class. Two further findings make the mean actively misleading: accuracy often peaks at intermediate cost and saturates above it, and neither the model nor a human expert can forecast a task’s cost in advance, with model self-predictions correlating only up to 0.39 and systematically underestimating.154
Cost per accepted change. Total spend divided by changes that merged. This is the only cost metric that survives contact with a CFO, because it prices the failures too.
Turn index at termination, split by outcome. Failed trajectories are consistently longer and show higher variance than successful ones.158 Once you have your own distribution, a long-running session is a signal rather than a curiosity.
Marginal turn cost. Vendor modeling of a fifty-turn session puts input at roughly 5,000 tokens per turn for turns one through ten, 20,000 for turns eleven through thirty, and 35,000 for turns thirty-one through fifty.159 Cost per turn rises roughly linearly with turn index, which means an agent that is going to fail is cheapest to stop early. Verify the shape against your own sessions rather than adopting the vendor numbers.
25.3 Early-Abort Detection #
State this plainly to your team: there is no published detection heuristic with a measured false-positive rate, and no published runaway-loop threshold. What follows is reasoning from measured failure structure, and it should be deployed with instrumentation rather than confidence.
The structure it reasons from is well measured. In failed trajectories the decisive error occurs at a median of step 7 out of roughly 27, the recovery window is a median of one step, observable signals of the failure surface roughly ten steps later, and 82 percent of failures show no termination after the error locks in.117 The agent keeps working, productively-looking, for the remaining twenty steps. That is where cost is spent after the task is already lost.
Candidate abort signatures, in rough order of confidence:
N consecutive turns with tool calls but no net change to the working tree.
The same file edited back and forth across turns, or the same command reissued with trivial variation.
The proportion of tool calls returning errors climbing across a moving window.
Repeated policy denials, which indicate an agent probing for a way around a boundary.
Spend accelerating without artifact production.
Set thresholds from your own successful-session distribution, abort at a percentile you can defend, and measure what aborting cost you by sampling aborted sessions and re-running them to completion. An abort policy without that sampling is a policy that quietly discards good work.
25.4 Two Rules Worth Enforcing in Configuration #
Ceilings on every deployment. Maximum turns and maximum spend are separate controls, and at least one major SDK defaults both to unlimited.130 Default-deny unlimited at the platform level and log every override with an expiry.
Persistent constraints re-injected, not stated once. Anything that must hold for the whole task belongs in a re-injected artifact rather than the opening prompt, because compaction will summarize the opening prompt away. Verify with a test: run a long session, force compaction, and confirm the constraint still binds.
25.5 Failure Signatures #
- Everything succeeds. The termination field is not populated, or is populated by the harness rather than by outcome.
- Cost reported as an average. Hides the thirtyfold spread and produces budgets that are wrong in both directions.
- Telemetry collected and unread. Instrument the ratio of telemetry generated to telemetry analyzed and report it. Recording is not detection.
- Abort thresholds copied from a blog post. There are no published thresholds. Anything you adopt from outside is somebody’s guess about a different codebase.
25.6 What This Rests On #
The event schema derives from published termination taxonomies and compaction behavior.130 The cost findings, the failure-process structure, and the trajectory-length signal are all measured.154,117,158 The abort signatures and thresholds are this framework’s reasoning from that structure, and are explicitly unevidenced.
References cited in this section
5 of 243 · numbering matches the PDF
- 130Anthropic, "How the Agent Loop Works," Claude Agent SDK documentation, accessed August 28, 2026. Vendor documentation; cited as product fact.code.claude.com/docs/en/agent-sdk/agent-loop ↗
- 154Longju Bai, Zhemin Huang, Xingyao Wang, Jiao Sun, Rada Mihalcea, Erik Brynjolfsson, Alex Pentland, and Jiaxin Pei, "How Do AI Agents Spend Your Money? Analyzing and Predicting Token Consumption in Agentic Coding Tasks," arXiv:2604.22750. Preprint.arxiv.org/abs/2604.22750. ↗
- 158Oorja Majgaonkar, Zhiwei Fei, Xiang Li, Federica Sarro, and He Ye, "Understanding Code Agent Behaviour: An Empirical Study of Success and Failure Trajectories," arXiv:2511.00197, October 31, 2025. Preprint.arxiv.org/abs/2511.00197 ↗
- 159Vantage, "The Hidden Cost Driver in Agentic Coding Sessions," April 15, 2026. Vendor modeling, not measurement.
- 117Xiangxin Zhao, Han Li, Shuaiting Li, Tianyi Zhao, Earl T. Barr, Federica Sarro, and He Ye, "Failure as a Process: An Anatomy of CLI Coding Agent Trajectories," arXiv:2607.09510, July 10, 2026. 1,794 trajectories, >63,000 steps, seven models, three scaffolds. Preprint.arxiv.org/abs/2607.09510 ↗