AIエージェントのループに隠されたO(N²)の課金 — 測定とベンチマークMost AI agents resend their full transcript each loop iteration, causing O(N²)…
匿名の公開いいねです。記事の保存・お気に入りではなく、Featured、Top 3、重要度、掲載順位には影響しません。仕組みとプライバシーAnonymous public likes are reactions, not saved articles or bookmarks. They do not affect Featured, Top 3, importance, or listing order.How it works and privacy
AIエージェントはループのたびに全トランスクリプトを再送信するためトークン消費がO(N²)に膨らむが、コンパクトなメモリ方式に切り替えると62.8〜85.9%のコンテキストトークン削減が実測で確認された。
Most AI agents resend their full transcript each loop iteration, causing O(N²) token costs; benchmarks show that compact memory strategies can cut context tokens by 62.8–85.9% in multi-session tasks.
要約と収集メタデータをもとに生成した AI 解説本文です。元記事全文の転載・翻訳ではありません。This AI explainer is generated from the summaries and collected metadata, not from a reproduction or translation of the full source article.
AIエージェントは動作ループを一巡するたびに、それまでの会話履歴(トランスクリプト)を丸ごと再送信する設計が一般的だ。この方式はループが長引くほどトークン消費が急増し、計算量の目安でいえばO(N²)に近い膨張を招く。Qiitaに公開されたベンチマーク記事は、履歴を圧縮した「コンパクトなメモリ」を呼び出す方式へ切り替えることで、実際の複数セッションタスクにおいてコンテキストトークンを62.8〜85.9%削減できたと、実測結果とともに報告している。
なぜO(N²)になるのか。エージェントは「観察・推論・行動」を繰り返し、各ステップで大規模言語モデル(LLM)を呼び出す。このとき毎回、過去のやり取りをすべて入力に含めると、1回目は短い履歴で済む一方、N回目にはN回分の履歴を送ることになる。総トークン量は各回の履歴の合計、つまりおおむねNの二乗に比例して増えていく。ステップ数が数十から数百に及ぶ長いタスクほど、この差は無視できない規模になる。
コンパクトなメモリ方式は、全文をそのまま持ち越すのではなく、要点を圧縮した状態で保持し、必要に応じて呼び出す。これにより毎回の入力に載せるコンテキスト量を抑え、同じタスクをより少ないトークンで完了できる。記事が示す62.8〜85.9%という幅は、タスクの性質や履歴の長さによって削減効果が変動することを示唆していると見られる。
背景として、LLMのAPI利用料は入力トークン数に比例する課金体系が主流であり、エージェントを長時間・多段で動かすほど、トランスクリプトの再送信によってコストが膨らみやすい。この課題への対処として、会話の自動要約やコンテキスト圧縮、長期記憶の外部化といった手法が各所で模索されている。今回のように削減効果を測定・ベンチマーク化する取り組みは、感覚的なコスト最適化を定量的な指標へ落とし込む点で意義がある。
ただし、履歴を圧縮すれば情報の一部が失われるトレードオフも伴うため、タスクの正確性や再現性への影響を含めた評価が今後の焦点になりそうだ。エージェント開発が広がる中、トークン効率の設計は運用コストと直結するテーマとして、引き続き注目される可能性が高い。
Autonomous coding and reasoning agents have become a standard way to automate multi-step work, but a recent benchmark highlights a cost pattern that many teams overlook: most AI agents resend their entire conversation transcript on every loop iteration, which drives token consumption to grow quadratically, or O(N²), as a task gets longer. For anyone paying per token, this matters because both the bill and the latency can balloon well before an agent finishes a complex, multi-session job.
The mechanism is straightforward. An agent works by repeatedly calling a language model in a loop: it sends the current context, receives an action or a piece of reasoning, executes it, appends the result, and calls the model again. Because most large language models are stateless between calls, the conventional way to preserve continuity is to include the full running transcript each time. If each step adds roughly a fixed amount of text, then step one sends a short prompt, step two sends that plus the new content, and so on. Summed across N steps, the total tokens processed scale with the square of the number of steps. That quadratic curve is why the analysis frames the problem as O(N²) rather than a linear cost.
The reported measurement compares that naive resend approach against a compact memory strategy, in which the agent stores a condensed representation of prior work and recalls only what is relevant instead of replaying everything. According to the benchmark, calling compact memory in realistic multi-session tasks used 62.8 to 85.9 percent fewer context tokens than full retransmission. The write-up presents the measurement results, the method used to obtain them, and an offline component, which suggests the comparison was run under controlled conditions rather than inferred from theory alone.
The compact memory idea is not new in spirit, but the contribution here appears to be quantifying the savings on concrete tasks. Memory in agent frameworks typically takes a few forms: summarizing older turns into shorter notes, storing facts in an external store such as a vector database and retrieving them on demand, or maintaining a structured scratchpad that captures decisions and state without every intermediate token. Each approach trades some fidelity for a much smaller working context, and the benchmark's wide range likely reflects how different task types and memory designs recover different amounts of that overhead. Multi-session workloads, where an agent returns to a project across separate runs, are an especially clear case, because a fresh session would otherwise reload a long history that a compact memory can compress.
This tension is relevant to agentic coding tools such as Cursor, which is the context in which the analysis was published. Editors and assistants that iterate over a codebase run exactly the kind of loops described here, accumulating file contents, diffs, test output, and tool results across many steps. As those transcripts grow, the effective context sent per iteration grows with them, so the choice between replaying everything and recalling a summary has a direct effect on cost and responsiveness.
It is worth noting that the industry already offers partial mitigations that operate alongside memory design. Prompt caching, offered in various forms by major model providers, lets repeated prefixes be reused so that resending a long transcript is cheaper than paying full price each time. Longer context windows reduce the risk of truncation but do not by themselves flatten the O(N²) curve, since the tokens still have to be processed on every call. Compact memory addresses the growth at its source by keeping the working context small, which is a different lever than caching or window size, and the approaches can be combined.
Readers should treat the specific 62.8 to 85.9 percent figures as results from one benchmark rather than a universal guarantee, since outcomes depend on the tasks, the memory implementation, and how much of the transcript is genuinely needed for correct behavior. Aggressive compaction can drop details an agent later requires, so the practical goal is usually to balance token savings against reliability. Even so, the underlying point is well grounded: naively resending the full transcript scales poorly, and measuring the alternative gives teams a concrete basis for deciding when a memory layer is worth the added engineering complexity. For cost-sensitive deployments running long or repeated agent sessions, that measurement is a useful starting point for optimization.
本ページの本文と要約は AI による自動生成です。日本語版と英語版は言語ごとに独立して生成されるため、表現や詳しさが異なる場合があります。正確性は元記事 (qiita.com) をご確認ください。The body and summaries are AI-generated independently for each language, so wording and detail may differ. Verify accuracy at the original source (qiita.com).





