How to Reduce GLM-5 Token Costs in Long Agent Runs
To cut GLM-5 token costs in long agent runs, summarize completed steps, drop raw tool output after use, and cap the loop length. A 40-step agent re-sends its whole history on every step, so cost grows faster than the task does. These changes often cut the bill in half.
Why Do Agent Runs Get Expensive Fast?
Because the API is stateless. Every step sends the full conversation back. Step 1 sends 1 unit of history, step 40 sends 40. The total is the sum, which is quadratic in step count. A task that looks like “one job” is actually hundreds of billed calls, and the history you carry is most of the payload.
What Are the Five Levers?
- Summarize done steps. Replace a completed step’s raw output with a one-line result before the next call.
- Drop used tool output. Once a search result is consumed, it does not need to stay in context.
- Cap the loop. Set a hard maximum step count so a stuck agent stops instead of spinning.
- Trim tool schemas. Send only the tools relevant to the current phase, not the full catalog.
- Batch read-only calls. Fetch several facts in one step instead of one call each.
The first two do most of the work. History is the biggest part of every request, and it is the part you control.
How Much Can You Actually Save?
| Lever | Typical effect |
|---|---|
| Summarize steps | Large cut to history size |
| Drop used output | Moderate |
| Cap the loop | Stops worst cases |
| Trim schemas | Small per-call |
| Batch reads | Fewer steps overall |
The numbers depend on your step count and output sizes, so measure your own runs. But the direction is reliable: shorter history means a smaller bill on every remaining step.
What Breaks If You Over-Trim?
The agent forgets why it took an action and repeats work. Summaries must keep the decision and its result, just not the raw payload. If you strip too hard, the model loses the thread and loops on a problem it already solved – which costs more than the tokens you saved. Keep the “what happened and what it means” line, drop the “here is the full response body” blob.
Does This Apply to Other Models?
Yes. Any stateless agent loop has this shape, whether on GLM-5 or a coding model like Grok Code Fast. The levers are model-agnostic. What changes is how terse the model can be in summaries before it loses accuracy.
Quick Summary
- Agent cost grows quadratically because history is re-sent every step.
- Summarize completed steps and drop used tool output first.
- Cap the loop so a stuck agent cannot spin forever.
- Over-trimming causes repeated work – keep decisions, drop payloads.
- The same levers work on any stateless agent loop.
