Chapterبەش 1 · Lectureوانە 1.3
How a Language Model Writes: Tokens, Probabilities and the Context Windowمۆدێلی زمان چۆن دەنووسێت: تۆکنەکان، ئەگەرەکان و پەنجەرەی کۆنتێکست
How text becomes tokens (byte-pair encoding, vocabularies, why Kurdish costs more), the next-token loop and what happens inside a transformer, choosing the next token (greedy decoding, sampling, temperature and top-p, and why the newest models no longer let you set them), and the context window: working memory, stateless requests, context rot and limits. Small Python programs you can run.
دەق چۆن دەبێت بە (، ، بۆچی کوردی گرانترە)، خولی داهاتوو و ئەوەی لە ناو ڕوو دەدات، هەڵبژاردنی داهاتوو (، ، و top-p و بۆچی نوێترین مۆدێلەکان چیتر ڕێگە نادەن دایانبنێیت) و : بیرگەی کارکردن، ، و سنوورەکان. چەند بەرنامەیەکی بچووکی Python کە دەتوانیت کاریان پێ بکەیت.
What you will learnچی فێر دەبیت
- Explain what a token is and how byte-pair encoding builds a tokenizer's vocabulary.
- Explain why the same text usually needs more tokens in Kurdish than in English, and what that costs.
- Describe the next-token loop and the role of embeddings, self-attention and layers at concept level.
- Turn scores into probabilities in Python and compare greedy decoding, sampling, temperature and top-p.
- Check whether a request fits a context window, and explain stateless requests and context rot.
- ڕوون بکەیتەوە چییە و چۆن دروست دەکات.
- ڕوون بکەیتەوە بۆچی هەمان دەق زۆرجار بە کوردی زیاتری لە ئینگلیزی دەوێت و ئەمە چی تێدەچێت.
- خولی داهاتوو و ڕۆڵی ، و بە شێوەیەکی بیرۆکەیی وەسف بکەیت.
- بە Python نمرەکان بکەیت بە و ، ، و top-p بەراورد بکەیت.
- بپشکنیت ئایا لە جێی دەبێتەوە و و ڕوون بکەیتەوە.
A reply, one piece at a timeوەڵامێک، پارچە پارچە
When Aram asks a chatbot a question, the answer does not appear all at once. It grows on the screen, a few letters at a time. Each piece is a token: a word, part of a word, a space with a word, or a punctuation mark. A language model writes by choosing one token, then the next, then the next, until the answer is finished.
کاتێک ئارام پرسیارێک لە دەکات، وەڵامەکە بە یەکجار دەرناکەوێت. لەسەر شاشەکە پارچە پارچە گەورە دەبێت. هەر پارچەیەک : وشەیەک، بەشێک لە وشەیەک، بۆشاییەک لەگەڵ وشەیەک یان نیشانەیەکی خاڵبەندی. بەم شێوەیە دەنووسێت: هەڵدەبژێرێت، پاشان یەکێکی تر، پاشان یەکێکی تر، تا وەڵامەکە تەواو دەبێت.
This lesson opens the box: how text becomes tokens, how the model scores every possible next token, why the same question can get different answers, and how much text a model can look at in one go (its context window). The Python examples use lists, dictionaries and functions; no install and no API key are needed. Counting which word follows which is taught in Language models, small and large; here we start from the model's scores.
ئەم وانەیە سندوقەکە دەکاتەوە: دەق چۆن دەبێت بە ، چۆن نمرە دەداتە هەموو گونجاوی داهاتوو، بۆچی هەمان پرسیار لەوانەیە وەڵامی جیاواز وەربگرێت و لە یەک کاتدا چەند دەق دەبینێت (). نموونەکانی Python ، و بەکار دەهێنن؛ نە دامەزراندن پێویستە نە . ژماردنی ئەوەی کام وشە دوای کام وشە دێت لە مۆدێلەکانی زمان، بچووک و گەورە فێر کراوە؛ لێرە لە نمرەکانی دەست پێ دەکەین.


Tokens: the pieces a model readsتۆکنەکان: ئەو پارچانەی مۆدێل دەیانخوێنێتەوە
A model does not read letters as we do. A program called a tokenizer first cuts the text into tokens and gives each token an id number from the model's vocabulary: the fixed list of every token it knows. A common word is usually one token. A longer or rarer word is cut into pieces: unbelievable may become un + believ + able, and «کتێبەکانمان» (our books) may become «کتێب» + «ەکان» + «مان». A verb such as «دەکاتەوە» may become «دەکات» + «ەوە».
وەک ئێمە پیت ناخوێنێتەوە. بەرنامەیەکی سەرەتا دەقەکە دەکات بە و هەر ژمارەیەکی ناسنامە لە وەردەگرێت: لیستی جێگیری هەموو ئەو دەیانناسێت. وشەیەکی باو زۆرجار یەک . وشەیەکی درێژتر یان دەگمەنتر دەکرێت بە پارچە: unbelievable لەوانەیە ببێت بە un + believ + able و «کتێبەکانمان» لەوانەیە ببێت بە «کتێب» + «ەکان» + «مان». کردارێکی وەک «دەکاتەوە» لەوانەیە ببێت بە «دەکات» + «ەوە».
After this step the model never sees letters, only a list of numbers. Spaces and signs count too: a full stop is usually a token of its own. GPT-2 (2019) had a vocabulary of 50,257 tokens; newer models have even more. Every company trains its own tokenizer, and even one company's change: Anthropic's tokenizer since Claude Opus 4.7 gives about 30 % more tokens for the same text than the one before.
دوای ئەم هەنگاوە هەرگیز پیت نابینێت، تەنیا لیستێک ژمارە. بۆشایی و نیشانەکانیش دەژمێردرێن: خاڵ زۆرجار سەربەخۆیە. GPT-2 (2019) 50,257 تۆکنی هەبوو؛ نوێتر زیاتریان هەیە. هەر کۆمپانیایەک بەرنامەی خۆی ڕادەهێنێت و تەنانەت هی یەک کۆمپانیاش دەگۆڕێت: بەرنامەی Anthropic لە Claude Opus 4.7ـەوە بۆ هەمان دەق نزیکەی 30 % زیاتر دەدات لە هی پێشووتر.
How a tokenizer learns its pieces: byte-pair encodingبەرنامەی تۆکنکردن چۆن پارچەکانی فێر دەبێت: کۆدکردنی جووتەبایت
Most tokenizers learn their vocabulary with byte-pair encoding (BPE), an old compression idea that Sennrich, Haddow and Birch brought to language models in 2015. The recipe is short:
زۆربەی بەرنامەکانی بە (BPE) فێر دەبن، کە بیرۆکەیەکی کۆنی پەستاندنە و Sennrich، Haddow و Birch لە ساڵی 2015دا هێنایانە ناو . ڕێگاکە کورتە:
Start with single characters (or bytes).
بە تاکە پیت (یان ) دەست پێ بکە.
Count every pair of neighbours in a large training text.
هەموو جووتێکی دراوسێ لە دەقێکی گەورەی ڕاهێناندا بژمێرە.
Join the most frequent pair into one new token and add it to the vocabulary.
زۆرترین جووتی دووپاتە بکە بە یەک نوێ و بیخەرە ناو .
Repeat, thousands of times.
دووپاتەی بکەرەوە، هەزاران جار.
from collections import Counter
def merge_pair(pieces, pair):
"""Join the two pieces of `pair` wherever they stand side by side."""
out, i = [], 0
while i < len(pieces):
if i + 1 < len(pieces) and (pieces[i], pieces[i + 1]) == pair:
out.append(pieces[i] + pieces[i + 1])
i += 2
else:
out.append(pieces[i])
i += 1
return out
def train(text, rounds):
words = [list(w) + ["</w>"] for w in text.split()] # "</w>" = end of a word
merges = []
for _ in range(rounds):
pairs = Counter()
for w in words:
pairs.update(zip(w, w[1:]))
if not pairs:
break
best = max(pairs, key=pairs.get) # the most frequent neighbour pair
merges.append(best)
words = [merge_pair(w, best) for w in words]
return merges
def tokenize(text, merges):
tokens = []
for word in text.split():
pieces = list(word) + ["</w>"]
for pair in merges: # replay the merges in order
pieces = merge_pair(pieces, pair)
tokens += pieces
return tokens
training_text = "the student reads the book . the teacher reads the lesson . " * 5
merges = train(training_text, 30)
print("first 5 merges:", merges[:5])
for sentence in ["the student reads the lessons",
"the students read",
"قوتابییەکە وانەکان دەخوێنێتەوە"]:
tokens = tokenize(sentence, merges)
print(len(tokens), "tokens:", tokens)first 5 merges: [('h', 'e'), ('t', 'he'), ('the', '</w>'), ('e', 'a'), ('r', 'ea')]
7 tokens: ['the</w>', 'student</w>', 'reads</w>', 'the</w>', 'lesson', 's', '</w>']
6 tokens: ['the</w>', 'student', 's', '</w>', 'read', '</w>']
31 tokens: ['ق', 'و', 'ت', 'ا', 'ب', 'ی', 'ی', 'ە', 'ک', 'ە', '</w>', 'و', 'ا', 'ن', 'ە', 'ک', 'ا', 'ن', '</w>', 'د', 'ە', 'خ', 'و', 'ێ', 'ن', 'ێ', 'ت', 'ە', 'و', 'ە', '</w>']Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 16
Each word becomes a list of letters plus
</w>, an end-of-word mark (the 2015 paper used the same sign).هەر وشەیەک دەبێت بە لیستێک پیت لەگەڵ
</w>، نیشانەی کۆتایی وشە (وتارەکەی 2015 هەمان نیشانەی بەکار هێنا).- Lineدێڕی 19-24
zip(w, w[1:])gives every pair of neighbours;Countercounts them;max(..., key=pairs.get)takes the most frequent.zip(w, w[1:])هەموو جووتێکی دراوسێ دەدات؛Counterدەیانژمێرێت؛max(..., key=pairs.get)زۆرترینیان دەبات.- Lineدێڕی 32-34
To tokenize new text, the learned merges are replayed in the same order.
بۆ دەقی نوێ، ئەو بەیەکگەیاندنانەی فێر بوون بە هەمان ڕیز دووپاتە دەکرێنەوە.
The unseen words lessons and students are still written with known pieces (lesson + s). The Kurdish sentence «قوتابییەکە وانەکان دەخوێنێتەوە» (the student reads the lessons) found no learned pair, so every letter became its own token: 31 tokens against 7. In the output each Kurdish word reads from right to left, as usual.
وشە نەبینراوەکانی lessons و students هێشتا بە پارچە ناسراوەکان دەنووسرێن (lesson + s). ڕستە کوردییەکەی «قوتابییەکە وانەکان دەخوێنێتەوە» هیچ جووتێکی فێربووی نەدۆزییەوە، بۆیە هەر پیتێک بوو بە سەربەخۆ: 31 بەرامبەر 7. لە دەرخستەکەدا هەر وشەیەکی کوردی وەک هەمیشە لە ڕاستەوە بۆ چەپ دەخوێنرێتەوە.
Why Kurdish needs more tokensبۆچی کوردی تۆکنی زیاتری دەوێت
A tokenizer learns from its training text, and most of that text is English. So English words become whole tokens, while Kurdish words are cut into many small pieces. Petrov and colleagues (2023) compared real tokenizers and found that the same text can need up to 15 times more tokens in one language than in another. Kurdish letters also take more space: in UTF-8, the usual way to store text (UTF-8: one to four bytes), an English letter is 1 byte and a Kurdish letter is 2 bytes. A tokenizer that works on bytes and has learned no Kurdish pieces may even need two tokens for one Kurdish letter.
بەرنامەی لە دەقی ڕاهێنانەکەی فێر دەبێت و زۆربەی ئەو دەقە ئینگلیزییە. بۆیە وشە ئینگلیزییەکان دەبن بە تەواو، بەڵام وشە کوردییەکان دەکرێن بە چەندین پارچەی بچووک. Petrov و هاوکارانی (2023) بەرنامە ڕاستەقینەکانی بەراورد کرد و بۆیان دەرکەوت کە هەمان دەق لەوانەیە لە زمانێکدا تا 15 هێندە زیاتری پێویست بێت لە زمانێکی تر. پیتی کوردی جێگەی زیاتریش دەگرێت: لە UTF-8، ڕێگە باوەکەی هەڵگرتنی دەق (UTF-8: یەک تا چوار بایت)، پیتێکی ئینگلیزی 1 و پیتێکی کوردی 2 . بەرنامەیەکی کە لەسەر کار دەکات و هیچ پارچەیەکی کوردی فێر نەبووە لەوانەیە بۆ یەک پیتی کوردی دوو پێویست بێت.
print(len("hello"), len("hello".encode("utf-8")))
print(len("سڵاو"), len("سڵاو".encode("utf-8")))5 5 4 8
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
«سڵاو» has 4 letters but 8 bytes: every Kurdish letter is 2 bytes in UTF-8.
«سڵاو» 4 پیتی هەیە بەڵام 8 : هەر پیتێکی کوردی لە UTF-8دا 2 .
More tokens mean a higher price, a slower answer and less room in the context window. Rules of thumb exist only for English and differ by company: Google says a Gemini token is about 4 characters and 100 tokens are about 60–80 English words; Anthropic says 1 million tokens are about 555,000 English words on Claude's newest tokenizer (as of November 2026). For Kurdish, count with the provider's own free token counter (Token optimization).
زیاتر واتە نرخی زیاتر، وەڵامی هێواشتر و جێگەی کەمتر لە . یاسای نزیکەیی تەنیا بۆ ئینگلیزی هەن و لە کۆمپانیایەکەوە بۆ کۆمپانیایەکی تر جیاوازن: Google دەڵێت Gemini نزیکەی 4 پیتە و 100 نزیکەی 60–80 وشەی ئینگلیزییە؛ Anthropic دەڵێت 1 ملیۆن لەسەر نوێترین بەرنامەی Claude نزیکەی 555,000 وشەی ئینگلیزییە (لە مانگی 11ی 2026دا). بۆ کوردی، بە ژمێرەری بێبەرانبەری خودی دابینکەرەکە بیژمێرە (باشترینکردنی تۆکن).
The next-token loopخولی تۆکنی داهاتوو
Once the text is a list of token ids, writing is a loop:
کاتێک دەقەکە بوو بە لیستێک ژمارەی ناسنامەی ، نووسین دەبێت بە :
- Readخوێندنەوە
The model takes all the tokens so far: the instructions, the question and its own answer up to now.
هەموو تا ئێستا وەردەگرێت: ڕێنماییەکان، پرسیارەکە و وەڵامی خۆی تا ئەم ساتە.
- Scoreنمرەدان
It gives every token in its vocabulary a score: how well it fits next.
نمرەیەک دەداتە هەموو : چەند بۆ دواتر دەگونجێت.
- Probabilitiesئەگەرەکان
The softmax function turns the scores into probabilities between 0 and 1 that add up to 1 (in Python below).
نمرەکان دەکات بە لە نێوان 0 و 1دا کە کۆیان 1ـە (بە Python لە خوارەوە).
- Pickهەڵبژاردن
One token is chosen, usually one of the most likely.
هەڵدەبژێردرێت، زۆرجار یەکێک لە ئەگەرترینەکان.
- Add and repeatزیادکردن و دووپاتەکردنەوە
The token joins the text and the loop starts again, until the model picks a special end token or reaches the length limit.
دەچێتە سەر دەقەکە و دیسان دەست پێ دەکاتەوە، تا تایبەتی کۆتایی هەڵدەبژێرێت یان دەگاتە سنووری درێژی.
Take a Kurdish saying. After «دڵۆپ دڵۆپ دەبێتە» (drop by drop becomes …), a model trained on Kurdish text gives «دەریا» (sea) the highest probability, because that is how the saying usually goes. It does not know the saying; it has seen what usually comes next. In the same way, after «ئازاد بیر دەکاتەوە لە ژیریی دەستکرد» (Azad thinks about artificial intelligence) the next token might be «و» 22 %, «کە» 18 %, «چونکە» 15 % or «بەڵام» 10 % (invented numbers).
پەندێکی کوردی وەربگرە. دوای «دڵۆپ دڵۆپ دەبێتە»، کە لەسەر دەقی کوردی ڕاهێنرابێت بەرزترین دەداتە «دەریا»، چونکە پەندەکە زۆرجار وا دەڕوات. پەندەکە نازانێت؛ بینیویەتی زۆرجار چی دوای ئەوە دێت. بە هەمان شێوە، دوای «ئازاد بیر دەکاتەوە لە ژیریی دەستکرد» لەوانەیە داهاتوو «و» بێت 22 %، «کە» 18 %، «چونکە» 15 % یان «بەڵام» 10 % (ژمارەی نموونەیی).


Inside the model: embeddings, attention and layersناو مۆدێلەکە: ئیمبەدینگ، سەرنجی خۆیی و چینەکان
What happens between read and score? Today's language models are transformers (Vaswani and others, 2017; why they replaced older networks is in RNNs and transformers). Each token id first becomes an embedding, a long list of numbers (next lesson). Then the numbers pass through many layers. In each layer:
لە نێوان خوێندنەوە و نمرەداندا چی ڕوو دەدات؟ ئەمڕۆ ن (Vaswani و هاوکارانی، 2017؛ بۆچی جێگەی تۆڕە کۆنەکانیان گرتەوە لە تۆڕی دەماریی گەڕاوە و ترانسفۆرمەردا هاتووە). هەر ژمارەیەکی ناسنامەی سەرەتا دەبێت بە ، لیستێکی درێژی ژمارە (وانەی داهاتوو). پاشان ژمارەکان بە چەندین تێدەپەڕن. لە هەر :
- Self-attention: every token looks back at the tokens before it and decides which ones matter for its meaning. In «ئازاد بیر دەکاتەوە لە ژیریی دەستکرد», «دەکاتەوە» pays most attention to «ئازاد» (who is thinking?) and «دەستکرد» to «ژیریی» (artificial what?).
- A small feed-forward network then adds more meaning to each token on its own.
- Normalisation keeps the numbers in a steady range, so the many layers work well together.
- : هەر سەیری ئەو پێش خۆی دەکات و بڕیار دەدات کامیان بۆ واتاکەی گرنگن. لە «ئازاد بیر دەکاتەوە لە ژیریی دەستکرد»دا، «دەکاتەوە» زۆرترین سەرنج دەداتە «ئازاد» (کێ بیر دەکاتەوە؟) و «دەستکرد» سەرنج دەداتە «ژیریی» (چی دەستکرد؟).
- پاشان تۆڕێکی بچووکی feed-forward واتای زیاتر بە هەر دەدات بە تەنیا.
- ژمارەکان لە مەودایەکی جێگیردا دەهێڵێتەوە، بۆ ئەوەی پێکەوە باش کار بکەن.
Big models repeat this block in dozens of layers. After the last layer come the scores for the next token. A model that writes looks only backwards, at earlier tokens, because the later ones do not exist yet. To watch a small GPT-style model do all of this in 3D, open Brendan Bycroft's LLM Visualization (bbycroft.net/llm).
گەورە ئەم بلۆکە لە دەیان دووپاتە دەکاتەوە. دوای دوایین نمرەکانی داهاتوو دێنە دەرەوە. کە دەنووسێت تەنیا سەیری دواوە دەکات، سەیری پێشوو، چونکە ئەوانەی دواتر هێشتا نین. بۆ ئەوەی ببینیت بچووکی وەک GPT هەموو ئەمانە بە سێ ڕەهەند دەکات، LLM Visualizationـی Brendan Bycroft بکەرەوە (bbycroft.net/llm).
Choosing the next token: greedy or by chance?هەڵبژاردنی تۆکنی داهاتوو: چاوچنۆکانە یان بە هەڵکەوت؟
The model gives probabilities; a separate rule chooses the token. Two simple rules:
دەدات؛ یاسایەکی جیا هەڵدەبژێرێت. دوو یاسای سادە:
- Greedy decoding: always take the most likely token. The same input always gives the same output, but long texts become dull and start to repeat themselves.
- Sampling: choose by chance, using the probabilities as weights, like drawing a ticket from a bag with 66 tickets saying sugar, 24 lemon, 9 milk and 1 salt. Likely tokens win most of the time, but not always.
- : هەمیشە ئەگەرترین وەربگرە. هەمان هەمیشە هەمان دەدات، بەڵام دەقە درێژەکان بێتام دەبن و دەست دەکەن بە دووپاتەبوونەوە.
- : بە هەڵکەوت هەڵبژێرە و وەک کێش بەکار بهێنە، وەک دەرهێنانی پسوولەیەک لە کیسەیەک کە 66 پسوولەی sugar، 24 lemon، 9 milk و 1 saltـی تێدایە. ئەو ئەگەریان زیاترە زۆربەی جار دەبەنەوە، بەڵام نەک هەمیشە.
Chat apps usually sample, so asking the same question twice can give two different answers; both may be good, and both may be wrong. Here is the idea in Python, with invented scores for the next word of Tea in Erbil is served with …:
ئەپەکانی گفتوگۆ زۆرجار دەکەن، بۆیە دوو جار پرسینی هەمان پرسیار لەوانەیە دوو وەڵامی جیاواز بدات؛ لەوانەیە هەردووکیان باش بن و لەوانەیە هەردووکیان هەڵە بن. ئەمە بیرۆکەکەیە بە Python، بە نمرەی نموونەیی بۆ وشەی داهاتووی Tea in Erbil is served with … (چای هەولێر لەگەڵ … دەدرێت):
import math
import random
# Scores a model might give the next word of "Tea in Erbil is served with ..."
scores = {"sugar": 3.0, "lemon": 2.0, "milk": 1.0, "salt": -1.0}
def probabilities(scores, temperature=1.0):
"""Turn scores into probabilities (the softmax function)."""
top = max(scores.values())
powers = {w: math.exp((s - top) / temperature) for w, s in scores.items()}
total = sum(powers.values())
return {w: p / total for w, p in powers.items()}
for t in (0.5, 1.0, 2.0):
probs = probabilities(scores, t)
print(f"T={t}:", " ".join(f"{w} {p:.2f}" for w, p in probs.items()))
probs = probabilities(scores)
print("greedy:", max(probs, key=probs.get))
random.seed(5)
words, weights = list(probs), list(probs.values())
print("sampled:", random.choices(words, weights=weights, k=8))T=0.5: sugar 0.87 lemon 0.12 milk 0.02 salt 0.00 T=1.0: sugar 0.66 lemon 0.24 milk 0.09 salt 0.01 T=2.0: sugar 0.47 lemon 0.29 milk 0.17 salt 0.06 greedy: sugar sampled: ['sugar', 'lemon', 'lemon', 'milk', 'lemon', 'milk', 'sugar', 'sugar']
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 9-12
The softmax: raise e to each score, then divide by the total so the probabilities add up to 1. Subtracting the largest score first changes nothing in the result but keeps
math.expfrom overflowing (see Common mistakes).: e بەرز بکەرەوە بۆ توانی هەر نمرەیەک، پاشان دابەشی بکە بەسەر کۆکەدا بۆ ئەوەی کۆی ببێت بە 1. دەرکردنی گەورەترین نمرە پێشتر هیچ لە ئەنجامەکە ناگۆڕێت، بەڵام ڕێ لە
math.expدەگرێت (بڕوانە هەڵە باوەکان).- Lineدێڕی 18-19
Greedy decoding:
maxwithkey=probs.getreturns the word with the biggest probability.:
maxلەگەڵkey=probs.getئەو وشەیە دەگەڕێنێتەوە کە گەورەترین هەیە.- Lineدێڕی 21-23
Sampling:
random.choicesdraws 8 words with the probabilities as weights.random.seed(5)makes the draw repeatable, so you get exactly this output (the random module).:
random.choices8 وشە دەردەهێنێت و وەک کێش بەکار دەهێنێت.random.seed(5)دەرهێنانەکە دووپاتەکراوە دەکات، بۆیە ڕێک ئەم دەرخستەیە وەردەگریت (مۆدیولی random).
Temperature and top-pپلەی گەرمی و top-p
Before the softmax, the scores can be divided by a number called the temperature. A low temperature (0.5) makes the favourite even stronger: sugar gets 87 %. A high temperature (2) flattens the chances: sugar drops to 47 % and even salt gets 6 %. Low means safe and repetitive; high means varied and risky. Top-p sampling is a second filter: keep only the most likely tokens whose probabilities add up to p, then sample among them. With p = 0.8, sugar (0.66) and lemon (0.24) already reach 0.8, so milk and salt can never be chosen.
پێش ، دەکرێت نمرەکان دابەش بکرێن بەسەر ژمارەیەکدا کە پێی دەگوترێت . نزم (0.5) دڵخوازەکە بەهێزتر دەکات: sugar 87 % وەردەگرێت. بەرز (2) دەرفەتەکان یەکسانتر دەکات: sugar دادەبەزێت بۆ 47 % و تەنانەت salt 6 % وەردەگرێت. نزم واتە سەلامەت و دووپاتە؛ بەرز واتە جۆراوجۆر و مەترسیدار. فلتەرێکی دووەمە: تەنیا ئەگەرترین بهێڵەرەوە کە کۆی دەگاتە p، پاشان لە نێوانیاندا بکە. بە p = 0.8، sugar (0.66) و lemon (0.24) پێکەوە دەگەنە 0.8، بۆیە milk و salt هەرگیز هەڵنابژێردرێن.
The context window: the model's working memoryپەنجەرەی کۆنتێکست: بیرگەی کارکردنی مۆدێل
A model can look at only a limited amount of text in one request: its context window, counted in tokens. Everything must fit inside it: the app's hidden instructions (the system prompt), the conversation so far, any attached files, the new question and the answer the model writes. Anthropic's documentation calls it the model's working memory, different from the huge training data it learned from.
لە یەک تەنیا دەتوانێت سەیری بڕێکی سنووردار دەق بکات: ، کە بە دەپێورێت. هەموو شتێک دەبێت تێیدا جێی ببێتەوە: ڕێنماییە شاراوەکانی (هەوێنی سیستەم)، گفتوگۆکە تا ئێستا، هەر فایلێکی هاوپێچ، پرسیارە نوێیەکە و ئەو وەڵامەی دەینووسێت. بەڵگەنامەکانی Anthropic پێی دەڵێن بیرگەی کارکردنـی ، کە جیاوازە لەو گەورەیەی لێی فێر بووە.
Windows have grown fast. GPT-2 (2019) could see 1,024 tokens. In November 2026 the main Claude and Gemini models see 1 million tokens and the GPT-6 family 1.05 million; one answer can be up to 128,000 tokens on Claude and GPT. On Claude's newest tokenizer, 1 million tokens are about 555,000 English words: several long books.
خێرا گەورە بوون. GPT-2 (2019) دەیتوانی 1,024 ببینێت. لە مانگی 11ی 2026دا گرنگترین Claude و Gemini 1 ملیۆن دەبینن و خێزانی GPT-6 1.05 ملیۆن؛ یەک وەڵام دەتوانێت لە Claude و GPTـدا تا 128,000 بێت. لەسەر نوێترین بەرنامەی Claude، 1 ملیۆن نزیکەی 555,000 وشەی ئینگلیزییە: چەند کتێبێکی درێژ.
A context window is like a classroom whiteboard: the teacher can use only what is written on it now. Last week's lesson is not there unless someone writes it again; when the board is full, something must be wiped.
وەک تەختەی پۆلێکە: مامۆستا تەنیا دەتوانێت ئەوە بەکار بهێنێت کە ئێستا لەسەر تەختەکە نووسراوە. وانەی هەفتەی ڕابردوو لەوێ نییە مەگەر کەسێک دووپاتە بینووسێتەوە و کاتێک تەختەکە پڕ دەبێت، دەبێت شتێک بسڕدرێتەوە.
Where the analogy stops workingسنووری ئەم نموونەیە
A teacher reads the whole board equally well; a model reads a very long context less carefully (long contexts).
مامۆستا هەموو تەختەکە بە یەکسانی دەخوێنێتەوە. لە کۆنتێکستێکی زۆر درێژدا کەمتر بە وردی سەرنج دەدات (کۆنتێکستی درێژ).
The context window: the model's working memoryپەنجەرەی کۆنتێکست: بیرگەی کارکردنی مۆدێل
Does it fit? A budget check in Pythonجێی دەبێتەوە؟ پشکنینی بودجە بە Python
Karwan wants Shilan's helper to answer questions about a 300-page book. Before he sends it, he estimates the tokens. The rule of thumb is rough, but it shows the idea: everything sent plus room for the answer must stay under the window.
کاروان دەیەوێت یاریدەدەرەکەی شیلان وەڵامی پرسیار لەبارەی کتێبێکی 300 لاپەڕەیی بداتەوە. پێش ناردن، مەزەندە دەکات. یاسا نزیکەییەکە وردی نییە، بەڵام بیرۆکەکە پیشان دەدات: هەموو ئەوەی دەنێردرێت لەگەڵ جێگەی وەڵام دەبێت لە ژێر بمێنێتەوە.
WINDOW = 1_000_000 # tokens the model can see at once (main models, November 2026)
MAX_OUTPUT = 32_000 # tokens kept free for the answer
CHARS_PER_TOKEN = 4 # Google's rule of thumb for English text on Gemini
def fits(parts):
used = sum(chars // CHARS_PER_TOKEN for chars in parts.values())
total = used + MAX_OUTPUT
print(f"{used:,} + {MAX_OUTPUT:,} for the answer = {total:,} of {WINDOW:,}")
return total <= WINDOW
book = 300 * 1_800 # 300 pages, about 1,800 characters each
request = {"instructions": 2_000, "book": book, "question": 400}
print("one book fits:", fits(request))
request["book"] = 10 * book # a shelf of ten such books
print("ten books fit:", fits(request))135,600 + 32,000 for the answer = 167,600 of 1,000,000 one book fits: True 1,350,600 + 32,000 for the answer = 1,382,600 of 1,000,000 ten books fit: False
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
A rough English rule. Claude's newest tokenizer gives about 2.5 characters per token, so the same book counts more tokens there (exercise 3).
یاسایەکی نزیکەیی بۆ ئینگلیزی. نوێترین بەرنامەی Claude بۆ هەر نزیکەی 2.5 پیت دەدات، بۆیە هەمان کتێب لەوێ زیاتر دەژمێرێت (ڕاهێنانی 3).
- Lineدێڕی 6
sum(...)adds the estimated tokens of every part;//is whole-number division.sum(...)ژمارەی مەزەندەکراوی هەموو بەشەکان کۆ دەکاتەوە؛//دابەشکردنی ژمارەی تەواوە.
import anthropic
client = anthropic.Anthropic()
response = client.messages.count_tokens(
model="claude-opus-5-5",
system="You answer questions about one book.",
messages=[{"role": "user", "content": "Who is the main character?"}],
)
print(response.json())No memory between requestsهیچ بیرگەیەک لە نێوان داواکارییەکاندا نییە
The model itself remembers nothing from one request to the next: the API is stateless. So how does a chat continue? The app keeps the conversation and sends all of it again with every new message: turn 1 sends the first question; turn 2 sends question 1, answer 1 and question 2; and so on. Each turn is longer than the last, which is why long chats get slower and cost more (Token optimization).
خودی هیچ شتێک لە بۆ داهاتوو لەبیر ناکات: . کەواتە گفتوگۆ چۆن بەردەوام دەبێت؟ گفتوگۆکە هەڵدەگرێت و لەگەڵ هەر نامەیەکی نوێدا هەمووی دووپاتە دەنێرێتەوە: نۆرەی 1 پرسیاری یەکەم دەنێرێت؛ نۆرەی 2 پرسیاری 1، وەڵامی 1 و پرسیاری 2 دەنێرێت؛ بەم شێوەیە. هەر نۆرەیەک لە نۆرەی پێشوو درێژترە، بۆیە گفتوگۆ درێژەکان هێواشتر دەبن و تێچوویان زیاتر دەبێت (باشترینکردنی تۆکن).
When an app says it remembers you, the app has saved notes about you and quietly puts them into the context of new chats. The model's weights did not change; it learned nothing about you.
کاتێک دەڵێت تۆی لە بیرە، تێبینیی لەبارەی تۆ پاشەکەوت کردووە و بە بێدەنگی دەیانخاتە ناو کۆنتێکستی گفتوگۆ نوێکانەوە. نەگۆڕاون؛ هیچ شتێکی لەبارەی تۆ فێر نەبووە.


When the window is long, or fullکاتێک پەنجەرەی کۆنتێکست درێژ یان پڕ دەبێت
A bigger window does not mean the model reads everything equally well. Anthropic's documentation warns that as the number of tokens grows, accuracy and recall get worse, and calls this context rot. So give the model the parts that matter, not everything you have; the next lesson shows how to fetch the right parts (RAG).
گەورەتر واتای ئەوە نییە کە هەموو شتێک بە یەکسانی باش دەخوێنێتەوە. بەڵگەنامەکانی Anthropic ئاگادار دەکەنەوە کە هەتا ژمارەی زیاد بکات، وردی و بیرهێنانەوە خراپتر دەبن و پێی دەڵێن . بۆیە ئەو بەشانەی گرنگن بدە بە ، نەک هەموو ئەوەی هەتە؛ وانەی داهاتوو پیشان دەدات چۆن بەشە دروستەکان بهێنیت (RAG).
And when it is full? On the Claude API, as of November 2026:
ئەی کاتێک پڕ دەبێت؟ لە Claude APIـدا، لە مانگی 11ی 2026دا:
- The input alone is too long → the request is refused with a 400 error (prompt is too long).
- The answer reaches the end of the window → it stops with the stop reason
model_context_window_exceeded. - The answer reaches the
max_tokensyou set → it stops withmax_tokens. A stopped answer is cut off, not finished.
- بەتەنیا زۆر درێژە ← بە هەڵەی 400 ڕەت دەکرێتەوە (prompt is too long).
- وەڵامەکە دەگاتە کۆتایی ← دەوەستێت بە هۆکاری وەستانی
model_context_window_exceeded. - وەڵامەکە دەگاتە ئەو
max_tokensـەی داتناوە ← بەmax_tokensدەوەستێت. وەڵامێکی وەستاو بڕاوە، نەک تەواو.
Chat apps hide this from you: some drop the oldest messages first, others replace them with a short summary (context compaction).
ئەپەکانی گفتوگۆ ئەمە لە تۆ دەشارنەوە: هەندێکیان سەرەتا کۆنترین نامەکان فڕێ دەدەن و هەندێکی تر کورتەیەکی کورتیان لەبری دادەنێن (پوختکردنەوەی کۆنتێکست).
Common mistakesهەڵە باوەکان
A programming mistake: computing the softmax with math.exp(score) directly. Real scores can be large, and e to the power 1,000 is too big for a Python float:
هەڵەیەکی بەرنامەسازی: ژماردنی ڕاستەوخۆ بە math.exp(score). نمرە ڕاستەقینەکان لەوانەیە گەورە بن و e بە توانی 1,000 بۆ floatـی Python زۆر گەورەیە:
import math
scores = {"sugar": 1000.0, "lemon": 999.0}
powers = {w: math.exp(s) for w, s in scores.items()}
print(powers)Traceback (most recent call last):
File "overflow.py", line 4, in <module>
powers = {w: math.exp(s) for w, s in scores.items()}
~~~~~~~~^^^
OverflowError: math range errorimport math
scores = {"sugar": 1000.0, "lemon": 999.0}
top = max(scores.values())
powers = {w: math.exp(s - top) for w, s in scores.items()}
total = sum(powers.values())
print({w: round(p / total, 2) for w, p in powers.items()}){'sugar': 0.73, 'lemon': 0.27}Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4-5
After subtracting 1,000, the scores are 0 and −1, so
math.expgives 1 and 0.37. The probabilities are the same as before; only the overflow is gone.دوای دەرکردنی 1,000، نمرەکان دەبن بە 0 و −1، بۆیە
math.exp1 و 0.37 دەدات. وەک پێشوون؛ تەنیا نەماوە.
Exercisesڕاهێنانەکان
Use the 30 merges that toy_bpe.py learned (the words the, reads, teacher, student and book are whole tokens with </w>; read, student, teacher and lesson also exist without it). Tokenize the teachers read the book. How many tokens? Check with the program.
ئەو 30 بەیەکگەیاندنە بەکار بهێنە کە toy_bpe.py فێریان بوو (وشەکانی the، reads، teacher، student و book لەگەڵ </w> تەواون؛ read، student، teacher و lesson بەبێ ئەویش هەن). the teachers read the book بکە بە . چەند ؟ بە بیپشکنە.
Show answerوەڵامەکە پیشان بدە
8 tokens: the</w> teacher s </w> read </w> the</w> book</w>. teachers is new, so it is written as the known piece teacher plus s; read without s never got its end mark joined, so </w> stays separate. The program prints ['the</w>', 'teacher', 's', '</w>', 'read', '</w>', 'the</w>', 'book</w>'].
8 : the</w> teacher s </w> read </w> the</w> book</w>. teachers نوێیە، بۆیە بە پارچە ناسراوەکەی teacher لەگەڵ s دەنووسرێت؛ read بەبێ s هەرگیز نیشانەی کۆتایی پێوە نەلکاوە، بۆیە </w> جیا دەمێنێتەوە. ['the</w>', 'teacher', 's', '</w>', 'read', '</w>', 'the</w>', 'book</w>'] چاپ دەکات.
After «دڵۆپ دڵۆپ دەبێتە» a model gives the scores دەریا 2, ڕووبار 1 and لافاو 0. Compute the probabilities at temperature 1 and at temperature 2 (use e ≈ 2.718; round to two decimals). Which choice changes more?
دوای «دڵۆپ دڵۆپ دەبێتە» ئەم نمرانە دەدات: دەریا 2، ڕووبار 1 و لافاو 0. لە 1 و 2دا بژمێرە (e ≈ 2.718؛ بۆ دوو ژمارەی دوای فاریزە نزیکی بکەرەوە). کام هەڵبژاردن زیاتر دەگۆڕێت؟
Show answerوەڵامەکە پیشان بدە
دەریا falls from 67 % to 51 %; لافاو more than doubles, from 9 % to 19 %. A higher temperature spreads the chances. (Checked with python3.13.)
دەریا لە 67 %ـەوە دادەبەزێت بۆ 51 %؛ لافاو زیاتر لە دوو هێندە دەبێت، لە 9 %ـەوە بۆ 19 %. بەرزتر دەرفەتەکان بڵاو دەکاتەوە. (بە python3.13 پشکنراوە.)
In context_budget.py, set CHARS_PER_TOKEN = 2.5 (about right for Claude's newest tokenizer). (a) How many tokens does one 300-page book need now? (b) With 32,000 tokens kept for the answer, how many such books fit in 1,000,000 tokens with 4 characters per token, and how many with 2.5?
لە context_budget.pyـدا CHARS_PER_TOKEN = 2.5 دابنێ (نزیکەی ڕاستە بۆ نوێترین بەرنامەی Claude). (a) کتێبێکی 300 لاپەڕەیی ئێستا چەند دەوێت؟ (b) بە هێشتنەوەی 32,000 بۆ وەڵام، چەند کتێبی لەو جۆرە لە 1,000,000 جێیان دەبێتەوە بە 4 پیت بۆ هەر و چەند بە 2.5؟
Show answerوەڵامەکە پیشان بدە
(a) 540,000 ÷ 2.5 = 216,000 tokens for the book (plus 960 for the instructions and question). (b) With 4 characters per token a book is 135,000 tokens: 7 books fit (945,000 + 600 + 32,000 = 977,600), 8 do not. With 2.5 a book is 216,000 tokens: only 4 fit (864,000 + 960 + 32,000 = 896,960). The same text costs more on a tokenizer that makes smaller pieces, so always count with the model you will use.
(a) 540,000 ÷ 2.5 = 216,000 بۆ کتێبەکە (لەگەڵ 960 بۆ ڕێنمایی و پرسیار). (b) بە 4 پیت بۆ هەر ، کتێبێک 135,000 : 7 کتێب جێیان دەبێتەوە (945,000 + 600 + 32,000 = 977,600)، 8 نا. بە 2.5 کتێبێک 216,000 : تەنیا 4 جێیان دەبێتەوە (864,000 + 960 + 32,000 = 896,960). هەمان دەق لەسەر بەرنامەیەکی کە پارچەی بچووکتر دروست دەکات تێچووی زیاترە، بۆیە هەمیشە بەو دەژمێرە کە بەکاری دەهێنیت.
Shilan's helper has a 100-token system prompt. Each customer question is 50 tokens and each answer 200 tokens. The API is stateless, so every turn re-sends everything. How many input tokens does turn 3 send, and how many input tokens do the three turns send together?
یاریدەدەرەکەی شیلان 100 تۆکنی هەیە. هەر پرسیارێکی کڕیار 50 و هەر وەڵامێک 200 . ، بۆیە هەر نۆرەیەک هەموو شتێک دووپاتە دەنێرێتەوە. نۆرەی 3 چەند دەنێرێت و هەر سێ نۆرەکە پێکەوە چەند؟
Show answerوەڵامەکە پیشان بدە
Turn 1: 100 + 50 = 150. Turn 2: 100 + 50 + 200 + 50 = 400. Turn 3: 100 + 50 + 200 + 50 + 200 + 50 = 650. Together 150 + 400 + 650 = 1,200 input tokens, for only 150 tokens of new questions. This is why long chats get expensive.
نۆرەی 1: 100 + 50 = 150. نۆرەی 2: 100 + 50 + 200 + 50 = 400. نۆرەی 3: 100 + 50 + 200 + 50 + 200 + 50 = 650. پێکەوە 150 + 400 + 650 = 1,200 ، بۆ تەنیا 150 پرسیاری نوێ. بۆیە گفتوگۆ درێژەکان گران دەبن.
Summaryپوختە
- A tokenizer cuts text into tokens and gives each an id from a fixed vocabulary; byte-pair encoding learns the pieces by joining the most frequent pairs.
- Kurdish usually needs more tokens than English (up to 15× between languages; 2-byte letters), so it costs more and fills the window sooner.
- Writing is a loop: read all tokens → score every token → softmax → pick → add → repeat. Inside, transformer layers use self-attention and feed-forward networks.
- Greedy decoding always takes the top token; sampling draws by chance; temperature and top-p shape the chances. In November 2026 the newest Claude, Gemini and GPT models set this for you.
- The context window (about 1 million tokens on the main models, November 2026) holds everything in one request, including the answer; the API is stateless, so the app re-sends the conversation; long contexts suffer from context rot.
- بەرنامەی دەق دەکات بە و ژمارەیەکی ناسنامە لە جێگیر دەداتە هەریەکەیان؛ پارچەکان بە بەیەکگەیاندنی زۆرترین جووتی دووپاتە فێر دەبێت.
- کوردی زۆرجار زیاتری لە ئینگلیزی دەوێت (تا 15 هێندە لە نێوان زمانەکاندا؛ پیتی 2 بایتی)، بۆیە گرانترە و زووتر پڕ دەکات.
- نووسین : هەموو بخوێنەوە ← نمرە بدە بە هەموویان ← ← هەڵبژێرە ← زیادی بکە ← دووپاتە. لە ناوەوە، و تۆڕی feed-forward بەکار دەهێنن.
- هەمیشە بەرزترین وەردەگرێت؛ بە هەڵکەوت دەردەهێنێت؛ و top-p دەرفەتەکان دادەڕێژن. لە مانگی 11ی 2026دا نوێترین Claude، Gemini و GPT ئەمە بۆ تۆ دادەنێن.
- (نزیکەی 1 ملیۆن لە مۆدێلە سەرەکییەکاندا، مانگی 11ی 2026) هەموو شتێکی یەک لەخۆ دەگرێت، وەڵامەکەشی؛ ، بۆیە گفتوگۆکە دووپاتە دەنێرێتەوە؛ کۆنتێکستی درێژ تووشی دەبێت.
Next: how the model turns tokens into meaning, and how the same idea powers search by meaning and answers from your own documents: Meaning as numbers.
داهاتوو: چۆن دەکات بە واتا و چۆن هەمان بیرۆکە گەڕان بەپێی واتا و وەڵامدانەوە لە بەڵگەنامەکانی خۆتەوە دەجووڵێنێت: مانا وەک ژمارە.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Meaning as Numbers: Vectors and Embeddingsمانا وەک ژمارە: ئاڕاستەبڕەکان و ئیمبەدینگەکان
Related lecturesوانە پەیوەندیدارەکان
- Probability in Computing: Speech, Predictive Text and Riskبەکارهێنانی ئەگەر لە بواری کۆمپیوتەردا: ئاخاوتن، پێشبینیی دەق و مەترسی
- Neural Networks and Deep Learningتۆڕە دەمارییەکان و فێربوونی قووڵ
- Machine Learning Basics (Supplementary)بنەماکانی فێربوونی ئامێر (زیادە)
- Modules and importمۆدیوڵەکان و import
- ASCII and Character CodesASCII و کۆدەکانی نووسە
- Meaning as Numbers: Vectors and Embeddingsمانا وەک ژمارە: ئاڕاستەبڕەکان و ئیمبەدینگەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- Sennrich, Haddow and Birch (2015): Neural Machine Translation of Rare Words with Subword Units (byte-pair encoding)
- Petrov, La Malfa, Torr and Bibi (2023, NeurIPS): Language Model Tokenizers Introduce Unfairness Between Languages (up to 15× longer)
- Vaswani and others (2017): Attention Is All You Need (the transformer)
- Google Machine Learning Crash Course: LLMs, transformers and self-attention
- Stanford CS336 Language Modeling from Scratch (Spring 2025): lecture 1, tokenization
- Hugging Face Transformers: Generation strategies (greedy search, sampling)
- Hugging Face: GPT-2 model configuration (vocabulary 50,257, context 1,024)
- Claude docs: Models overview (1M context, 128K output, 555k words per 1M tokens; checked 2026-11-10)
- Claude docs: Context windows (working memory, context rot, overflow behaviour)
- Claude docs: API versions (2026-10-21 removes temperature, top_p and top_k)
- Claude docs: Token counting (free estimate; newer tokenizer ≈ 30 % more tokens)
- Google Gemini API: Understand and count tokens (≈ 4 characters per token)
- Google Gemini API: Text generation (Gemini 4 and 3.6+ ignore custom sampling values)
- OpenAI API docs: Using GPT-6 (sampling parameters only with reasoning effort none) and Models (1.05M context)
- Brendan Bycroft: LLM Visualization (3D walk-through of a GPT-style model)
- Python docs: math (exp, OverflowError) and random (choices, seed)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.