wikillm

Paper · 2026-04-24 · cs.CL, cs.AI, cs.CY, cs.HC, cs.SE

How Do AI Agents Spend Your Money? Analyzing and Predicting Token Consumption in Agentic Coding Tasks

Measures token use of eight frontier LLMs running the OpenHands coding agent on SWE-bench Verified. Agentic runs use far more tokens than code chat or single-turn reasoning, input tokens drive cost, and runs on the same task vary up to 30x. Accuracy saturates with cost. Self-predicted token use correlates weakly (up to 0.39) and is biased low. Author-reported.

LLM agentTest-time scalingLLM self-evaluationagentscoding-agentstoken-costcost-predictionswe-benchprompt-caching

arXivPDFCodeMarkdown12 min read

AI-compiled by claude-haiku-5-5 on 2026-11-08, not yet reviewed by a human. Every claim cites its source; check it when a detail matters.

In plain words

AI coding agents fix software bugs by reading files, running commands and trying edits. Each step adds more text to the conversation, and the whole history goes back to the model on every step. This paper measures where that text, and therefore the cost, goes. It runs eight large language models through one standard coding-agent setup on real GitHub bug reports and records the tokens each run uses.

The main finding is that most of the bill comes from input: the long history the agent keeps resending. Two runs on the same bug can cost very different amounts. Spending more does not reliably buy a better result, and the costlier runs show more repeated file reads and edits. Models also differ in how many tokens they need for similar work.

The authors also ask each agent to estimate its own token use before it starts. These estimates follow the real usage only loosely, and they tend to come out too low. The authors suggest such estimates could serve as a rough early warning for expensive runs, not as an exact price.

TL;DR

  • Eight frontier LLMs run OpenHands on the 500 SWE-bench Verified tasks, four runs per task §2. Agentic coding uses about 3500x the tokens of single-round code reasoning and about 1200x those of multi-round code chat §3 fig. 1. The abstract puts the first gap at 1000x [abstract].
  • Input tokens, not output, drive the cost. In a Claude Sonnet 4.5 case study, cache reads are the largest cost in every phase §5.2 fig. 8.
  • Runs on the same task differ by up to 30x in total tokens §1. Accuracy rises modestly to a mid cost and then levels off §3 fig. 3.
  • Self-prediction correlates weakly with real token use: up to 0.39 for output tokens (Sonnet 4.5), and the models underestimate §6 fig. 10 fig. 11.

Problem

Coding agents are usually billed per token, and the user does not know the total until the task ends. The bill is also owed when the task fails §1. The paper asks whether token use can be predicted before a task runs, so that users, providers and budget alerts can act on it §1.

Method

The study has three parts. First, it collects agent trajectories and analyses where tokens go §2 §3. Second, it compares models on accuracy and token use §4. Third, it breaks one model's cost down by phase and by round §5, and tests self-prediction §6.

  1. Agent and benchmark. OpenHands runs on SWE-bench Verified, real GitHub issues with their repositories and tests §2. The agent works in rounds: the model writes a response, makes a tool call and sees the result. The full conversation history is carried forward unchanged to each later round §2.
  2. Models and runs. Eight models are run, each four times per task: Claude Sonnet 3.7, Sonnet 4, Sonnet 4.5, GPT-5, GPT-5.2, Qwen3-Coder-480B-A35B-Instruct, Kimi-K2 and Gemini 3 Pro §2. Per-task token metrics average the four runs §2.
  3. Baseline tasks. Agentic coding is compared with code reasoning (single turn, no tools) and code chat (multi-turn dialogue) §3.
  4. Cost levels. For each task, the four runs are ranked by token cost and grouped as MinCost, LowerCost, UpperCost and MaxCost §3 fig. 3. Repeated file views and edits are counted per run fig. 4.
  5. Difficulty. SWE-bench Verified's expert time estimates are grouped into "<15 min", "15 min to 1 hour" and ">1 hour". The 1–4 hour and over-4-hour groups are merged, since only three tasks are in the latter §3. Rank correlation with tokens is Kendall tau-b §3.
  6. Model efficiency. Accuracy is plotted against cost. Two subsets are used: tasks solved by every model, and tasks failed by every model §4 fig. 6.
  7. Cost decomposition (Claude Sonnet 4.5). The bill is split into four token types: uncached input, output, cache creation and cache read §5.1 app. B. Each run is cut into five phases, named Setup, Explore, Fix, Validate and Closeout, by the agent's behaviour §5.2 tab. 1. A single trajectory is also traced round by round §5.3 tab. 2 fig. 9.
  8. Self-prediction. The same agent, with its tools, is told to explore the repository and output a JSON estimate instead of a fix app. C.1. The prompt includes one worked example §6 app. C.2. Each model makes three independent predictions per task §6. The estimate has predicted input, output and total tokens, a confidence in [0, 1], and a per-phase breakdown app. C.1.

The cost of one API round, with explicit prompt caching, is:

Pseudocode
# Dollar cost of one agent round, with explicit prompt caching  [app. B]
# Input:  token counts of one API call
# Output: cost in US dollars

def round_cost(prompt, completion, cache_write, cache_read):
    # Uncached input is prompt tokens minus cache reads  [app. B]
    uncached = max(0, prompt - cache_read)
    # Each token type has its own rate per million tokens; values are
    # in the Hyperparameters table  [code: analysis/phase_level_analysis/
    # token_cost_ratio_analysis.py#L42-L47]
    cost = uncached * RATE_INPUT
    # Cache creation is added on top, at the 5-minute write rate
    # [§5.1] [app. B]
    cost += cache_write * RATE_CACHE_WRITE
    # Cache reads are billed at the discounted read rate  [app. B]
    cost += cache_read * RATE_CACHE_READ
    # Generated tokens  [app. B]
    cost += completion * RATE_OUTPUT
    return cost / 1_000_000

Self-prediction and its scoring:

Pseudocode
# Self-prediction of token use before execution  [§6] [app. C]
# Input:  one task description and the coding agent with its tools
# Output: predicted input, output and total tokens; prediction cost

def predict_tokens(task, agent):
    # The agent may read files and run commands but must not edit  [app. C.1]
    # It must finish by calling the finish tool with a JSON estimate  [app. C.1]
    estimate = agent.run(task_prompt(task))
    # Parse the JSON fields the prompt asks for  [app. C.1]
    prediction = parse_json(estimate.finish_message)
    # The cost of the prediction run itself  [§6]
    prediction_cost = round_cost_total(estimate.usage)
    return prediction, prediction_cost

# Three runs per model; Pearson r per run, then the mean over runs
# [§6] [code: self_prediction_analysis/calculate_correlations.py#L158-L166]
def score(predicted_by_run, actual):
    # One correlation per run index, between predicted and real tokens
    run_corrs = [pearson(p, actual) for p in predicted_by_run]
    return mean(run_corrs)

Hyperparameters

NameValueWhereNotes
Agent frameworkOpenHands§2
BenchmarkSWE-bench Verified, 500 tasks§2 §6
Trajectory runs per task4§2metrics averaged over runs
Self-prediction runs per model3§6"due to budget constraints"
ModelsSonnet 3.7, Sonnet 4, Sonnet 4.5, GPT-5, GPT-5.2, Qwen3-Coder-480B-A35B-Instruct, Kimi-K2, Gemini 3 Pro§2Gemini listed as "Gemini 3 Pro Preview" in §7; exact API ids not stated
PhasesSetup, Explore, Fix, Validate, Closeout§5.2 tab. 1defined by behaviour; labelling rule not stated
Phase share of rounds9.98%, 30.37%, 33.53%, 16.59%, 9.53%tab. 1Sonnet 4.5 runs, all rounds
Difficulty bins"<15 min", "15 min to 1 hour", ">1 hour"§31–4 h and >4 h merged; three tasks >4 h
Cost levelsMinCost, LowerCost, UpperCost, MaxCost§3 fig. 3the four runs of one task ranked by tokens
Rank statisticKendall tau-b§3
Shared subsetssuccess n=230 (all models solve); failure n=100 (all fail)fig. 6of 500 tasks
Cache write rate5-minute write rate§5.1
Rate, input (code)$3.00 per million tokenstoken_cost_ratio_analysis.pynot stated in the paper text
Rate, cache write (code)$3.75 per million tokenstoken_cost_ratio_analysis.pylabelled "1h cache" in code, see Paper vs code
Rate, cache read (code)$0.30 per million tokenstoken_cost_ratio_analysis.py
Rate, output (code)$15.00 per million tokenstoken_cost_ratio_analysis.py
GPT-5 prices (paper)input $1.250, cached input $0.125, output $10.000 per millionapp. Bcited to an OpenAI page
GPT-5 cached discount in formulacached tokens billed at 0.2 x input rateapp. Bdoes not match the listed prices, see Pitfalls
Self-prediction output fieldspredicted input, output and total tokens; confidence in [0,1]; breakdown by phaseapp. C.1
Worked example in promptone example, predicting 1,250,000 input and 18,500 output tokens for a different issueapp. C.2 swe_token_estimate.j2
Prediction metricsPearson r of predicted vs actual tokens; overhead = prediction cost / task cost§6
Regressionregression on cost level with model as a term; coefficients vs MinCostfig. 3 fig. 4estimator and random effects not stated
Contextfull history carried forward unchanged§2
Max rounds, step budgetnot stated
Random seeds, sampling temperaturenot stated

Results

Author-reported. Setup: OpenHands on SWE-bench Verified (500 tasks), eight models, four runs per task §2.

QuantityValueSetupSource
Agentic vs single-round reasoningabout 3500x tokensaverage over tasks§3 fig. 1
Agentic vs multi-round chatabout 1200x tokensaverage over tasks§3 fig. 1
Headline gap, abstract1000x tokensvs code reasoning and code chat[abstract]
Gap between most and least expensive taskabout 7M tokens500 tasks, mean over runs and modelsfig. 2
Most vs least expensive run, same taskabout 2x (average); up to 30x in total tokensper model, across 500 tasksfig. 2 §1
Accuracy vs costrises modestly to LowerCost, then saturatesrun quartiles per taskfig. 3 §3
Excess tokens on failed tasksGPT-5 and GPT-5.2: under 0.5M more; Kimi-K2: about 2M moreshared-failure subset, n=100§4
Model gapKimi-K2 and Sonnet 4.5 use over 1.5M more tokens than GPT-5 on averageall tasks; gap holds on the shared-success subset§1 §4 fig. 6
Difficulty vs tokensKendall tau-b = 0.32expert time bins§3
Tasks against the difficulty order6.7% of "<15 min" tasks use more than the average ">1 hour" task; 11.1% of ">1 hour" use fewer than the average "<15 min" task§3
Self-prediction, output tokensPearson r up to 0.39 (Sonnet 4.5)3 predictions per model§6 fig. 10
Self-prediction, input tokensKimi-K2 highest, 0.38§6 fig. 10
Self-prediction overheadSonnet 4.5: 0.32x task cost; GPT-5.2: under 6%; Sonnet 3.7 and Sonnet 4: over 2x§6 fig. 10
Direction of errormost points below the diagonal for every model; input most underestimated§6 fig. 11
Without the worked exampleSonnet 4.5 and GPT-5.2 only; correlations fall, underestimation remains2 modelsapp. D tab. 3

Without the worked example, correlations of the two models that still followed the format were:

ModelTokenCorr. with ground truthCorr. (abs. error, task cost)Corr. (pred. cost, task cost)
Sonnet-4.5Input0.13550.11550.1185
Sonnet-4.5Output0.1229-0.45630.1185
GPT-5.2Input0.17960.22430.2461
GPT-5.2Output0.21300.08220.2461

Source: tab. 3 app. D

Paper vs code

Official code: the project page the paper links points to LongjuBai/agent_token_consumption_analysis, owned by the first author. It was cloned at commit 335ecf2. The repository has scripts and prompt templates only; its README says it does not track generated CSV or PNG results README.md. There is no LICENSE file, so the code is cite-only and nothing is quoted from it.

The project page also links a Hugging Face dataset of trajectories (loong0814/openhands_trajectories). It was not fetched or license-checked for this card.

I searched GitHub for agent_token_consumption; the author's second repository, LongjuBai/agent_token_consumption, returned a 504 and was not read.

  • Pricing constants. The code uses $3.00 input, $3.75 cache write, $0.30 cache read and $15.00 output per million tokens token_cost_ratio_analysis.py. The paper states none of these values in its text. It cites a pricing page §5.1 and says output costs about 80x cache reads §5.2. The code's rates give 15 / 0.30 = 50x, not 80x.
  • Cache-write label. The constant is named cache_write_1h_rate with a comment reading "1h cache" token_cost_ratio_analysis.py. The paper says the 5-minute write rate is used throughout §5.1. The code's value, $3.75, is the one used; the label and the paper's stated rate disagree, and the paper does not give the value.
  • Uncached input. The code subtracts cache reads from prompt tokens and charges cache writes separately token_cost_ratio_analysis.py. This matches the paper's formulas app. B.
  • Phases are positional in the code. divide_into_phases cuts each run's rounds into five equal-count bins by round index. The bins are named early, early_mid, mid, later_mid and later token_cost_ratio_analysis.py token_cost_ratio_analysis.py. The paper's Setup, Explore, Fix, Validate and Closeout phases are labelled by behaviour §5.2. No labelling code is in the repository, so tab. 1 and the phase figures cannot be reproduced from it.
  • Difficulty statistics. The repository has no Kendall tau-b computation. The difficulty script produces box plots multi_model_difficulty_box_plots.py. The tau of 0.32 and the 6.7% and 11.1% figures cannot be reproduced from the release. The figure caption mentions a bootstrap interval, but that text is commented out in the source §3 fig. 5.
  • Cost grouping. Before ranking tasks by cost, the code clips per-task cost at the 1st and 99th percentiles multi_model_group_accuracy_analysis.py. The paper does not mention clipping. The per-task MinCost to MaxCost table is read from cost_level_accuracy_chart.csv, which a script not in the repository produces multi_model_cost_level_accuracy_plots.py.
  • Regression form. The regression formula in the code has model as a fixed term and no random effect multi_model_cost_level_accuracy_plots.py. The paper calls its analysis mixed-effects fig. 3 fig. 4.
  • Self-prediction scoring. The code computes Pearson r separately for each of the three runs, then averages the three calculate_correlations.py calculate_correlations.py. The paper does not say whether it pools or averages. The code also reports correlations on log-transformed values, which the paper does not describe calculate_correlations.py.
  • Overhead. The code's overhead is the mean of per-run ratios of prediction cost to task cost calculate_correlations.py. This is not the ratio of means.
  • Missing task costs. For Qwen3-Coder, a missing task cost is filled from a different CSV, dataset/qwen/swe_bench_token_cost_aggregated_total_with_accuracy.csv calculate_correlations.py calculate_correlations.py. The paper does not mention this.
  • Prompt. The template's phase list and finish fields match the appendix swe_token_estimate.j2 swe_token_estimate.j2. The template includes warning-symbol lines that the printed prompt does not. I did not compare the system prompt line by line.

Pitfalls

  • Headline multipliers conflict. The abstract gives 1000x over code reasoning and code chat. The body gives 3500x and 1200x [abstract] §3. The source does not explain the difference.
  • Phase breakdown is not reproducible. The five phases are defined by behaviour, but the released code only makes five equal-count bins by round index token_cost_ratio_analysis.py. Treat tab. 1 as the paper's labelling, which an implementer cannot re-run.
  • Price ratio. The text says output costs about 80x cache reads §5.2. At the code's rates the ratio is 50x token_cost_ratio_analysis.py. Use the rates you have for your provider, not either number.
  • Cache-creation double count. The formula charges cache creation on top of prompt tokens minus cache reads app. B token_cost_ratio_analysis.py. If the provider's prompt count already includes cache-creation tokens, they would be billed twice. The paper does not say which convention the API uses.
  • GPT-5 cached price. The formula bills cached tokens at 0.2 times the input rate. The listed prices, $0.125 cached against $1.250 input, give 0.1 app. B. The two disagree within the appendix, so check the provider's price list before using either.
  • Cost depends on the price list. Dollar shares depend on prices that change over time. The cache-read result is for Claude Sonnet 4.5 at the prices the paper used, which it links to a pricing page §5.1 §5.2 fig. 8.
  • The worked example anchors the estimate. The prompt's example predicts 1,250,000 input tokens and 18,500 output tokens for a different issue app. C.2. The paper argues that underestimation is not caused by the example app. D. That check covers only two models, and correlations drop without the example.
  • Few models follow the no-example prompt. The paper says most models did not follow the instruction without the example, so the no-example results cover only Sonnet 4.5 and GPT-5.2 app. D.
  • Self-prediction is weak. The best output correlation is 0.39, and Kimi-K2's best input correlation is 0.38 §6. The paper says the estimates are not usable for instance-level pricing §6 §7.
  • Failure cost is model-specific. The extra tokens spent on failed tasks differ a lot by model: GPT-5 and GPT-5.2 under 0.5M, Kimi-K2 about 2M §4. A budget alert calibrated on one model will not transfer to another.
  • Per-model numbers are in figures. The text gives only some correlations and overheads. The rest are in fig. 10 and fig. 11, which are not transcribed here. Read the figures before quoting per-model values.
  • Generalisation. One benchmark, one agent framework, and eight models §7. The paper says the patterns held across the models it tested, not beyond them.
  • Version. This card follows arXiv v3, updated 2026-10-02 [raw metadata]. Earlier versions may differ.

Lineage

  • Concurrent work: token distribution across multi-agent systems and token pricing in reasoning models, both cited as parallel work §1.
  • Related: inverse test-time scaling, where longer reasoning does not improve accuracy, is the context for the accuracy-cost finding §3.
  • Related: self-feedback and language-model calibration, cited as grounding for self-prediction §6.
  • Builds on: OpenHands, the agent framework §2; SWE-bench Verified, the benchmark §2.
  • Points to: budget-aware tool-use policies, cited as a way to enforce runtime token limits §7.

Source and license

Adapted from “How Do AI Agents Spend Your Money? Analyzing and Predicting Token Consumption in Agentic Coding Tasks” by Longju Bai, Zhemin Huang, Xingyao Wang, Jiao Sun, Rada Mihalcea, Erik Brynjolfsson, Alex Pentland, Jiaxin Pei (arXiv:2604.22750), used under CC BY 4.0. Changes: condensed and restructured for implementation, with tables, equations and algorithms taken from the paper and its code, each cited to its source. Not affiliated with or endorsed by the authors. Code citations refer to https://github.com/LongjuBai/agent_token_consumption_analysis at commit 335ecf2a2960 (none (no LICENSE file in repository)). Any quoted code is shown under that license.