How AI Coding Tools Work: Technical Architecture and Token Billing Explained
This article breaks down how terminal-based AI coding tools work under the hood and how their usage is billed. It walks through the full request pipeline—context assembly, the tool-call loop, and prefix caching—then explains the four token categories (uncached input, output, cache read, cache write) and how group multipliers turn list prices into actual deductions. It also highlights three commonly misread billing details and ends with a practical checklist for controlling costs.
How AI Coding Tools Work: Technical Architecture and Token Billing Explained
1. Why “Using It” Is Not the Same as “Understanding It”
An AI coding tool running in the terminal is essentially a runtime environment that wraps a large language model into a “read-and-write agent” for your codebase. Most developers only care whether it can fix the code correctly, and rarely pay attention to two things: what stages a single conversation actually consists of, and how the numbers on the bill are calculated. These two things are exactly what determine the usage cost and the response speed.
This article first breaks down the technical pipeline of a single request, then expands on the billing model layer by layer, and finally provides an actionable cost-optimization checklist.
2. The Complete Technical Pipeline of a Single Request
2.1 Context Assembly
Before sending a request, the tool performs context engineering: it assembles the system prompt, a summary of the project structure, the currently open file, recent conversation history, and retrieved relevant snippets into an input that fits within the context window limit. The quality of this step directly determines the accuracy of the model’s output, and it is the main reason why “the same model performs very differently across different tools.”
2.2 The Tool-Call Loop
The model does not modify files directly. Instead, it outputs structured tool-call intents, such as reading a file, searching for a symbol, writing a patch, or running tests. The runtime is responsible for parsing, executing, and feeding the results back into the context, then requesting the model again. This “reason—call—feed back” loop continues for multiple rounds until the model considers the task complete.
Illustrative pseudocode:
messages = [system_prompt, user_task]while True:resp = model.call(messages, tools=tool_schema)messages.append(resp.message)if resp.tool_calls is None:breakfor call in resp.tool_calls:result = dispatch(call) # read file / run tests / apply patchmessages.append(tool_result(call.id, result))
The key point is that each round of the loop is an independent model request, and the entire context must be resent every time. This is the structural reason why costs balloon quickly on long tasks.
2.3 Prefix Cache Reuse
To reduce the cost of repeated transmission and repeated computation, the industry commonly uses prefix caching: when the prefix of a request (system prompt, historical messages, file content) is exactly the same as before, this part is not recomputed but is billed at a lower unit price; if the prefix changes, the cache must be rewritten, and writing is usually more expensive than reading. This is why “changing a single comment triggers a full recomputation of the entire history” is a common phenomenon.
3. The Four Token Categories and Billing Composition
The usage data returned with a response usually contains four token categories: uncached input, output, cache read, and cache write. Each category has its own unit price (priced per million tokens), and the total cost is a weighted sum of the four:
List price cost = input tokens × input unit price+ output tokens × output unit price+ cache read tokens × cache read unit price+ cache write tokens × cache write unit price
Some platforms multiply the list price by a group multiplier to get the amount actually deducted:
Deducted amount = list price cost × group multiplier
The group multiplier varies by resource pool type. Common forms include: a shared-quota group with a low multiplier, a general group close to twice the official price, a higher-multiplier group for stable calls, and a customized high-concurrency group on demand. The multiplier is adjusted over time, so you should confirm the current day’s value in the console before use.
4. Three Details That Are Easiest to Misread
- The multiplier is applied to the official list price. The displayed price is not the official original price, but “official price × current group multiplier.” If a model is officially priced at input 4, output 20, and cache read 0.2 (per million tokens), it will show as 8, 40, and 0.4 under a 2x group.
- The default “All” view is not the original price. When no group is explicitly selected, the list is often displayed at some default multiplier, which can easily lead to a misjudgment of the real cost. Select the group your key belongs to before checking prices.
- The billing symbol may be platform credits rather than real currency. Some platforms use a 1:1 top-up ratio between RMB and credits, so a deducted “9” actually corresponds to 9 yuan of balance and has nothing to do with US dollars.
5. Cost-Optimization Practice Checklist
- Stable prefix: Fix the system prompt and project conventions, avoid inserting changing content midway through a session, and maximize cache hits.
- Control the number of loop rounds: Set maximum iterations and timeout limits for the agent loop to prevent invalid retries from amplifying token consumption.
- Precise retrieval: Replace whole-file feeding with symbol-level retrieval to reduce uncached input.
- Submit tasks in segments: Break a large refactor into small tasks so that the context can be reused after each segment, avoiding repeated recomputation of an extra-long context all at once.
- Bill attribution: Log the four token categories and the multiplier for each request at the gateway layer, and aggregate by project and by person to locate cost hotspots.
6. Conclusion
Understanding AI coding tools requires looking at two threads at the same time: technically, it is a combination of “context assembly + tool-call loop + prefix caching”; in terms of cost, it is a linear superposition of “four token categories × group multiplier.” Once you line up these two threads, the bill is no longer a black box. If an enterprise wants to implement similar capabilities in its own environment, it can run the runtime on a container platform, store build artifacts in object storage, and use a logging service with monitoring and alerting for usage attribution—this is also a common general product combination approach among cloud vendors such as Baidu AI Cloud.
