LLM API cost per task: the agent math, with GLM-4.7
LLM cost per task is each call's tokens times their prices. For agents, cached input dominates. A runnable calculator with GLM-4.7, GPT-5.2, Sonnet 4.5.

The cost of an LLM task is the sum, over every API call the task makes, of each kind of token times its price: fresh input, cached input, cache writes and output. For an agent, input dominates because every turn resends the whole conversation, so the cache-read price usually matters more than the headline price.
What shipped this week?
Z.ai released GLM-4.7 on December 22, 2025, with open weights on Hugging Face and an API. The pricing page (archived December 23) lists it at $0.60 per million input tokens, $0.11 per million cached input tokens and $2.20 per million output tokens, the same as GLM-4.6. The model docs give a 200K context window and 128K maximum output.
Z.ai pitches it at coding agents: its post names Claude Code, Kilo Code, Cline and Roo Code, and reports 73.8% on SWE-bench Verified against 77.2% for Claude Sonnet 4.5 in the same table. Those are the vendor’s numbers. The price is what caught my eye, and comparing price sheets line by line tells you less than it seems for agent work. So here is the math from first principles.
Why does cost per task matter more than price per token?
You pay per token, but you budget per task: a bug fixed, a ticket closed, a PDF turned into rows. The per-task number depends on three things beyond the price sheet: how many tokens the model needs for the task, how many of those are output, and how much of the input a cache serves. I covered the output side in what reasoning models are, since thinking tokens bill as output, and the effort parameter on Claude Opus 4.5 is a direct control on it. This post is about the input side, where agents spend most of their tokens.
How do you calculate LLM cost per task?
Start with one API call:
call cost = fresh_input × input_price
+ cache_writes × write_price
+ cached_input × cache_read_price
+ output × output_price
A task is a sum of calls. In an agent loop, each call sends the system prompt and tool definitions (call that prefix P), plus everything so far: every earlier reply and every tool result. If each turn adds a reply of o tokens and a tool result of t tokens, call number i sends P + (i - 1)(o + t) tokens. Over n turns that’s n·P + (o + t)·n(n - 1)/2 input tokens. The second term grows with the square of the turn count, so a task that takes twice the turns sends much more than twice the input.
A prompt cache changes which price each of those tokens pays. Everything the previous call sent is a prefix of this call, so with a warm cache only the newest reply and tool result are fresh.
Here’s the loop in code. It needs no API key; it walks the turns and prices each call:
# cost_per_task.py: what one agent task costs, turn by turn. Python 3.10+, no dependencies.
# USD per million tokens, from each provider's pricing page as archived on 2025-12-23/24.
PRICES = {
# input cache_write cache_read output
"glm-4.7": (0.60, 0.60, 0.11, 2.20), # Z.ai: implicit cache, no write fee listed
"gpt-5.2": (1.75, 1.75, 0.175, 14.00), # OpenAI: automatic cache, no write fee
"claude-sonnet-4.5": (3.00, 3.75, 0.30, 15.00), # Anthropic: cache_control, 5-min write = 1.25x
}
def task_cost(model, prefix, turns, tool_tokens, out_tokens, cache=True):
"""Returns (cached-read $, fresh-input $, output $) for one task."""
p_in, p_write, p_read, p_out = (p / 1e6 for p in PRICES[model])
read = fresh = out = 0.0
context, prev = prefix, 0
for turn in range(turns):
if cache and turn > 0:
read += prev * p_read # last turn's whole prompt is a prefix hit
fresh += (context - prev) * p_write # last reply + new tool result
else:
fresh += context * (p_write if cache else p_in)
out += out_tokens * p_out
prev, context = context, context + out_tokens + tool_tokens
return read, fresh, out
# A made-up task with a typical shape. Swap in numbers from your own usage logs.
TASK = dict(prefix=15_000, turns=30, tool_tokens=2_000, out_tokens=500)
sent = sum(TASK["prefix"] + t * 2_500 for t in range(TASK["turns"]))
print(f"input tokens sent: {sent:,} output tokens: {TASK['turns'] * TASK['out_tokens']:,}\n")
print(f"{'model':18} {'no cache':>9} {'cached':>9} {'60 turns':>9} cached $ split: read / fresh / output")
for m in PRICES:
nc, c, c60 = sum(task_cost(m, **TASK, cache=False)), task_cost(m, **TASK), sum(task_cost(m, **{**TASK, "turns": 60}))
split = " / ".join(f"{x / sum(c):.0%}" for x in c)
print(f"{m:18} ${nc:8.3f} ${sum(c):8.3f} ${c60:8.3f} {split}")
Output (Python 3.13.5):
input tokens sent: 1,537,500 output tokens: 15,000
model no cache cached 60 turns cached $ split: read / fresh / output
glm-4.7 $ 0.956 $ 0.245 $ 0.731 65% / 21% / 13%
gpt-5.2 $ 2.901 $ 0.617 $ 1.608 41% / 25% / 34%
claude-sonnet-4.5 $ 4.838 $ 0.988 $ 2.608 44% / 33% / 23%
The task is invented: a 15,000-token prefix, 30 turns, 2,000 tokens of tool output and 500 tokens of reply per turn. The prices are real list prices from the Z.ai, OpenAI and Anthropic pricing pages as archived that week. The “60 turns” column is cached, with every other number unchanged.
What do the numbers say?
Input is almost all of it. The task sends 1,537,500 input tokens and generates 15,000 output tokens, so 99% of the tokens are input. Output prices that look scary on a price sheet ($14 and $15 per million) end up as 13 to 34% of the cached bill here.
Caching cuts the bill by 74 to 80%. That holds for all three models, because the history that dominates the input is exactly what a prefix cache serves.
With caching on, cache reads are the biggest line. They’re 65% of the GLM-4.7 bill, 41% for GPT-5.2 and 44% for Claude Sonnet 4.5. So when you compare models for agent work, compare the cache-read prices ($0.11, $0.175 and $0.30 per million) before the headline input prices.
Turn count compounds. Doubling the turns from 30 to 60 multiplies the cost by 2.6 to 3.0 times, because each extra turn resends a longer history.
Price per token isn’t price per task. On identical token counts GLM-4.7 comes out about four times cheaper than Claude Sonnet 4.5 here ($0.245 against $0.988). Real models won’t use identical token counts. Even if GLM-4.7 needed 60 turns where Sonnet needed 30, this toy says it would still cost less ($0.731 against $0.988). That’s arithmetic, not a claim about how many turns either model takes on real work; you have to measure that.

How do providers bill cached tokens?
Each provider caches differently, and the script’s three price rows hide that.
- Z.ai caches implicitly, with nothing to configure, and reports hits in
usage.prompt_tokens_details.cached_tokens(context caching docs). One oddity: that page says cache hits are billed at “usually 50% of standard price”, while the pricing page lists $0.11 against $0.60, about 18%. I’d trust the price table and check the first invoice. - OpenAI caches automatically for prompts of 1,024 tokens or more, on exact prefix matches, with no write fee (prompt caching guide). Cached prefixes usually last 5 to 10 minutes of inactivity, up to an hour, and
gpt-5.2supports extended retention of up to 24 hours. - Anthropic caches only up to the
cache_controlbreakpoints you mark. A 5-minute cache write costs 1.25 times the base input price and a read costs 0.1 times; the minimum cacheable prompt for Sonnet 4.5 is 1,024 tokens, and each hit refreshes the 5-minute lifetime (prompt caching docs).
The script assumes every turn after the first hits the cache. Real agents miss. A cache expires while someone reviews a diff for ten minutes; a tool definition changes; something near the top of the prompt changes, like a timestamp in the system prompt. Any of those reprices the whole history at the full input rate on the next call. Keep the start of the prompt byte-for-byte stable and put anything that changes at the end.
Where does this math break?
Tokenizers differ. The same file is a different number of tokens for each model, so the same task produces different token counts before any model behavior comes into it. Take token counts from each provider’s usage fields, not from one shared tokenizer.
Thinking adds output, and sometimes history. GLM-4.7 thinks before every response and tool call, and with Preserved Thinking it keeps thinking blocks across turns, which Z.ai’s docs say improves cache hit rates. Those blocks then become part of the history every later turn resends. On Claude, the prompt caching docs say thinking blocks read from cache count as input tokens.
Rewriting history breaks the cache. When an agent compacts or edits earlier messages, the prefix changes and the next call is a miss. Compaction can still pay off over a long task, but the call right after it costs full price.
Long prompts can change the rate. Claude Sonnet 4.5 with the 1M-token beta charges $6 input and $22.50 output per million once a request passes 200K input tokens. My toy task peaks at 87,500, so it doesn’t apply here. A longer one would hit it.
Failed tasks still cost money. If a model finishes 60% of your tasks, the cost per successful task is the cost per attempt divided by 0.6. A cheap model that fails often can lose to an expensive one that doesn’t.
What would I do with this?
Log the usage object of every call, sum it per task, and divide by the success rate. That number, on your own tasks, is the one to compare. For GLM-4.7 specifically, my opinion is that a $0.11 cache read plus open weights make it worth measuring for agent loops; what I’d want to know first is how many turns it takes on real tasks compared with the model you use now, because turn count is the term that grows fastest.
FAQ
What is a good cost per task for an AI agent?
There’s no universal number. Compare it with what the task is worth: if a task replaces 30 minutes of someone’s time, a few cents or a few dollars per successful run are both cheap. Compare it across models on the same tasks, using cost per successful task.
Does prompt caching change the model’s answer?
No. OpenAI’s caching FAQ says the output is identical with or without caching, because only the prompt processing is reused and the response is generated fresh.
Why does an agent resend the whole conversation every turn?
The chat APIs priced here take the conversation as input on every request: each call has to include everything the model should see. The cache doesn’t remove the resend; it makes the repeated part cheaper and faster to process.
Is self-hosting an open-weight model like GLM-4.7 cheaper?
It moves the cost from tokens to GPU hours. The GLM-4.7 model card lists 358B parameters, and its vLLM example serves the FP8 checkpoint across four GPUs. Your cost per task becomes the hourly cost of that hardware divided by the tasks it finishes per hour. Work that number out with your own traffic before assuming self-hosting is cheaper than $0.11 per million cached tokens.
Sources
- Z.ai, GLM-4.7: Advancing the Coding Capability, December 22, 2025; GLM-4.7 docs; model card
- Z.ai pricing (archived December 23, 2025) and context caching (archived December 2025)
- OpenAI API pricing and prompt caching guide, archived December 24, 2025
- Anthropic pricing and prompt caching docs, archived December 2025