Why AI Coding Agents Become the Most Expensive Interns

    A practical look at why autonomous coding agents produce unpredictable bills, where their tokens actually go, and how teams can control cost through context engineering, explicit budgets, observability, and verification-first workflows. The article summarizes empirical findings on token consumption in agentic coding tasks and turns them into a four-layer cost governance framework.

    Why AI Coding Agents Become the Most Expensive Interns

    1. The Symptom: One Afternoon, One Three-Figure Bill

    Most teams shipping AI in production have seen some version of this scene: you hand a description of a production bug to an autonomous coding agent, expecting it to read the repository, locate the root cause, patch the code, and run verification while you move on to something else. Hours later you come back, the task status is failed, and the number on the usage dashboard has already climbed into three figures. The money is gone, the work is not done, and you cannot even explain where the money went.

    This is not bad luck for one team. It is a structural outcome of how agents work today. Over the past two years, coding agents with tool-calling and long-horizon planning capabilities have been wired into development workflows, and two complaints show up constantly. First, cost is unpredictable: no credible budget can be given before a task starts. Second, failure is still billed: tokens are consumed during reasoning, and the bill is not discounted when the output is zero.

    To explain either issue, a more basic question has to be answered first: where do an agent’s tokens actually go, and can we predict that before the task starts? The industry has long relied on intuition, with common claims such as “output is more expensive than input, so output is the main cost,” “harder tasks consume more,” “more consumption means better quality,” and “manual difficulty labels plus caching are enough to control cost.” A recent large-scale empirical study, produced jointly by several universities and research institutions, systematically measured and predicted token consumption for agentic coding tasks. Its conclusions largely reject those intuitions. The value of the work is not any single number, but the fact that it offers a reusable measurement methodology and modeling approach that any team can reproduce in its own environment.

    2. Fixing the Measure First: Why “Priced per Token” Is Not “Priced per Unit of Work”

    Settle on a common measure before discussing cost. Under mainstream usage-based pricing, cost is roughly input tokens times their unit price plus output tokens times theirs, and on the input side you usually also distinguish whether a cache hit occurred. There is one asymmetry that is easy to miss: the output unit price is typically much higher than the input unit price, but an agent’s input volume is often one to two orders of magnitude larger than its output.

    The reason is that an agent’s reasoning turns are not independent. Each turn has to carry the system prompt, tool definitions, prior messages, and tool results (file contents, retrieval snippets, command output, stack traces), plus the overhead of multiple round trips. Let the input at turn $i$ be $I_i$ and the output be $O_i$. The total input cost term is approximately:

    1. TotalInputTokens ≈ Σ I_i
    2. where I_(i+1) ≈ I_i + O_i + R_i - Evict_i
    3. R_i is the tool return volume, Evict_i is the context trimming amount

    This recurrence says two things. First, cost grows and compounds roughly with the number of turns, and $R_i$ (reading files, grep, test logs) is usually the fastest-growing term; one uncontrolled read of a large file or a full log is enough to make every subsequent turn more expensive. Second, caching only reduces the unit price of the “repeated prefix” portion and cannot reduce the volume itself. As $R_i$ keeps injecting new content, the share of the context that is cacheable keeps shrinking, and the cache hit rate decays with it.

    The first practical conclusion follows: context engineering is cost management. Capping the size of each tool return (paginated reads, locating by symbol instead of loading whole files, aggregating and truncating logs), moving the stable parts (system prompt, tool schemas, specification notes) to the front to maximize prefix reuse, and compressing history structurally rather than stacking raw text all tend to pay off more than any unit-price-level optimization.

    3. Where the Variance Comes From: Why the Same Task Can Differ by Orders of Magnitude

    The most counterintuitive set of observations in the empirical work is that the decisive driver of consumption is not task difficulty but the agent’s behavioral policy. With the same model and the same task, consumption across runs can differ by more than an order of magnitude, driven mainly by:

    • Number of turns and retry behavior: unconstrained self-correction after a failure turns into repeated trial and error, paying again on every turn with an inflated context.
    • Granularity of tool calls: reading one snippet at a time versus reading an entire directory produces completely different cost structures.
    • Whether the stopping condition is explicit: without a decidable completion criterion, the agent tends to “just double-check a bit more,” and tail turns account for a large share of consumption.
    • Share of failed paths: a substantial portion of tokens is spent in runs that never produce a usable patch, which is pure sunk cost.

    The second counterintuitive conclusion is that consumption and success rate are not monotonically correlated. Past a certain scale threshold, additional consumption yields very little improvement in success rate, and more consumption usually corresponds to unproductive exploration rather than effective reasoning. This means “let it think a little longer” is not a general remedy. A budget cap should be treated as a quality guardrail: a run that exceeds the threshold is more likely to have gone down the wrong path, so terminating it promptly and falling back to a human or a different strategy has a higher expected payoff.

    The third conclusion is that manual difficulty labels have weak predictive power. A task that looks “simple” to a human reading the description has little correlation with how much code the agent actually has to read and how many files it must cross to locate the problem. The features that do predict are static quantities computable from the repository and the task description: the number of files and the volume of code involved, the module boundaries that must be crossed, the reference breadth of the relevant symbols, test coverage, and the turn count and tool return volume in historical execution traces. This makes a pre-task budget range feasible: train a lightweight regression or quantile model on historical run data, take those static features as input, output a quantile range of consumption, and then set quotas and circuit-breaker thresholds from that range.

    4. A Cost Governance Framework You Can Actually Ship

    Organized into engineering practice, the above breaks into four layers.

    Layer 1: Task classification and routing. Do not send every request to the same most expensive configuration. Route by automatically computable complexity features: a single-file local change goes to a small model on a short loop; a cross-module change goes to a strong model with human confirmation; a vague requirement enters a clarification step instead of starting work immediately.

    1. def route(task):
    2. score = 0.6 * task.affected_files + 0.4 * task.cross_module_hops
    3. if task.spec_clarity < 0.5:
    4. return "clarify_then_plan"
    5. if score < 3:
    6. return "small_model_short_loop"
    7. return "strong_model_with_human_gate"

    Layer 2: Budget as a first-class citizen. Make the budget an explicit parameter that constrains both tokens and turns, do cumulative accounting and a prediction update after every turn, and on hitting a threshold terminate gracefully while emitting whatever partial results exist (localization conclusions, a list of suspects, reproduction steps) instead of returning only “failed.” This converts sunk cost into reusable intermediate artifacts.

    Layer 3: Observability. No measurement, no governance. At minimum, record per run the turn count, input/output/cache-hit tokens, tool call types and return volumes, termination reason, and whether verification ultimately passed, and feed that into your logging service and monitoring and alerting system for trend and anomaly detection. What a team should review in its weekly meeting is the marginal cost per successful fix and the share of consumption coming from failed runs, not the total bill.

    Layer 4: Verification first, closed loop. Get the agent in front of executable verification signals (unit tests, static checks, type checks) as early as possible. Making “run the tests once” a low-cost way to correct direction significantly compresses the tail turns of unproductive exploration. At the same time, accumulate each run’s trace as a dataset used to iterate on the prediction model and routing policy above, closing the loop.

    5. Conclusion

    An agent’s bill feels like a blind box because we infer the behavior of a system governed by context volume, turn policy, and failure rate using a human intuition about task difficulty. Once that is corrected, the action path is clear: treat context as a budget, treat budget and circuit breaking as runtime constraints, treat observability as infrastructure, and treat verification signals as the steering wheel. With those four in place, a coding agent can carry stable capacity in the development workflow instead of acting as an expensive intern who bills by the hour and may hand in a blank sheet at any moment. If a company already has a unified container platform, logging service, and monitoring and alerting system, these governance capabilities can be implemented by reusing the existing infrastructure rather than building a separate system; the support Baidu AI Cloud provides for this kind of general capability is mainly the combined use of these standard component categories.