跪拜 Guibai
← All articles
Architecture

Tokenization and Embeddings: The Two Primitives Every LLM Application Sits On

By mONESY ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Tokenization and embeddings are not academic details — they determine API costs, retrieval quality, and whether a RAG pipeline returns relevant documents or noise. Misunderstanding either leads to bloated bills or broken search.

Summary

Every LLM call starts with tokenization: text is broken into subword IDs using BPE, with roughly 3 English characters or 2 Chinese characters per token. These IDs are the only input neural networks can process, and the sum of input and output tokens determines API cost. The open-source tiktoken library encodes and decodes this locally with a single function call.

After tokenization, embedding models convert text into 1024-dimensional vectors where semantically similar sentences sit close together. Cosine similarity between these vectors powers retrieval-augmented generation, document matching, and plagiarism detection. Alibaba Cloud's Bailian API provides an OpenAI-compatible embedding endpoint that returns these vectors directly.

Practical debugging reveals that longer Chinese texts produce more stable vectors than short phrases, and 1024 dimensions handle small knowledge bases fine. At hundreds of thousands of documents, a dedicated vector database becomes necessary to keep queries fast.

Takeaways
BPE tokenization splits text into subword IDs; roughly 3 English characters or 2 Chinese characters make one token.
API billing counts both input tokens and output tokens, with per-million-token costs in the single-digit yuan range.
Embedding models output 1024-dimensional float vectors in the [-1, 1] range, where proximity equals semantic similarity.
Cosine similarity between embedding vectors is the core mechanism behind RAG retrieval, document matching, and deduplication.
Longer Chinese texts produce more stable embedding vectors than short sentences, which show higher variance in similarity scores.
1024-dimensional vectors are sufficient for small local knowledge bases; at scale, a vector database is required for performance.
Conclusions

Tokenization and embedding are the two gates every LLM application passes through, yet many developers treat them as black boxes until a billing surprise or retrieval failure forces them to look.

The observation that short Chinese texts produce less stable embeddings is a concrete, testable claim that most embedding documentation glosses over — it implies minimum text-length thresholds matter for production RAG quality.

Using Alibaba Cloud's OpenAI-compatible embedding endpoint shows how the API surface has standardized across Western and Chinese providers, making the choice of vendor largely a matter of latency, cost, and data locality rather than integration effort.

Concepts & terms
BPE (Byte Pair Encoding)
A subword tokenization algorithm that iteratively merges the most frequent pairs of bytes or characters. It handles rare words and mixed-language text by breaking them into known subword units, which is why GPT models can tokenize arbitrary input without an out-of-vocabulary problem.
Cosine Similarity
A measure of the angle between two vectors, ranging from -1 (opposite) to 1 (identical). In embedding space, it quantifies semantic similarity: two sentences about the same topic will have a cosine similarity close to 1, while unrelated sentences approach 0.
Embedding Vector
A dense, fixed-length array of floats (commonly 1024 dimensions) that represents a piece of text in a continuous vector space. Models like text-embedding-v4 map semantically similar texts to nearby points, enabling search and clustering without keyword matching.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗