TokenCast:预测LLM代理执行期间的Token消耗
研究论文 · 2026/09/29
当大型语言模型(LLM)代理执行同一任务时,不同运行间的token消耗可能相差一个数量级以上。代理根据工具反馈和中间结果选择下一步,而不断增长的上下文会持续膨胀每次后续调用的输入大小。因此,任务的总消耗在执行前难以预测,且预测必须随着运行展开而修正。本文提出TokenCast,它为每个执行片段学习一种可组合的成本表示,记录其自身消耗及其引入的上下文增长。组合相邻片段可得到累积估计,捕捉早期片段的上下文被后续每次调用重新读取时产生的额外输入成本。随着执行展开,新观察到的证据会刷新预测,无需额外LLM调用,在SWE-bench Verified上平均每次运行的累积预测时间为32.8毫秒。在4个任务套件和6个代理模型上,TokenCast相对于最强对比方法的平均绝对误差降低在96个评估组合中平均为14.5%。在离线预算控制重放中,TokenCast在匹配轨迹完成度下比固定预算策略平均少用21.3%的token。代码见https://github.com/DEFENSE-SEU/TokenCast。
该研究解决了LLM代理执行中token消耗难以预测的关键问题,提出的方法无需额外LLM调用即可实现低延迟、高精度的预测,并在多个任务和模型上验证了有效性,同时展示了在预算控制中显著节省token的潜力,对实际部署具有重要价值。
When a large language model (LLM) agent executes the same task, token consumption can vary by over an order of magnitude across runs. The agent chooses its next steps based on tool feedback and intermediate results, while the growing context steadily inflates the input size of every subsequent call. The total consumption of a task is therefore hard to predict before execution and the prediction must be revised as the run unfolds. In this paper, we propose TokenCast, which learns a composable cost representation for each execution segment, recording its own consumption and the context growth it introduces. Composing adjacent segments yields a cumulative estimate that captures the extra input cost incurred when context from earlier segments is re-read by every later call. As execution unfolds, newly observed evidence refreshes the forecast, requiring no additional LLM calls and incurring a mean cumulative prediction time of 32.8 ms per run on SWE-bench Verified. Across 4 task suites and 6 agent models, TokenCast's mean absolute error reduction against the strongest comparator averages 14.5% over 96 evaluated combinations. In offline budget-control replay, TokenCast uses 21.3% fewer tokens on average than a fixed-budget policy at matched trace completion. The code is available at https://github.com/DEFENSE-SEU/TokenCast.