Token Generation and Consumption Explained: A Complete Guide for AI Infrastructure
This article breaks down how tokens are generated and consumed in large language model (LLM) serving, and why token metrics matter more than traditional hardware utilization. It covers the token lifecycle from text to embeddings and back, the structure of input, output, and tool-call token consumption, an eight-layer observability model, and practical recommendations for capacity planning, cost governance, and performance tuning. Readers will learn why GPU utilization is not the end goal and how to treat token cost as the north star metric.
1. Why We Need to Reconsider Tokens
When discussing AI infrastructure, many people first focus on hardware-level utilization metrics, such as accelerator occupancy, remaining GPU memory, and network bandwidth. But once large model services move into production, it becomes clear that healthy hardware metrics do not necessarily mean a good user experience: a cluster can be close to full capacity without a proportional increase in throughput, and there may still be free GPU memory while time-to-first-token keeps getting worse. This shows that the four resource types emphasized in the traditional cloud-native era—CPU, memory, network, and disk—are no longer enough to describe the real state of an intelligent inference system.
The real core variable is the token. It is both the smallest unit in which a model processes information and a shared measure of cost, latency, and throughput. Understanding how tokens are generated, how they are consumed within a single request, and how they accumulate across multi-turn tool calls is the prerequisite for capacity planning, cost governance, and performance tuning.
2. The Token Generation Pipeline: From Text to Vectors and Back
The token generation process can be abstracted as a two-way pipeline.
- Tokenization: The input text is split into a number of tokens. The granularity is determined by the model’s tokenization scheme, so the same passage may consume a different number of tokens under different models.
- Embedding: Each token is mapped to a multi-dimensional floating-point vector, often with thousands or even tens of thousands of dimensions. Different models may use different compression and representation schemes. This part is closely tied to model design and training, and ordinary users do not need to dig into it.
- Inference: The model performs attention computation and feed-forward transformations in the vector space, predicting the vector representation of the next token position by position.
- Decoding: The vector result is mapped back to a token in the vocabulary, and tokens are continuously appended autoregressively until they are assembled into readable text.
Schematic view of a single output:
User text -> tokenize -> tokens -> embedding -> inference -> vectors -> decode -> token -> assembled output
The key point is that output is not produced all at once; it is generated iteratively token by token. This directly determines the importance of streaming responses and time-to-first-token.
3. Token Consumption Structure: Input, Output, and Loop Accumulation
A complete request typically consumes three parts: the initial input, the tool-call loop, and the final output.
Output tokens include three types:
- Reasoning tokens: the model’s thinking process;
- Text tokens: the formal answer presented to the user;
- Tool-call tokens: structured call requests initiated by the model’s reasoning.
Input tokens include the original user input, plus the new input formed when each round of tool results is stitched back into the context.
Because tool calling is a loop, input tokens keep accumulating: the input of round N equals the initial input plus all outputs and tool results from the previous N-1 rounds. This is the fundamental reason costs balloon quickly for long-running tasks, and it is also why caching matters—saving already computed input and output states so the next round can reuse them directly and avoid recomputation.
4. Understanding Token Cost Through Eight Layers of Observability
Observability for modern AI infrastructure can be built in layers, roughly from bottom to top: hardware, scheduling, container and orchestration, inference engine, model and context, request and loop, business experience, and cost.
- Hardware layer: utilization, temperature, GPU memory bandwidth;
- Scheduling layer: queueing time, resource fragmentation, priority preemption;
- Orchestration layer: replica elasticity, scaling speed;
- Inference engine layer: batching efficiency, KV cache hit rate, decode throughput;
- Context layer: context length, truncation strategy;
- Loop layer: number of tool-call rounds and failed retries;
- Experience layer: time-to-first-token and overall completion time;
- Cost layer: cost per thousand tokens and total tokens per task as the final metrics.
GPU utilization is not the endpoint; token cost is the north star metric. In most business scenarios, users do not pay for “whether the accelerator is busy” but for “the tokens consumed to complete a task.”
5. Practical Recommendations
First, optimize the input. The more concise the prompt and the cleaner the context, the fewer input tokens and subsequent loop accumulations there will be. Second, control the loop. Set a maximum number of rounds and timeouts for tool calls to avoid wasted retries amplifying consumption. Third, make good use of caching. Stable system prompts and repeated context should be reusable. Fourth, observe in layers. Put token metrics and hardware metrics on the same dashboard to locate the real bottleneck behind “high utilization, low throughput.” Fifth, attribute cost. Track token consumption by business and by task to enable accurate budgeting and optimization.
In practice on platforms such as Baidu AI Cloud, the approach typically builds on common capabilities like container platforms, log services, and monitoring and alerting to bring token metrics into a unified observability system, achieving closed-loop governance from a resource perspective to a cost perspective.
