Sub topic 3
Tokens, Embedding Input Limits, and Context Budgets
This chapter assumes you know basic Python, what an embedding is, and how a RAG system retrieves and then generates. It also assumes you know that a token is the unit a language model reads. Here you will see why chunk size has to be measured in tokens, and which limits it must fit inside.
Tokens, not words
A tokenizer splits text into tokens, which are pieces of words, whole short words, or punctuation. In English, one token averages about four characters, or roughly three quarters of a word. That ratio changes with the content:
- Source code, URLs, and tables often use more tokens per character.
- Languages other than English often use more tokens for the same meaning.
- Each model family has its own tokenizer, so the same text can have different counts for different models.
Chunkers such as chunk_words count words or characters because that is cheap. The limits you must respect are counted in tokens. A 200-word chunk may be 260 tokens of prose or 450 tokens of code.
Three limits that apply to a chunk
| Limit | What it caps | What happens when you exceed it |
|---|---|---|
| Embedding input limit | Tokens the embedding model reads per input | The input is truncated or rejected, depending on the model |
| Context window | Total tokens the generation model sees, input plus output | The request fails or older content is dropped |
| Context budget | The part of the window you can give to retrieved chunks | Fewer chunks fit, or the answer is cut short |
The embedding limit varies a lot. Many open-source sentence embedding models accept 256 to 512 tokens, and some hosted models accept several thousand. Check the model card for the model you use.
If a chunk is longer than the embedding limit, some models silently ignore the extra tokens. The vector then represents only the start of the chunk. Search will never match the part that was cut off.
Computing a context budget
The context window is shared. You must subtract everything that is not retrieved text before you decide how many chunks to send.
The code below turns this into a calculation. It uses a rough four-characters-per-token estimate, which is only a planning tool. For real counts, use the tokenizer that matches your model.
import math
def estimate_tokens(text, chars_per_token=4):
return max(1, math.ceil(len(text) / chars_per_token))
def max_chunks(window, prompt, question, answer_reserve, chunk_tokens):
available = window - prompt - question - answer_reserve
if available <= 0:
return 0
return available // chunk_tokens
def oversized(chunks, embed_limit=512, margin=0.9):
"""Return indexes of chunks likely to exceed the embedding limit."""
cap = embed_limit * margin
return [i for i, c in enumerate(chunks) if estimate_tokens(c) > cap]
print(max_chunks(window=8000, prompt=500, question=100,
answer_reserve=1000, chunk_tokens=400))
chunks = ["short chunk", "x" * 3000]
print(oversized(chunks))The first call prints 16: 8000 − 500 − 100 − 1000 leaves 6400 tokens, and 6400 divided by 400 is 16. The second call prints [1] because the 3,000-character chunk is about 750 estimated tokens, above the 460-token cap. The 0.9 margin leaves room for the estimate being wrong.
What this means for chunk size
- Set the maximum chunk size below the embedding limit, not at it. Leave 10 to 20 percent of room for estimation error and for any metadata prepended to the chunk text.
- Treat
top_k × chunk tokensas a cost that must fit in the retrieval budget. Doubling the chunk size halves the number of chunks you can send. - A larger window does not remove the need to chunk. Retrieval still picks passages by embedding, and an embedding of a very long chunk blurs many topics together.
Exercise
Exercise
A generation model has a 16,000-token context window. Your system prompt uses 800 tokens, the question uses 200, and you reserve 2,000 tokens for the answer. Each retrieved chunk is about 500 tokens. What is the largest top_k that fits? If the embedding model accepts only 512 tokens, is 500-token chunks a safe choice?
Show answer
The retrieval budget is 16,000 − 800 − 200 − 2,000 = 13,000 tokens. Dividing by 500 gives a maximum top_k of 26. A 500-token chunk is only 12 tokens under a 512-token embedding limit, so it is risky. Added metadata or a tokenizer mismatch could push some chunks over and cause truncation. A safer size is about 400 tokens, which keeps roughly 20 percent margin.