ai newsMi., 25. Nov.
EilmeldungCan Claude’s storage webhooks miss an hour-long outage?

AI agent tokens: why your agent re-sends its history on every call

Your coding agent's input token count can be 5,000 times a chat. The tokens went to re-sending history, and the fix is to send less of it. Why the total grows close to N squared A model keeps no memory between calls. Each call computes over the system prompt, tool definitions and messages in its co

dev.toDienstag 14:37 · 24.11.2 Min. Lesezeit
Bild: dev.to

Posted on Nov 24 • Originally published at systemdesignsimple.com

Your coding agent's input token count can be 5,000 times a chat. The tokens went to re-sending history, and the fix is to send less of it.

A model keeps no memory between calls. Each call computes over the system prompt, tool definitions and messages in its context, and nothing else. A multi-turn conversation exists because earlier turns are put back into that context on every call, either uploaded again by the client or rebuilt by the service from stored state. So call k takes everything before it as input, and with roughly constant increments the total input across N calls grows close to N squared. Server-side conversation state saves upload bytes but does not shrink the logical context: the provider still bills all previous input tokens in the chain.

We built a toy coding agent in our lab, a synthetic simulation with estimated tokens, not a billed run. One chat sends 158 estimated input tokens. One 30-call agent task sent 863,762 estimated input tokens, 5,467 times one chat, and 94.0 percent of it was history re-sent from earlier calls. The fixed part, system prompt plus 10 tool schemas plus the task, was 2,230 estimated tokens on every call.

First, shrink tool results before they enter history. In the lab, keeping 100 percent of tool output cost 504 times one chat at 30 calls; keeping 20 percent cost 136 times. Use caps, line ranges and failure-only test output.

Second, turn on prompt caching. The model still receives the full context, so the token count is unchanged, but cached tokens bill at a read price: 82 percent lower illustrative cost in the lab. Put static content first so the prefix stays byte-stable.

Third, clear or summarize old tool results when a task nears the context limit. Clearing caps request size and makes growth closer to linear, but the part after the edit stops hitting the cache, and detail is gone, so clear in large batches.

History is the bill. An append-only loop pays for every past tool result on every future call, so the cheapest agent is the one that decides what to forget. Measure your own re-sent share before you pay for a bigger context window.

Full article with the worked example and cheat sheet: https://systemdesignsimple.com/why-an-ai-agent-re-sends-its-history/

Quelle: dev.to (dev.to). Original öffnen