CLOUD CONCEPT

Tokenization

Tokenization is the process of converting text into smaller units called tokens and mapping them to numerical identifiers that a language model can process.

Cloud Concepts Hub →

What is Tokenization

Tokens can represent whole words, word fragments, punctuation, whitespace, or other character sequences, depending on the model’s tokenizer. Tokenization determines how much content fits into a model request and directly affects token-metered inference costs, processing requirements, and response latency.

Generative language models process sequences of numerical identifiers rather than raw text. A tokenizer splits text into units called tokens and maps those units to identifiers from a vocabulary. Depending on the tokenizer, a familiar word might be one token, while an unfamiliar technical term might become several tokens. Punctuation, spaces, source code, and non-English text also contribute to token counts. A token is therefore not necessarily a word, character, or byte.

The tokenizer must match the model. Common approaches include byte-pair encoding, WordPiece, and unigram tokenization, but their vocabularies and segmentation rules differ. Identical text can produce different token counts across models. Rules of thumb relating English words or characters to tokens are useful for early estimates, not for enforcing production limits or predicting exact charges. Use the selected model’s tokenizer or a provider-supported token-counting interface when available.

Tokenization is distinct from embeddings and chunking. Tokenization identifies the units a model processes; an embedding represents an input, such as a token or document passage, as a numerical vector. Chunking divides documents into larger passages for storage, retrieval, or processing. In retrieval-augmented generation, an application might split a manual into passages, embed those passages for search, and insert selected passages into a prompt. The generation model then tokenizes that complete prompt.

Cloud inference APIs commonly meter input and output tokens separately. Input includes more than the latest user question: system instructions, conversation history, retrieved documents, and tool definitions can all consume tokens. Generated answers consume output tokens. Some models also account for reasoning tokens or use separate rules for cached input and multimodal content. Engineers should inspect the selected model’s pricing and response usage fields rather than assume a universal billing formula.

For a hypothetical model charging $1 per million input tokens and $4 per million output tokens, a request with 2,000 input tokens and 500 output tokens costs $0.004 before any other charges. At 100,000 such requests, token charges total $400. These are illustrative rates, not a cloud service price quote. Provisioned capacity, self-hosted endpoints, and other deployment options may instead charge primarily for allocated compute or throughput.

Models have finite context windows, usually expressed in tokens. For many models, the combined input and generated output must fit within that window, while separate input or output limits may also apply. Reserve room for the answer when constructing a prompt. A maximum-output setting caps generation; it does not guarantee an answer of that length, and setting it too low can truncate useful output.

Token volume also affects performance. Longer inputs generally increase prompt-processing work and memory requirements, while longer outputs generally increase completion time because autoregressive models generate tokens sequentially. Streaming improves perceived responsiveness but does not inherently reduce token usage. Reduce unnecessary history, retrieve only relevant passages, and request concise answers, while testing that these changes preserve accuracy. Removing essential context merely to save tokens can produce worse answers and additional requests.

Finally, language-model tokenization is not security tokenization, which replaces sensitive values with surrogate values. Converting text to token identifiers does not anonymize or encrypt it. Multimodal models may also represent images, audio, or video using model-specific units or token equivalents; text-token estimates alone do not reliably predict their context usage or cost.

Key aspects of Tokenization

Model-specific tokenization governs text representation, context capacity, and the cost and performance of token-metered inference.

Model-specific segmentation

Token boundaries and numerical identifiers depend on the tokenizer paired with the model. Count tokens again when changing models rather than carrying over assumptions from the previous model.

Tokens are not words

A word can occupy one token or several, and punctuation or whitespace can contribute tokens. Technical identifiers, code, and different languages can have substantially different token counts for similar amounts of visible text.

Context-window budgeting

Budget for instructions, history, retrieved content, tool definitions, and the generated response. Enforce the model’s context and output limits before submitting requests.

Input and output metering

Token-metered inference commonly applies separate rates to input and output, with model-specific rules for caching, reasoning, or other modalities. Estimate workload cost using expected request volume and measured token usage, not prompt length alone.

Latency and resource impact

More input tokens generally require more prompt processing, and more generated tokens generally extend completion time. Measure latency alongside answer quality when shortening prompts or limiting responses.

Relationship to chunking and embeddings

Chunking creates passages, tokenization converts text into model-processing units, and embedding models produce vectors. In retrieval pipelines, size passages with the relevant model’s token limits in mind and count the final assembled generation prompt separately.

Cloud services for Tokenization

Managed model APIs such as Amazon Bedrock and Vertex AI can handle tokenization internally while exposing supported token-counting operations and usage measurements. Amazon SageMaker AI offers more control over model serving, including tokenizer packaging and generation configuration. Across these services, an application should assemble its full prompt, estimate tokens using the appropriate model interface, reserve output capacity, and record actual usage after inference. Compare quality, latency, and total cost using representative requests: equal token counts do not imply equal model capability or equal charges. These services are alternative deployment choices and do not need to be combined to implement token-aware applications.

Amazon Bedrock

Amazon Bedrock provides access to foundation models with model-specific token limits and inference pricing. Supported models expose token usage, and the CountTokens API can estimate input token counts for supported model requests before inference.

Open Amazon Bedrock →
Amazon SageMaker image Amazon SageMaker

Amazon SageMaker AI can host language models with their matching tokenizers in inference containers. Engineers control token limits and generation settings, while endpoint compute charges make token throughput and utilization important even when billing is not directly per token.

Open Amazon SageMaker →