Tokens are the smallest units that large language models (LLMs) process. Instead of reading character by character or word by word, LLMs split text into tokens - which can be words, parts of words, or even individual characters depending on the language and context.
Token là đơn vị nhỏ nhất mà các mô hình ngôn ngữ lớn (LLM) xử lý. Thay vì đọc từng ký tự hoặc từng từ, LLM chia văn bản thành các token - có thể là từ, phần của từ, hoặc thậm chí ký tự đơn lẻ tùy thuộc vào ngôn ngữ và ngữ cảnh.
Understanding tokens is crucial because they directly affect API costs, context limits, and how you structure your prompts.
Hiểu về token rất quan trọng vì chúng ảnh hưởng trực tiếp đến chi phí API, giới hạn context, và cách bạn cấu trúc các prompt của mình.
When you send text to an LLM, it gets split into tokens first. For example, the word "understanding" might be split into "under" + "stand" + "ing". Code typically generates more tokens because of symbols and syntax.
Khi bạn gửi text đến LLM, nó được chia thành các token trước. Ví dụ, từ 'understanding' có thể được chia thành 'under' + 'stand' + 'ing'. Code thường tạo ra nhiều token hơn vì ký hiệu và cú pháp.
1// This simple function...2function greet(name) {3 return "Hello, " + name;4}56// ...becomes approximately 15-20 tokens:7// "function" "greet" "(" "name" ")" "{" "return"8// """ "Hello" "," """ "+" "name" ";" "}"Quy tắc chung: 1 token gần bằng 4 ký tự tiếng Anh, hoặc khoảng 3/4 từ. Code thường có mật độ token cao hơn do dấu câu và cú pháp.
The context window is the LLM's "working memory" - the total number of tokens it can process in a single interaction. This includes everything: the system prompt, conversation history, file contents you share, and its response.
Context window là 'bộ nhớ làm việc' của LLM - tổng số token mà nó có thể xử lý trong một lần tương tác. Điều này bao gồm mọi thứ: system prompt, lịch sử hội thoại, nội dung file bạn chia sẻ, và câu trả lời của nó.
Different models have different context limits. Claude has a context window of up to 200K tokens, allowing it to process large codebases. However, efficient context management is still crucial.
Các model khác nhau có giới hạn context khác nhau. Claude có context window lên đến 200K token, cho phép xử lý các codebase lớn. Tuy nhiên, quản lý context hiệu quả vẫn rất quan trọng.
- Full context: When the context window fills up, older information gets truncated or summarized
Context đầy: Khi context window đầy, thông tin cũ sẽ bị cắt bớt hoặc tóm tắt
- Cost: More tokens means higher API costs
Chi phí: Nhiều token hơn đồng nghĩa với chi phí API cao hơn
- Performance: Very large contexts can slow down response times
Hiệu suất: Các context rất lớn có thể làm chậm thời gian phản hồi
- Accuracy: Information in the middle of long contexts may get "lost in the middle"
Độ chính xác: Thông tin ở giữa context dài có thể bị 'mất' (lost in the middle)
LLMs don't "understand" code the way humans do. Instead, they predict the most likely next token based on patterns learned from millions of lines of code in their training data.
LLM không 'hiểu' code theo cách con người làm. Thay vào đó, chúng dự đoán token tiếp theo có khả năng nhất dựa trên pattern học được từ hàng triệu dòng code trong dữ liệu huấn luyện.
When an LLM generates text, it calculates probabilities for each possible next token. "Temperature" controls how random the selection is:
Khi LLM tạo text, nó tính xác suất cho mỗi token tiếp theo có thể. 'Temperature' kiểm soát mức độ ngẫu nhiên trong việc chọn lựa:
- Low temperature (0.0-0.3): More deterministic and focused output, good for code
Temperature thấp (0.0-0.3): Đầu ra có tính xác định và tập trung hơn, tốt cho code
- High temperature (0.7-1.0): More varied and creative output, good for brainstorming
Temperature cao (0.7-1.0): Đầu ra đa dạng và sáng tạo hơn, tốt cho brainstorming
Even with the same prompt, an LLM may generate slightly different responses due to random sampling. This is why:
Ngay cả với cùng một prompt, LLM có thể tạo ra các câu trả lời hơi khác nhau do sampling ngẫu nhiên. Đây là lý do tại sao:
- The same question might yield different code
Cùng một câu hỏi có thể cho kết quả code khác nhau
- Rerunning a prompt might improve results
Chạy lại prompt có thể cải thiện kết quả
- More specific prompts lead to more consistent outputs
Prompt cụ thể hơn dẫn đến đầu ra nhất quán hơn
RAG (Retrieval-Augmented Generation) is a technique that helps LLMs access information beyond their trained knowledge. Instead of relying only on what it learned, the LLM can search for and use relevant information from external sources.
RAG (Retrieval-Augmented Generation) là kỹ thuật giúp LLM truy cập thông tin bên ngoài kiến thức được huấn luyện. Thay vì chỉ dựa vào những gì nó đã học, LLM có thể tìm kiếm và sử dụng thông tin liên quan từ nguồn bên ngoài.
Claude Code uses RAG to work effectively with large codebases. Instead of trying to fit the entire codebase into the context window, it:
Claude Code sử dụng RAG để làm việc hiệu quả với các codebase lớn. Thay vì cố gắng nhét toàn bộ codebase vào context window, nó:
- Searches for relevant files when needed (using Grep, Glob)
Tìm kiếm các file liên quan khi cần (sử dụng Grep, Glob)
- Only reads the portions of code necessary for the task
Chỉ đọc những phần code cần thiết cho tác vụ
- Combines retrieved information with trained knowledge
Kết hợp thông tin được truy xuất với kiến thức đã học
- Generates accurate, up-to-date responses about your project
Tạo ra câu trả lời chính xác và cập nhật về project của bạn
Đây là lý do tại sao Claude Code có thể làm việc với các dự án có hàng triệu dòng code - nó không cần phải 'nhớ' mọi thứ cùng một lúc.
Understanding how tokens work helps you write more efficient prompts:
Hiểu cách token hoạt động giúp bạn viết prompt hiệu quả hơn:
- Be concise: Remove filler words from your prompts
Ngắn gọn: Loại bỏ từ thừa trong prompt của bạn
- Be specific: Point to exact files or functions instead of asking to search
Cụ thể: Chỉ định chính xác file hoặc hàm thay vì yêu cầu tìm kiếm
- Break up tasks: Large tasks can be split into smaller conversations
Chia nhỏ tác vụ: Các tác vụ lớn có thể được chia thành các cuộc hội thoại nhỏ hơn
- Use CLAUDE.md: Put repeated instructions in this file instead of repeating each time
Sử dụng CLAUDE.md: Đưa hướng dẫn lặp lại vào file này thay vì nhắc lại mỗi lần
In the next chapter, you'll learn how to manage the context window effectively, including:
Trong chương tiếp theo, bạn sẽ học cách quản lý context window một cách hiệu quả, bao gồm:
- How Claude Code automatically compresses context
Cách Claude Code tự động nén context
- Techniques to keep context focused and relevant
Kỹ thuật để giữ context tập trung và liên quan
- When to start a new conversation
Khi nào nên bắt đầu cuộc hội thoại mới
Key Takeaways
Điểm Chính
- Tokens are the fundamental units LLMs process - typically 1 token equals 4 charactersToken là đơn vị cơ bản LLM xử lý - thường 1 token bằng 4 ký tự
- Context window is limited working memory - everything must fit including your prompt and the responseContext window là bộ nhớ làm việc có giới hạn - mọi thứ phải vừa bao gồm prompt và câu trả lời
- LLMs generate code by predicting the most likely next token based on learned patternsLLM tạo code bằng cách dự đoán token tiếp theo có khả năng nhất dựa trên pattern đã học
- RAG allows Claude Code to work with large codebases by retrieving relevant context on demandRAG cho phép Claude Code làm việc với codebase lớn bằng cách truy xuất context liên quan khi cần
Practice
Test your understanding of this chapter
What is a token in the context of LLMs?
Token trong ngữ cảnh của LLM là gì?
A larger context window always means better AI responses.
Context window lớn hơn luôn có nghĩa là câu trả lời AI tốt hơn.
What does RAG stand for?
RAG là viết tắt của gì?
Complete the context window layers
Hoàn thành các lớp của context window
System → CLAUDE.md → Conversation