Chapterبەش 3 · Lectureوانە 3.2

Token Optimization: Saving Cost, Time and Contextباشترینکردنی تۆکن: پاشەکەوتی پارە، کات و شوێن

Why tokens matter (money, time, the context window, rate limits); how pricing works, with input, output, thinking and tool tokens (example prices as of November 2026); estimating tokens and why Kurdish needs more; a cost calculator in Python; trimming prompts and controlling the output; choosing the right model size; prompt caching; the cost of a growing chat and context compaction; batch jobs and rate limits; measuring usage first; and the energy behind every token.

بۆچی گرنگن (پارە، کات، ، )؛ نرخدانان چۆن کار دەکات، بە ، ، بیرکردنەوە و ئامراز (نرخی نموونە بەپێی تشرینی دووەمی ⁦2026⁩)؛ مەزەندەکردنی و بۆچی کوردی زیاتری دەوێت؛ حاسیبەیەکی تێچوو بە Python؛ کورتکردنەوەی و کۆنترۆڵکردنی ؛ هەڵبژاردنی قەبارەی گونجاوی ؛ ؛ تێچووی گفتوگۆیەکی گەورەبوو و ؛ و سنووری داواکاری؛ پێوانی بەکارهێنان پێش هەموو شتێک؛ و ئەو وزەیەی لە پشت هەموو .

22 min read22 خولەک خوێندنەوە · 19 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Give four reasons why tokens matter and read a token bill with input, output, thinking and tool tokens.
  • Estimate tokens and a monthly cost in Python, and explain why Kurdish text costs more.
  • Cut tokens without cutting meaning: trim prompts, control the output and choose the right model size.
  • Use prompt caching, context compaction and batches, and calculate what they save.
  • Measure usage before optimising, set limits against runaway costs, and link tokens to energy use.
  • چوار هۆکار بۆ گرنگیی بدەیت و پسوولەیەکی بخوێنیتەوە بە ، ، بیرکردنەوە و ئامرازەوە.
  • و تێچووی مانگانە لە مەزەندە بکەیت و ڕوون بکەیتەوە بۆچی دەقی کوردی گرانترە.
  • کەم بکەیتەوە بێ ئەوەی واتا کەم بکەیتەوە: کورت بکەیتەوە، کۆنترۆڵ بکەیت و قەبارەی گونجاوی هەڵبژێریت.
  • ، و بەکار بهێنیت و پاشەکەوتەکەیان بژمێریت.
  • پێش باشترکردن بەکارهێنان بپێویت، سنوور دژی تێچووی لە کۆنترۆڵ دەرچوو دابنێیت و بە بەکارهێنانی وزەوە ببەستیتەوە.

A telegram paid by the wordبروسکەیەک کە پارەی هەر وشەیەکی دەدرا

Before mobile phones, people sent telegrams and paid for every word. So they wrote “ARRIVE ERBIL FRIDAY 10 AM”, not a polite letter. But a telegram that said only “ARRIVE FRIDAY” saved two coins and left the family guessing which city and what time.

پێش مۆبایل، خەڵک بروسکەیان دەنارد و پارەی هەموو وشەیەکیان دەدا. بۆیە دەیاننووسی «هەینی ⁦10⁩ی بەیانی دەگەمە هەولێر»، نەک نامەیەکی درێژی ڕێزدار. بەڵام بروسکەیەک کە تەنیا «هەینی دەگەم» بێت، دوو پارە پاشەکەوت دەکات و خێزانەکە لە مەزەندەدا دەهێڵێتەوە: کام شار و چ کاتێک؟

Language models are paid in the same way, in tokens (tokens). Token optimization means getting the same good answer with fewer tokens: tell the model everything it needs, and nothing more. Cutting what it needs is a false saving, because a wrong answer costs a second request, or worse.

پارەی بە هەمان شێوە دەدرێت، بە (تۆکنەکان). واتە بەدەستهێنانی هەمان وەڵامی باش بە کەمتر: هەرچی پێویستیەتی پێی بڵێ، نە زیاتر. بڕینی ئەوەی پێویستیەتی پاشەکەوتێکی درۆیینە، چونکە وەڵامێکی هەڵە دووەمی دەوێت، یان خراپتر.

Three telegram forms. Too long: “Hello dear brother, I hope you and the family are well, I wanted to tell you that I will arrive in Erbil on Friday at ten”: 26 words, 26 coins; you pay for every polite word. Just right: “Arrive Erbil Friday 10 am”: 5 words, 5 coins; every fact is there. Too short: “Arrive Friday”: 2 words, 2 coins; which city, what time? Below: a model's bill works the same way, counted in tokens: say everything it needs, and nothing more.سێ فۆرمی بروسکە. زۆر درێژ: «سڵاو برای ئازیز، هیوادارم خۆت و خێزانەکەت باش بن، ویستم پێت بڵێم کە ڕۆژی هەینی کاتژمێر دەی بەیانی دەگەمە هەولێر»: 20 وشە، 20 پارە؛ پارە بۆ هەموو وشەیەکی ڕێزدار دەدەیت. گونجاو: «هەینی 10ی بەیانی دەگەمە هەولێر»: 5 وشە، 5 پارە؛ هەموو زانیارییەکان تێدان. زۆر کورت: «هەینی دەگەم»: 2 وشە، 2 پارە؛ کام شار؟ چ کاتێک؟ لە خوارەوە: پسوولەی مۆدێل بە هەمان شێوەیە و بە تۆکن دەژمێردرێت: هەرچی پێویستیەتی بڵێ، نە زیاتر.
Pay for what is needed, not for padding, and do not cut the facts.پارە بۆ ئەوە بدە کە پێویستە، نەک بۆ زیادە؛ ڕاستییەکانیش مەبڕە.CêIT

Why tokens matter: four reasonsبۆچی تۆکنەکان گرنگن: چوار هۆکار

  • Money. Providers charge per million tokens, both for what you send (input) and for what the model writes (output).
  • Time. The model writes one token after another, so a long answer takes longer to arrive. This waiting is the latency the user feels.
  • Space. The prompt and the answer must fit in the context window (the context window), and long, noisy contexts make answers worse (context rot).
  • Limits. Providers cap how many requests and tokens an account may use per minute (rate limiting, also used against floods of traffic: defending against DDoS). Over the limit, the API answers with status code 429 and you must wait.
  • پارە. دابینکەران بۆ هەر ملیۆن پارە وەردەگرن، هەم بۆ ئەوەی دەینێریت () و هەم بۆ ئەوەی دەینووسێت ().
  • کات. یەک لە دوای یەک دەنووسێت، بۆیە وەڵامێکی درێژ درەنگتر دەگات. ئەم چاوەڕوانییە ئەو کە هەستی پێ دەکات.
  • شوێن. و وەڵام دەبێت لە جێیان ببێتەوە (پەنجەرەی کۆنتێکست) و کۆنتێکستی درێژ و پڕ لە زیادە وەڵامەکان خراپتر دەکات ().
  • سنوور. دابینکەران سنوور بۆ ژمارەی و لە هەر خولەکێکدا بۆ هەر هەژمارێک دادەنێن (، کە دژی لافاوی هاتوچۆش بەکار دێت: بەرگری لە DDoS). ئەگەر لە سنوور تێپەڕیت، بە 429 وەڵام دەداتەوە و دەبێت چاوەڕێ بکەیت.
Four tiles. Money, a coin with a dollar sign: you pay for every token, in and out. Time, a clock: more output tokens, a slower answer. Space, a full bar: all of it must fit in the context window. Limits, a gauge near its maximum: providers cap tokens per minute.چوار خانە. پارە، دراوێک بە نیشانەی دۆلار: پارە بۆ هەموو تۆکنێک دەدەیت، چوون و هاتن. کات، کاتژمێرێک: تۆکنی دەرچووی زیاتر، وەڵامی خاوتر. شوێن، بارێکی پڕ: هەمووی دەبێت لە پەنجەرەی کۆنتێکستدا جێی ببێتەوە. سنوور، پێوەرێک نزیک لە بەرزترین ئاستی: دابینکەران تۆکنەکان لە هەر خولەکێکدا سنووردار دەکەن.
Every token costs money, time, space and part of your limit.هەموو پارە، کات، شوێن و بەشێک لە سنوورەکەت دەخوات.CêIT

How pricing worksنرخدانان چۆن کار دەکات

Prices are given in US dollars per million tokens, separately for input and output. On the main models in November 2026, output costs five times as much as input (the families and their prices). Three kinds of tokens are easy to forget:

نرخەکان بە دۆلاری ئەمریکی بۆ هەر ملیۆن دەدرێن، بە جیا بۆ و . لە سەرەکییەکانی تشرینی دووەمی ⁦2026⁩دا، پێنج ئەوەندەی گرانە (خێزانەکان و نرخەکانیان). سێ جۆر بە ئاسانی لەبیر دەچن:

  • Thinking tokens: a reasoning model's hidden thinking is billed as output, even when you never see it (reasoning models).
  • Tool tokens: tool descriptions are input, and Claude also adds a hidden tool-use system prompt, 286 tokens on Claude Opus 5.5 and Sonnet 5.5 (tool use).
  • The whole prompt, every time: the system prompt, the history and the files are sent, and paid for, on each request.
  • بیرکردنەوە: بیرکردنەوە شاراوەکەی وەک پارەی لێ وەردەگیرێت، تەنانەت ئەگەر هەرگیز نەیبینیت (مۆدێلە بیرکەرەوەکان).
  • ئامراز: وەسفی ئامرازەکان و Claude شاراوەی زیاد دەکات، ⁦286⁩ لە ⁦Claude Opus 5.5⁩ و ⁦Sonnet 5.5⁩دا (بەکارهێنانی ئامراز).
  • هەموو ، هەموو جارێک: ، مێژووی گفتوگۆ و فایلەکان لە هەموو دەنێردرێن و پارەیان دەدرێت.
A receipt for one request to Shilan's helper, at example prices of November 2026: $2 per million input tokens and $10 per million output tokens. Input lines: system prompt 1,200; tool definitions and the hidden tool prompt 600; part of the book list 2,500; the customer's question 10. Output lines: thinking 400 (hidden, billed as output); the answer 250. Input 4,310 tokens cost $0.00862; output 650 tokens cost $0.00650; one request $0.01512; 10,000 requests a month $151.20. Notes: output costs 5 times input; hidden tokens are paid too.پسوولەی داواکارییەک بۆ یارمەتیدەرەکەی شیلان، بە نرخی نموونەی تشرینی دووەمی 2026: $2 بۆ هەر ملیۆن تۆکنی نێوخستە و $10 بۆ هەر ملیۆن تۆکنی دەرخستە. هێڵەکانی نێوخستە: هەوێنی سیستەم 1,200؛ پێناسەی ئامرازەکان و هەوێنی شاراوەی ئامراز 600؛ بەشێک لە لیستی کتێبەکان 2,500؛ پرسیاری کڕیارەکە 10. هێڵەکانی دەرخستە: بیرکردنەوە 400 (شاراوە، وەک دەرخستە دەژمێردرێت)؛ وەڵامەکە 250. نێوخستەی 4,310 تۆکن $0.00862؛ دەرخستەی 650 تۆکن $0.00650؛ یەک داواکاری $0.01512؛ 10,000 داواکاری لە مانگێکدا $151.20. تێبینی: دەرخستە 5 ئەوەندەی نێوخستە گرانە؛ تۆکنە شاراوەکانیش پارەیان دەدرێت.
A token bill: the hidden lines are paid too.پسوولەی : هێڵە شاراوەکانیش پارەیان دەدرێت.CêIT

Estimating tokens, and why Kurdish needs moreمەزەندەکردنی تۆکنەکان و بۆچی کوردی زیاتری دەوێت

Rules of thumb exist only for English and differ by company (as of November 2026): Google says a Gemini token is about 4 characters, and 100 tokens are about 60 to 80 English words; Anthropic says 1 million tokens are about 555,000 English words on Claude's current tokenizer. Tokenizers change between versions too: Anthropic notes that its newer tokenizer makes about 30% more tokens from the same text.

یاسای خێرای مەزەندە تەنیا بۆ ئینگلیزی هەیە و لە کۆمپانیایەکەوە بۆ یەکێکی تر جیاوازە (بەپێی تشرینی دووەمی ⁦2026): Google⁩ دەڵێت Gemini نزیکەی 4 پیتە و ⁦100⁩ نزیکەی ⁦60⁩ تا ⁦80⁩ وشەی ئینگلیزییە؛ Anthropic دەڵێت 1 ملیۆن لەسەر ئێستای Claude نزیکەی ⁦555,000⁩ وشەی ئینگلیزییە. لە نێوان وەشانەکانیشدا دەگۆڕێت: Anthropic دەڵێت نوێترەکەی لە هەمان دەق نزیکەی ⁦30⁩٪ زیاتر دروست دەکات.

Kurdish needs many more, as the last lesson showed. How many? Petrov and colleagues (NeurIPS 2023) counted the same 2,000 sentences in many languages with GPT-4's tokenizer of that time: Central Kurdish needed about 4.8 times as many tokens as English, and Northern Kurdish about 2.2 times. Newer tokenizers may differ, so never budget Kurdish with English rules: count real texts with your provider's free token counter.

کوردی زۆر زیاتری دەوێت، وەک وانەی پێشوو پیشانی دا. چەند زیاتر؟ Petrov و هاوکارانی ⁦(NeurIPS 2023)⁩ هەمان ⁦2,000⁩ ڕستەیان بە زۆر زمان و بە ئەو کاتەی ⁦GPT-4⁩ ژمارد: کوردیی ناوەندی نزیکەی ⁦4.8⁩ ئەوەندەی ئینگلیزی پێویست بوو و کوردیی باکووری نزیکەی ⁦2.2⁩ ئەوەندە. نوێتر لەوانەیە جیاواز بن، بۆیە هەرگیز بودجەی کوردی بە یاسای ئینگلیزی دامەنێ: دەقی ڕاستەقینە بە ئامرازی بێبەرانبەری ژماردنی دابینکەرەکەت بژمێرە.

Bar chart: the same 2,000 sentences counted with GPT-4's 2023 tokenizer. English 52,835 tokens (1.0 times); Turkish 101,176 (1.9); Northern Kurdish, Kurmanji, 116,457 (2.2); Arabic 160,485 (3.0); Persian 173,333 (3.3); Central Kurdish, Sorani, 253,685 (4.8), the longest bar. Source: Petrov et al., NeurIPS 2023, FLORES-200 sentences, cl100k_base tokenizer; newer tokenizers differ, so always count your own text with your provider's counter.هێڵکاریی ستوونی: هەمان 2,000 ڕستە بە تۆکنکردنی GPT-4ـی ساڵی 2023 ژمێردراون. ئینگلیزی 52,835 تۆکن (1.0 ئەوەندە)؛ تورکی 101,176 (1.9)؛ کوردیی باکووری، کرمانجی، 116,457 (2.2)؛ عەرەبی 160,485 (3.0)؛ فارسی 173,333 (3.3)؛ کوردیی ناوەندی، سۆرانی، 253,685 (4.8)، درێژترین ستوون. سەرچاوە: Petrov و هاوکارانی، NeurIPS 2023، ڕستەکانی FLORES-200، تۆکنکردنی cl100k_base؛ تۆکنکردنی نوێتر جیاوازن، بۆیە هەمیشە دەقی خۆت بە ئامرازی ژماردنی دابینکەرەکەت بژمێرە.
Central Kurdish needed about 4.8 times as many tokens as English in this 2023 measurement.لەم پێوانەیەی ⁦2023⁩دا کوردیی ناوەندی نزیکەی ⁦4.8⁩ ئەوەندەی ئینگلیزی پێویست بوو.CêIT

Cost in Pythonتێچوو لە Pythonـدا

Karwan estimates the monthly bill for Shilan's helper before he builds it. Each question comes with the shop's rules (1,200 tokens) and part of the book list (2,500 tokens). The prices are the example ones from the bill: $2 and $10 per million.

کاروان پێش دروستکردنی یارمەتیدەرەکەی شیلان، پسوولەی مانگانەی مەزەندە دەکات. هەموو پرسیارێک لەگەڵ یاساکانی دوکان (⁦1,200⁩ ) و بەشێک لە لیستی کتێبەکان (⁦2,500⁩ ) دەنێردرێت. نرخەکان هەمان نرخی نموونەی پسوولەکەن: ⁦$2⁩ و ⁦$10⁩ بۆ هەر ملیۆنێک.

Estimating Shilan's monthly billمەزەندەکردنی پسوولەی مانگانەی شیلانtoken_cost.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
# Estimate the cost of Shilan's helper (example prices, checked November 2026)
PER_MILLION = 1_000_000
PRICE_IN = 2.00      # US dollars per million input tokens (a middle-size model)
PRICE_OUT = 10.00    # US dollars per million output tokens

def estimate_tokens(text, chars_per_token=4):
    """A rough English estimate: about 4 characters per token."""
    return max(1, round(len(text) / chars_per_token))

def request_cost(input_tokens, output_tokens):
    return input_tokens / PER_MILLION * PRICE_IN + output_tokens / PER_MILLION * PRICE_OUT

question = "Do you have Mem u Zin, and how much is it?"
q_tokens = estimate_tokens(question)
print(f"question: {len(question)} characters, about {q_tokens} tokens")

rules = 1_200        # tokens: the shop's system prompt
book_list = 2_500    # tokens: the part of the catalogue sent with each question
for answer in (250, 750):          # an English answer, and a 3x longer Kurdish one
    tokens_in = rules + book_list + q_tokens
    one = request_cost(tokens_in, answer)
    print(f"{tokens_in} in + {answer} out: ${one:.5f} per request, "
          f"${one * 10_000:.2f} for 10,000 requests")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-4

Constants with the unit in a comment: prices are per million tokens and will change.

بە یەکەکەیان لە تێبینیدا: نرخەکان بۆ هەر ملیۆن و دەگۆڕێن.

Lineدێڕی 6-8

A rough English estimate; for Kurdish, count with the provider's tool instead.

مەزەندەیەکی خێرای ئینگلیزی؛ بۆ کوردی، لە جیاتی ئەمە بە ئامرازی دابینکەرەکە بژمێرە.

Lineدێڕی 19

750 tests a guess: Kurdish answers needing three times the tokens. Measure your own.

⁦750⁩ مەزەندەیەک تاقی دەکاتەوە: وەڵامی کوردی سێ ئەوەندە بخوازێت. هی خۆت بپێوە.

The questions cost the same in both rows, but the longer answers add $50 a month. The factor 3 is only an assumption here: measure your own answers.

پرسیارەکان لە هەردوو ڕیزدا هەمان نرخیان هەیە، بەڵام وەڵامە درێژترەکان مانگانە ⁦$50⁩ زیاد دەکەن. ژمارە 3 لێرەدا تەنیا مەزەندەیەکە: وەڵامەکانی خۆت بپێوە.

Two stacked bars for 10,000 requests a month with 3,710 input tokens each. English answers of 250 tokens: input $74.20 plus output $25.00 equals $99.20. Kurdish answers, if they need three times the tokens (750): input $74.20 plus output $75.00 equals $149.20. The questions cost the same; the longer Kurdish answers add $50 a month. Measure, then budget.دوو ستوونی سەریەک بۆ 10,000 داواکاری لە مانگێکدا، هەر یەکەیان 3,710 تۆکنی نێوخستە. وەڵامی ئینگلیزیی 250 تۆکنی: نێوخستە $74.20 و دەرخستە $25.00، کۆی $99.20. وەڵامی کوردی، ئەگەر سێ ئەوەندە تۆکن بخوازێت (750): نێوخستە $74.20 و دەرخستە $75.00، کۆی $149.20. پرسیارەکان هەمان نرخیان هەیە؛ وەڵامە درێژە کوردییەکان مانگانە $50 زیاد دەکەن. بیپێوە، پاشان بودجە دابنێ.
Where the money goes: input and output for 10,000 requests a month.پارەکە بۆ کوێ دەچێت: و بۆ ⁦10,000⁩ لە مانگێکدا.CêIT

Cost in Pythonتێچوو لە Pythonـدا

Common mistake: per token or per million?هەڵەی باو: بۆ هەر تۆکنێک یان بۆ هەر ملیۆنێک؟

Always compare a new result with a rough hand estimate: 4,000 tokens at $2 per million is about one cent, not a millionth of one.

هەمیشە ئەنجامێکی نوێ لەگەڵ مەزەندەیەکی خێرای دەستیدا بەراورد بکە: ⁦4,000⁩ بە ⁦$2⁩ بۆ هەر ملیۆنێک نزیکەی یەک سەنتە، نەک بەشێک لە ملیۆنی سەنتێک.

Trim the prompt, not the meaningهەوێنەکە کورت بکەرەوە، نەک واتاکەی

Greetings, thanks and repeated instructions cost tokens on every request but give the model nothing it needs. The estimate here uses about 4 characters per token, as for English:

سڵاو، سوپاس و ڕێنماییی دووپاتە لە هەموو دەخۆن، بەڵام هیچ شتێک نادەن بە کە پێویستی بێت. مەزەندەکە لێرەدا نزیکەی 4 پیت بۆ هەر بەکار دەهێنێت، وەک بۆ ئینگلیزی:

Before and after trimmingپێش و دوای کورتکردنەوەtrim.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
def estimate_tokens(text, chars_per_token=4):
    return max(1, round(len(text) / chars_per_token))

before = ("Hello! I hope you are having a wonderful day. I was wondering if you could "
          "possibly help me with something. If it is not too much trouble, could you please "
          "summarise the attached shop report for me? Please summarise it in three bullet "
          "points. Thank you so much in advance!")
after = "Summarise the attached shop report in three bullet points."
too_short = "Summarise."

for name, text in [("before", before), ("after", after), ("too short", too_short)]:
    print(f"{name:9}: {len(text):3} characters, about {estimate_tokens(text):2} tokens")

Keep everything that changes the answer: the facts, the examples, the format and the reason. Then check that the shorter prompt still passes your test cases (test your prompt).

هەموو ئەو شتانە بهێڵەوە کە وەڵامەکە دەگۆڕن: ڕاستییەکان، نموونەکان، شێوەکە و هۆکارەکە. پاشان بپشکنە کە کورترەکە هێشتا لە دەردەچێت (هەوێنەکەت تاقی بکەرەوە).

Before, about 68 tokens: “Hello! I hope you are having a wonderful day. I was wondering if you could possibly help me with something. If it is not too much trouble, could you please summarise the attached shop report for me? Please summarise it in three bullet points. Thank you so much in advance!” An arrow says remove greetings and repeats. After, about 14 tokens: “Summarise the attached shop report in three bullet points.” Below, two boxes: too short, “Summarise.” (about 2 tokens): the format is lost and a long answer may cost more output; never cut what the model needs: facts, examples, the format and the reason.پێشتر، نزیکەی 68 تۆکن: «Hello! I hope you are having a wonderful day…» واتە سڵاو، هیوای ڕۆژێکی خۆش، داوای یارمەتی بە ڕێزەوە، دوو جار داوای پوختەکردنی ڕاپۆرتی دوکان بە سێ خاڵ و سوپاس. تیرێک دەڵێت سڵاو و دووپاتەکان لاببە. دوای، نزیکەی 14 تۆکن: «Summarise the attached shop report in three bullet points.» لە خوارەوە دوو سندوق: زۆر کورت، «Summarise.» (نزیکەی 2 تۆکن): شێوەکە ون بوو و وەڵامێکی درێژ دەرخستەی زیاتر تێدەچێت؛ ئەوەی مۆدێلەکە پێویستیەتی مەیبڕە: ڕاستییەکان، نموونەکان، شێوەکە و هۆکارەکە.
Cut padding, keep meaning: 68 → 14 tokens.زیادە ببڕە، واتا بهێڵەوە: ⁦68⁩ بۆ ⁦14⁩ .CêIT

Control the length of the answerدرێژیی وەڵامەکە کۆنترۆڵ بکە

Output tokens cost the most, so ask for the length and form you need: “three bullet points, under 60 words”, a small table, or JSON with fixed keys (structured output). For reasoning models, a lower effort level means less thinking and fewer output tokens.

لە هەمووان گرانترن، بۆیە داوای ئەو درێژی و شێوەیە بکە کە پێویستتە: «سێ خاڵ، کەمتر لە ⁦60⁩ وشە»، خشتەیەکی بچووک، یان بە جێگیر (دەرخستەی ڕێکخراو). بۆ ، ئاستی هەوڵـی نزمتر واتە بیرکردنەوەی کەمتر و کەمتر.

max_tokens is different: it is a hard ceiling, not a length request. The model does not plan for it; the answer is simply cut off at the limit and stops with max_tokens, as the last lesson showed. Use it as a safety cap, and ask for short answers in the prompt itself.

max_tokens جیاوازە: سنوورێکی ڕەقە، نەک داواکردنی درێژییەک. پلانی بۆ دانانێت؛ وەڵامەکە بە سادەیی لە سنوورەکەدا دەبڕدرێت و بە max_tokens دەوەستێت، وەک وانەی پێشوو پیشانی دا. وەک سنوورێکی سەلامەتی بەکاری بهێنە و لە خودی داوای وەڵامی کورت بکە.

Two answers. Left, asked for “three bullet points, under 60 words”: open 9:00 to 21:00, closed Fridays; Kurdish and English books; orders by phone or Facebook. Stop reason end_turn: short and finished. Right, only a cap, max_tokens = 40: a long sentence about the bookshop that breaks off in the middle, with a torn edge. Stop reason max_tokens: cut off, not finished.دوو وەڵام. لای چەپ، داوای «سێ خاڵ، کەمتر لە 60 وشە» کراوە: لە 9:00 تا 21:00 کراوەیە، هەینیان داخراوە؛ کتێبی کوردی و ئینگلیزی؛ داواکردن بە تەلەفۆن یان فەیسبووک. هۆکاری وەستان end_turn: کورت و تەواو. لای ڕاست، تەنیا سنوور، max_tokens = 40: ڕستەیەکی درێژ لەبارەی کتێبفرۆشییەکە کە لە ناوەڕاستدا دەبڕێت، بە لێوارێکی دڕاو. هۆکاری وەستان max_tokens: بڕاوە، تەواو نییە.
Ask for a short answer; a cap only cuts.داوای وەڵامی کورت بکە؛ سنوور تەنیا دەیبڕێت.CêIT

The right model for the jobمۆدێلی گونجاو بۆ کارەکە

Small models are fast and cheap, and good at sorting, extracting and short replies; big models are for hard reasoning (big, middle and small models). Anthropic's own advice is the same: a small model for simple tasks, a middle one for most work, the biggest for the most complex reasoning. Shilan's helper sorts 10,000 messages a month:

خێرا و هەرزانن و لە ، دەرهێنانی زانیاری و وەڵامی کورتدا باشن؛ گەورەکان بۆ بیرکردنەوەی قورسن (مۆدێلی گەورە، ناوەند و بچووک). ئامۆژگاریی خودی Anthropicـیش هەمانە: بچووک بۆ کاری سادە، ناوەندییەک بۆ زۆربەی کارەکان و گەورەترینیان بۆ ئاڵۆزترین بیرکردنەوە. یارمەتیدەرەکەی شیلان مانگانە ⁦10,000⁩ پەیام پۆلێن دەکات:

Monthly cost of sorting with three model sizesتێچووی مانگانەی پۆلێنکردن بە سێ قەبارەی مۆدێلtiers.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
# Sorting 10,000 customer messages a month: which model size?
# Example list prices, US dollars per million tokens (input, output), November 2026
PRICES = {"small": (0.10, 0.50), "middle": (2.00, 10.00), "large": (4.00, 20.00)}
MESSAGES = 10_000
TOKENS_IN, TOKENS_OUT = 400, 10     # one short message + instructions in, one word out

def monthly(model, share=1.0):
    price_in, price_out = PRICES[model]
    per_message = (TOKENS_IN * price_in + TOKENS_OUT * price_out) / 1_000_000
    return per_message * MESSAGES * share

for model in PRICES:
    print(f"{model:6} for every message: ${monthly(model):6.2f}")

# Routing: the small model sorts everything; the 5% it marks "unsure" go to the large model
routed = monthly("small") + monthly("large", share=0.05)
print(f"small + 5% sent to large: ${routed:.2f}")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-3

Example list prices of Claude Haiku 5.5, Sonnet 5.5 and Opus 5.5, November 2026.

نرخی نموونەی ⁦Claude Haiku 5.5⁩، ⁦Sonnet 5.5⁩ و ⁦Opus 5.5⁩، تشرینی دووەمی ⁦2026⁩.

Lineدێڕی 15-17

Routing: everything to the small model, and only the unsure 5% to the large one.

ناردن بەپێی پێویست: هەموو شتێک بۆ بچووک و تەنیا 5٪ـە نادڵنیاکە بۆ گەورە.

Count the cost per finished task, not per request: a cheap model that is often wrong costs retries, unhappy customers or a person's time. Sending each job to the model that fits it, small first and big only when needed, gives most of the saving.

تێچوو بۆ هەر کارێکی تەواوکراو بژمێرە، نەک بۆ هەر : هەرزان کە زۆرجار هەڵە دەکات، دووپاتەکردنەوە، کڕیاری ناڕازی یان کاتی مرۆڤێکی دەوێت. ناردنی هەر کارێک بۆ گونجاوی خۆی، سەرەتا بچووک و تەنیا کاتی پێویست گەورە، زۆربەی پاشەکەوتەکە دەدات.

10,000 messages a month go to a small model costing $0.45. The 95% it is sure about are done; the 5% it marks unsure go to a large model for another $0.90. Together $1.35 a month. A note: the large model for every message would cost $18.00, the middle model $9.00; measure quality on your own test cases first.10,000 پەیام لە مانگێکدا دەچنە مۆدێلێکی بچووکی زمان کە $0.45 تێدەچێت. ئەو 95٪ـەی دڵنیایە لێیان تەواو دەبن؛ ئەو 5٪ـەی بە نادڵنیا دیاریان دەکات دەچنە مۆدێلێکی گەورە بە $0.90ـی تر. پێکەوە $1.35 لە مانگێکدا. تێبینی: مۆدێلی گەورە بۆ هەموو پەیامێک $18.00 تێدەچێت و مۆدێلی ناوەند $9.00؛ سەرەتا کوالیتی بە حاڵەتەکانی تاقیکردنەوەی خۆت بپێوە.
Small first, big only when needed.سەرەتا بچووک، گەورە تەنیا کاتی پێویست.CêIT

Prompt caching: pay once for the fixed partکاشکردنی هەوێن: بۆ بەشە جێگیرەکە یەکجار پارە بدە

Many requests start with the same long text: tools, the system prompt, a handbook. With prompt caching, the provider keeps that processed beginning for a few minutes; a request that starts with exactly the same text reads it from the cache, faster and cheaper. On Claude (November 2026), a 5-minute cache write costs 1.25 × the input price (2 × for 1 hour) and a read costs 0.1 × (0.05 × on Opus 5.5 and Sonnet 5.5), for a cached beginning of at least 512 tokens.

زۆر بە هەمان دەقی درێژ دەست پێ دەکەن: ئامرازەکان، ، ڕێبەرێک. بە ، دابینکەر ئەو سەرەتا ئامادەکراوە بۆ چەند خولەکێک هەڵدەگرێت و کە ڕێک بە هەمان دەق دەست پێ بکات، لە دەیخوێنێتەوە، خێراتر و هەرزانتر. لە Claude (تشرینی دووەمی ⁦2026⁩)، نووسین لە 5 خولەکیدا ⁦1.25⁩ × نرخی تێدەچێت (⁦2⁩ × بۆ 1 کاتژمێر) و خوێندنەوە ⁦0.1 × (0.05⁩ × لە ⁦Opus 5.5⁩ و ⁦Sonnet 5.5⁩دا)، بۆ سەرەتایەکی کاشکراو کە لانیکەم ⁦512⁩ بێت.

The rule: fixed parts first, changing parts last. One changed character near the top, such as today's date and time, makes the whole beginning new again. OpenAI caches long prompts automatically from 1,024 tokens, and Gemini's newer models do it too; the same rule applies everywhere.

یاساکە: بەشە جێگیرەکان یەکەم، بەشە گۆڕاوەکان کۆتایی. گۆڕینی یەک پیت لە نزیک سەرەوە، وەک ڕێکەوت و کاتی ئەمڕۆ، هەموو سەرەتاکە دیسان نوێ دەکاتەوە. OpenAI درێژ لە ⁦1,024⁩ خۆکارانە کاش دەکات و نوێترەکانی Geminiـیش وا دەکەن؛ هەمان یاسا لە هەموو شوێنێک ڕاستە.

The baker's doughهەویری نانەواخانە

A baker in Erbil prepares one big bowl of dough at dawn and shapes each customer's bread from it. The dough is the slow, costly part; shaping is quick. Change the recipe at noon, and the dough must be made again.

نانەوایەک لە هەولێر بەرەبەیان یەک تەشت هەویری گەورە ئامادە دەکات و نانی هەر کڕیارێک لەوەوە دروست دەکات. هەویرەکە بەشە هێواش و گرانەکەیە؛ شێوەپێدان خێرایە. ئەگەر نیوەڕۆ ڕەچەتەکە بگۆڕیت، دەبێت هەویر دووپاتە بکرێتەوە.

Where the analogy stops workingسنووری ئەم نموونەیە

A cache lasts only minutes unless it is used.

تەنیا چەند خولەکێک دەمێنێت، مەگەر بەکار بهێنرێت.

An animation of four requests, each a row of parts: tools, system prompt, a handbook of 50,000 tokens, and a question. Request 1 at 9:00 writes the fixed beginning to the cache (×1.25). Requests 2 and 3 at 9:02 and 9:03 read the same beginning from the cache (×0.1); only the new question is processed at full price. Request 4 at 9:04 has the time put at the top, so the beginning changed, nothing matches and it is written again. Last frame: fixed parts first, changing parts last; the cache lives 5 minutes (or 1 hour), and every read starts the clock again.
An animation of four requests, each a row of parts: tools, system prompt, a handbook of 50,000 tokens, and a question. Request 1 at 9:00 writes the fixed beginning to the cache (×1.25). Requests 2 and 3 at 9:02 and 9:03 read the same beginning from the cache (×0.1); only the new question is processed at full price. Request 4 at 9:04 has the time put at the top, so the beginning changed, nothing matches and it is written again. Last frame: fixed parts first, changing parts last; the cache lives 5 minutes (or 1 hour), and every read starts the clock again.
Prompt caching: the same beginning is read cheaply until it changes.: هەمان سەرەتا بە هەرزانی دەخوێنرێتەوە تا دەگۆڕێت.CêIT

Prompt caching: pay once for the fixed partکاشکردنی هەوێن: بۆ بەشە جێگیرەکە یەکجار پارە بدە

How much does caching save?کاشکردنی هەوێن چەند پاشەکەوت دەکات؟

Aram's college has a helper for its 50,000-token student handbook, and twenty questions arrive within a few minutes. Only input is counted here, because the answers cost the same either way:

کۆلێژەکەی ئارام یارمەتیدەرێکی بۆ ڕێبەرە ⁦50,000⁩ خوێندکاران هەیە و بیست پرسیار لە چەند خولەکێکدا دێن. لێرەدا تەنیا دەژمێردرێت، چونکە وەڵامەکان لە هەردوو حاڵەتدا هەمان نرخیان هەیە:

Twenty questions, with and without cachingبیست پرسیار، بە کاش و بێ کاشcache_savings.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
# 20 questions about one 50,000-token shop handbook, asked within a few minutes
BASE = 2.00            # dollars per million input tokens (example, November 2026)
WRITE = 1.25           # a 5-minute cache write costs 1.25 x the base price
HANDBOOK, QUESTION, N = 50_000, 60, 20

def dollars(tokens, multiplier=1.0):
    return tokens * BASE * multiplier / 1_000_000

no_cache = N * dollars(HANDBOOK + QUESTION)
print(f"without caching: ${no_cache:.4f}")

for read in (0.10, 0.05):          # cache reads: 0.1 x on most models, 0.05 x on some
    first = dollars(HANDBOOK, WRITE) + dollars(QUESTION)
    others = (N - 1) * (dollars(HANDBOOK, read) + dollars(QUESTION))
    cached = first + others
    print(f"with caching (reads {read:.2f} x): ${cached:.4f}  saved {1 - cached / no_cache:.0%}")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 13-15

The first request pays 1.25 × to write the handbook; the other 19 read it at the cheap rate.

یەکەم ⁦1.25⁩ × دەدات بۆ نووسینی ڕێبەرەکە؛ ⁦19⁩ـەکەی تر بە نرخە هەرزانەکە دەیخوێننەوە.

Three bars for 20 questions about one 50,000-token handbook, input only at $2 per million: no caching $2.0024; caching with reads at 0.1 × $0.3174; caching with reads at 0.05 × $0.2224. The first request writes the cache at 1.25 × the price and the next 19 read it cheaply; reads cost 0.1 × on most Claude models and 0.05 × on Opus 5.5 and Sonnet 5.5 (November 2026).سێ ستوون بۆ 20 پرسیار لەبارەی یەک ڕێبەری 50,000 تۆکنی، تەنیا نێوخستە بە $2 بۆ هەر ملیۆنێک: بێ کاش $2.0024؛ بە کاش و خوێندنەوە بە 0.1 × $0.3174؛ بە کاش و خوێندنەوە بە 0.05 × $0.2224. یەکەم داواکاری کاشەکە بە 1.25 × نرخ دەنووسێت و 19ـەکەی دواتر بە هەرزانی دەیخوێننەوە؛ خوێندنەوە لە زۆربەی مۆدێلەکانی Claude 0.1 ×ـە و لە Opus 5.5 و Sonnet 5.5 0.05 ×ـە (تشرینی دووەمی 2026).
Caching cut the input bill by 84 to 89%. پسوولەی ⁦84⁩ تا ⁦89⁩٪ کەم کردەوە.CêIT

For the 5-minute cache, caching pays off from the first cache read. It saves money, not space: cached tokens still fill the context window. On Claude, cache reads also do not count against the per-minute input limit, so more requests get through. In code, you mark where the fixed part ends:

بۆ 5 خولەکی، لە یەکەم خوێندنەوەوە قازانج دەکات. پارە پاشەکەوت دەکات، نەک شوێن: کاشکراوەکان هێشتا پڕ دەکەنەوە. لە Claudeـدا خوێندنەوە لە لە سنووری هەر خولەکێکیش ناژمێردرێت، بۆیە زیاتر تێدەپەڕن. لە کۆددا شوێنی کۆتایی بەشە جێگیرەکە دیاری دەکەیت:

Example only (not run): marking the cached part with cache_controlتەنیا نموونە (کار پێ نەکراوە): دیاریکردنی بەشی کاشکراو بە ⁦cache_control⁩PYTHONType in a terminalلە تێرمیناڵدا بینووسە
import anthropic

client = anthropic.Anthropic()

with open("shop_handbook.txt", encoding="utf-8") as f:
    handbook = f.read()                  # the same long text in every request

response = client.messages.create(
    model="claude-sonnet-5-5",
    max_tokens=400,
    system=[
        {"type": "text", "text": "You answer questions about Shilan's bookshop. Use only the handbook."},
        {"type": "text", "text": handbook, "cache_control": {"type": "ephemeral"}},
    ],
    messages=[{"role": "user", "content": "Can a customer return a book after a week?"}],
)

u = response.usage
print(u.cache_creation_input_tokens, u.cache_read_input_tokens, u.input_tokens, u.output_tokens)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 13

cache_control marks the end of the fixed part: tools, the system text and the handbook are cached together.

cache_control کۆتایی بەشە جێگیرەکە دیاری دەکات: ئامرازەکان، دەقی سیستەم و ڕێبەرەکە پێکەوە کاش دەکرێن.

Lineدێڕی 15

The customer's question changes every time, so it comes after the cached part.

پرسیاری کڕیار هەموو جارێک دەگۆڕێت، بۆیە دوای بەشە کاشکراوەکە دێت.

Lineدێڕی 18-19

usage tells you what was written to, and read from, the cache.

usage پێت دەڵێت چی لە نووسرا و چی لێی خوێندرایەوە.

A long chat costs more every turnگفتوگۆی درێژ لە هەر نۆرەیەکدا گرانتر دەبێت

The model keeps nothing between requests, so the app re-sends the whole conversation each turn (no memory between requests). If every turn adds 300 tokens, turn 10 sends 3,000, and the whole chat sends 16,500:

لە نێوان هیچ ناهێڵێتەوە، بۆیە لە هەموو نۆرەیەکدا هەموو گفتوگۆکە دووپاتە دەنێرێتەوە (بێ بیرەوەری لە نێوان داواکارییەکاندا). ئەگەر هەر نۆرەیەک ⁦300⁩ زیاد بکات، نۆرەی ⁦10 3,000⁩ دەنێرێت و هەموو گفتوگۆکە ⁦16,500⁩:

Tokens sent in a 10-turn chat, with and without a summaryئەو تۆکنانەی لە گفتوگۆیەکی ⁦10⁩ نۆرەییدا دەنێردرێن، بە پوختە و بێ پوختەhistory_growth.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
# A chat re-sends the whole conversation every turn
TURN = 300        # tokens each turn adds (Aram's question + the answer)
SUMMARY = 200     # tokens of a short summary that replaces old turns

plain = 0
print("turn  sent  total")
for n in range(1, 11):
    sent = TURN * n               # all earlier turns + this one
    plain += sent
    print(f"{n:4} {sent:5} {plain:6}")

# The same chat, but after turn 5 the first five turns become one summary
compact = 0
for n in range(1, 11):
    if n <= 5:
        compact += TURN * n
    else:
        compact += SUMMARY + TURN * (n - 5)
compact += 5 * TURN + SUMMARY      # one extra request that writes the summary
print("without a summary:", plain, "tokens")
print("with a summary:   ", compact, "tokens")
print(f"saved: {1 - compact / plain:.0%}")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 8

Turn n re-sends all n turns so far.

نۆرەی n هەموو ئەو n نۆرەیەی تا ئێستا دووپاتە دەنێرێتەوە.

Lineدێڕی 17-18

After turn 5, the first five turns are replaced by a 200-token summary.

دوای نۆرەی 5، پێنج نۆرەی یەکەم بە پوختەیەکی ⁦200⁩ دەگۆڕدرێن.

Lineدێڕی 19

Writing the summary is one extra request, and it is counted.

نووسینی پوختەکە زیادەیە و ژمێردراوە.

Fixes: context compaction, where older turns become a short summary (Claude's API offers server-side compaction, in beta for Claude 4.6 and later); a new chat for each new topic; and sending only the passages that matter instead of whole documents (RAG). Agents need this most (context in long loops).

چارەسەرەکان: ، کە نۆرە کۆنەکان دەبنە پوختەیەکی کورت (APIـی Claude پوختکردنەوە لەسەر پێشکەش دەکات، بە شێوەی beta بۆ ⁦Claude 4.6⁩ و دواتر)؛ گفتوگۆیەکی نوێ بۆ هەر بابەتێکی نوێ؛ و ناردنی تەنیا ئەو بڕگانەی گرنگن لە جیاتی هەموو بەڵگەنامەکان (RAG). زیاتر لە هەمووان پێویستیان بەمەیە (کۆنتێکست لە خولەکان درێژدا).

Bar chart of the tokens sent at each turn of a 10-turn chat that adds 300 tokens a turn. Orange bars grow from 300 at turn 1 to 3,000 at turn 10. Green bars are the same up to turn 5; after a summary at turn 5 they drop to 500 at turn 6 and grow to 1,700 at turn 10. Sending everything: 16,500 tokens in all; with a summary: 11,700 tokens, 29% fewer. The summary itself costs one extra small request, which is included.هێڵکاریی ستوونیی ئەو تۆکنانەی لە هەر نۆرەیەکی گفتوگۆیەکی 10 نۆرەییدا دەنێردرێن کە هەر نۆرەیە 300 تۆکن زیاد دەکات. ستوونە پرتەقاڵییەکان لە 300 لە نۆرەی 1 گەورە دەبن بۆ 3,000 لە نۆرەی 10. ستوونە سەوزەکان تا نۆرەی 5 هەمانن؛ دوای پوختەیەک لە نۆرەی 5، لە نۆرەی 6 دادەبەزن بۆ 500 و لە نۆرەی 10 دەگەنە 1,700. ناردنی هەمووی: 16,500 تۆکن بە گشتی؛ بە پوختە: 11,700 تۆکن، واتە 29٪ کەمتر. خودی پوختەکە داواکارییەکی بچووکی زیادەی تێدەچێت کە ژمێردراوە.
Context compaction: a summary stops the staircase from climbing so fast.: پوختەیەک ڕێگە لە بەرزبوونەوەی خێرای پەیژەکە دەگرێت.CêIT

Batch jobs and rate limitsچارەسەرکردنی کۆمەڵەیی و سنووری داواکاری

Not every job needs an answer in seconds. Shilan wants new descriptions for 2,000 books by tomorrow. A Batch API takes all the requests at once and returns the results later for half the price: on Claude (November 2026), most batches finish within an hour, every batch within 24 hours, and one batch can hold up to 100,000 requests. OpenAI and Google also halve their batch prices. It is the old idea of batch processing, applied to models.

هەموو کارێک پێویستی بە وەڵام لە چەند چرکەیەکدا نییە. شیلان تا بەیانی وەسفی نوێی بۆ ⁦2,000⁩ کتێب دەوێت. Batch API هەموو پێکەوە وەردەگرێت و ئەنجامەکان دواتر بە نیوەی نرخ دەگەڕێنێتەوە: لە Claude (تشرینی دووەمی ⁦2026⁩)، زۆربەی کۆمەڵەکان لە ماوەی یەک کاتژمێردا تەواو دەبن، هەموو کۆمەڵەیەک لە ماوەی ⁦24⁩ کاتژمێردا و یەک کۆمەڵە دەتوانێت تا ⁦100,000⁩ هەڵبگرێت. OpenAI و Googleـیش نرخی کۆمەڵە نیوە دەکەن. ئەمە هەمان بیرۆکە کۆنەکەی چارەسەرکردنی کۆمەڵەییـە، بۆ بەکارهاتووە.

Providers also set rate limits: caps on requests per minute (RPM), input tokens per minute (ITPM) and output tokens per minute (OTPM). Over a limit, the API answers 429 with a retry-after header that says how many seconds to wait. A good program waits and tries again instead of hammering the server.

دابینکەران ـیش دادەنێن: سنوور بۆ لە خولەکێکدا ⁦(RPM)⁩، لە خولەکێکدا ⁦(ITPM)⁩ و لە خولەکێکدا ⁦(OTPM)⁩. ئەگەر لە سنوورێک تێپەڕیت، بە 429 وەڵام دەداتەوە لەگەڵ retry-after کە دەڵێت چەند چرکە چاوەڕێ بکەیت. باش چاوەڕێ دەکات و دووپاتە هەوڵ دەداتەوە، لە جیاتی ئەوەی بەردەوام لە بدات.

Describing 2,000 books: now, or overnight at half price? Top: normal requests, one at a time, answers in seconds, at full price; good for a customer waiting for an answer. Bottom: a batch, all 2,000 requests sent at once, results collected later; most finish within 1 hour, at most 24 hours; 50% off; good for jobs that can wait, such as book descriptions or tests.وەسفی 2,000 کتێب: ئێستا، یان بە درێژایی شەو بە نیوەی نرخ؟ سەرەوە: داواکارییەکان ئاسایی، یەک لە دوای یەک، وەڵام لە چەند چرکەیەکدا، بە نرخی تەواو؛ باشە بۆ کڕیارێک کە چاوەڕێی وەڵامە. خوارەوە: کۆمەڵەیەک، هەر 2,000 داواکارییەکە پێکەوە دەنێردرێن و ئەنجامەکان دواتر وەردەگیرێن؛ زۆربەیان لە 1 کاتژمێردا تەواو دەبن، زۆرترین 24 کاتژمێر؛ 50٪ داشکاندن؛ باشە بۆ کارێک کە دەتوانێت چاوەڕێ بکات، وەک وەسفی کتێب یان تاقیکردنەوە.
Batch: half price for work that can wait.: نیوەی نرخ بۆ کارێک کە دەتوانێت چاوەڕێ بکات.CêIT

Measure first, then optimiseسەرەتا بیپێوە، پاشان باشتری بکە

Every API response reports its usage: input tokens, output tokens, and tokens written to or read from the cache. Collect these numbers for a few days before you change anything. Then fix the biggest cost first, and after each change run your test cases again: a cheaper prompt that gives worse answers is not a saving.

هەموو وەڵامێکی بەکارهێنانەکەی ڕادەگەیەنێت: ، و ئەو لە نووسراون یان لێی خوێندراونەتەوە. پێش ئەوەی هیچ شتێک بگۆڕیت، ئەم ژمارانە بۆ چەند ڕۆژێک کۆ بکەرەوە. پاشان سەرەتا گەورەترین تێچوو چاک بکە و دوای هەر گۆڕانکارییەک دیسان تاقی بکەرەوە: هەرزانتر کە وەڵامی خراپتر بدات پاشەکەوت نییە.

Set limits as well: a monthly spend limit in the provider's console, and caps on steps, tokens and money per task in your own program. OWASP's Top 10 for LLM Applications (2025) lists unbounded consumption: an app without limits can be made to burn money (“denial of wallet”) or stop for everyone (stop conditions).

سنووریش دابنێ: سنووری خەرجیی مانگانە لە دابینکەرەکەدا و سنوور بۆ هەنگاو، و پارە بۆ هەر کارێک لە خۆتدا. لیستی ⁦Top 10⁩ـی OWASP بۆ ⁦LLM (2025)⁩ بەکارهێنانی بێسنوور دەژمێرێت: بێ سنوور دەکرێت وای لێ بکرێت پارە بسووتێنێت («denial of wallet») یان بۆ هەمووان بوەستێت (مەرجەکانی وەستان).

Left, the usage of one request to Shilan's handbook helper: input_tokens 60, cache_creation_input_tokens 0, cache_read_input_tokens 50,000, output_tokens 900. Right, the cost of each part at $2 in and $10 out: questions $0.00012, cache reads at 0.1 × $0.01000, output $0.00900. Below: here the answer costs almost as much as the whole cached handbook, so ask for shorter answers first, then check that quality stays good.لای چەپ، بەکارهێنانی داواکارییەک بۆ یارمەتیدەری ڕێبەرەکەی شیلان: input_tokens 60، cache_creation_input_tokens 0، cache_read_input_tokens 50,000، output_tokens 900. لای ڕاست، تێچووی هەر بەشێک بە $2 بۆ نێوخستە و $10 بۆ دەرخستە: پرسیارەکان $0.00012، خوێندنەوە لە کاش بە 0.1 × $0.01000، دەرخستە $0.00900. لە خوارەوە: لێرەدا وەڵامەکە بە ئەندازەی هەموو ڕێبەرە کاشکراوەکە تێدەچێت، بۆیە سەرەتا داوای وەڵامی کورتتر بکە، پاشان بپشکنە کە کوالیتی باش ماوەتەوە.
The usage numbers show where the money goes.ژمارەکانی بەکارهێنان پیشان دەدەن پارەکە بۆ کوێ دەچێت.CêIT

Measure first, then optimiseسەرەتا بیپێوە، پاشان باشتری بکە

Counting tokens in codeژماردنی تۆکنەکان لە کۆددا

The provider's own counter is the only reliable count, because each model has its own tokenizer. On Claude, counting is free (it has its own rate limit), and the count is a close estimate:

ئامرازی ژماردنی خودی دابینکەر تەنیا ژماردنی جێی متمانەیە، چونکە هەر خۆی هەیە. لە Claudeـدا ژماردن بێبەرانبەرە (سنووری داواکاریی تایبەت بە خۆی هەیە) و ژمارەکە مەزەندەیەکی نزیکە:

Example only (not run): counting before, and reading usage afterتەنیا نموونە (کار پێ نەکراوە): ژماردن پێش ناردن و خوێندنەوەی بەکارهێنان دوایPYTHONType in a terminalلە تێرمیناڵدا بینووسە
import anthropic

client = anthropic.Anthropic()

# Before sending: count the input tokens of a request (free, but rate-limited)
count = client.messages.count_tokens(
    model="claude-sonnet-5-5",
    system="You are the helper of Shilan's bookshop.",
    messages=[{"role": "user", "content": "ئایا کتێبی مەم و زینتان هەیە؟"}],
)
print("input tokens:", count.input_tokens)

# After sending: every response reports what it used
response = client.messages.create(
    model="claude-sonnet-5-5",
    max_tokens=300,
    system="You are the helper of Shilan's bookshop.",
    messages=[{"role": "user", "content": "ئایا کتێبی مەم و زینتان هەیە؟"}],
)
print(response.usage.input_tokens, response.usage.output_tokens)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 6-11

count_tokens takes the same fields as a real request and returns input_tokens.

count_tokens هەمان خانەکانی ڕاستەقینە وەردەگرێت و input_tokens دەگەڕێنێتەوە.

Lineدێڕی 21

usage on the response: what this request really used and will be billed for.

usage لەسەر وەڵامەکە: ئەوەی ئەم بە ڕاستی بەکاری هێنا و پارەی لێ وەردەگیرێت.

Measure first, then optimiseسەرەتا بیپێوە، پاشان باشتری بکە

Fewer tokens, less energyتۆکنی کەمتر، وزەی کەمتر

Every token is computed on GPUs in data centres that use electricity and cooling (inside a data centre). NIST's Generative AI Profile (NIST AI 600-1, July 2024) lists environmental impacts among the risks of generative AI: training and running models need a lot of energy, generative tasks such as summarising used more energy than simple classification in one study, and there is no agreed way to measure the impact yet.

هەموو لەسەر لە دەژمێردرێت کە کارەبا و ساردکردنەوە بەکار دەهێنن (ناو ناوەندێکی دراوە). پرۆفایلی ⁦NIST (NIST AI 600-1⁩، تەمموزی ⁦2024⁩) کاریگەریی ژینگەیی لە ناو دەژمێرێت: ڕاهێنان و کارپێکردنی وزەیەکی زۆریان دەوێت، لە توێژینەوەیەکدا کاری بەرهەمهێنەر وەک پوختەکردن وزەی زیاتری لە سادە بەکار هێنا و هێشتا ڕێگەیەکی ڕێککەوتوو بۆ پێوانی ئەم کاریگەرییە نییە.

You cannot see the energy of one request, but the direction is clear: a smaller model, fewer tokens and no wasted retries mean less computing work, a smaller bill and a smaller carbon footprint (carbon footprint).

ناتوانیت وزەی یەک ببینیت، بەڵام ئاڕاستەکە ڕوونە: بچووکتر، کەمتر و دووپاتەکردنەوەی بێهودە نەبوون واتە کاری ژمێرکاریی کەمتر، پسوولەی بچووکتر و ـی بچووکتر (شوێنپێی کاربۆن).

Common mistakesهەڵە باوەکان

Exercisesڕاهێنانەکان

ExerciseڕاهێنانCompute three costsسێ تێچوو بژمێرە

A request has 2,000 input tokens and 500 output tokens. (a) What does it cost on a middle model at $2 / $10 per million? (b) On a small model at $0.10 / $0.50? (c) What do 10,000 requests a month cost on the middle model?

⁦2,000⁩ و ⁦500⁩ هەیە. ⁦(a)⁩ لەسەر ناوەند بە ⁦$2 / $10⁩ بۆ هەر ملیۆنێک چەند تێدەچێت؟ ⁦(b)⁩ لەسەر بە ⁦$0.10 / $0.50⁩؟ ⁦(c) 10,000⁩ لە مانگێکدا لەسەر ناوەند چەند تێدەچێت؟

Hintئاماژە

Divide by a million once, then multiply by the price per million.

تەنیا یەکجار بە ملیۆن دابەشی بکە، پاشان لێکدانی نرخ بۆ هەر ملیۆنێک.

Show answerوەڵامەکە پیشان بدە
One way to check it in Pythonڕێگەیەک بۆ پشکنینی لە Pythonـداex_costs.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
PER_MILLION = 1_000_000

def cost(tokens_in, tokens_out, price_in, price_out):
    return (tokens_in * price_in + tokens_out * price_out) / PER_MILLION

a = cost(2_000, 500, 2.00, 10.00)       # middle model
b = cost(2_000, 500, 0.10, 0.50)        # small model
print(f"(a) ${a:.5f}")
print(f"(b) ${b:.5f}")
print(f"(c) ${a * 10_000:.2f} a month")

By hand: (a) 2,000 × 2 / 1,000,000 = $0.004 plus 500 × 10 / 1,000,000 = $0.005, so $0.009. (b) $0.0002 + $0.00025 = $0.00045, twenty times cheaper. (c) $0.009 × 10,000 = $90.

بە دەست: ⁦(a) 2,000 × 2 / 1,000,000 = $0.004⁩ و ⁦500 × 10 / 1,000,000 = $0.005⁩، کۆی ⁦$0.009. (b) $0.0002 + $0.00025 = $0.00045⁩، بیست جار هەرزانتر. ⁦(c) $0.009 × 10,000 = $90⁩.

ExerciseڕاهێنانReorder a prompt for cachingهەوێنێک بۆ کاشکردن ڕیز بکەرەوە

Karwan's prompt has these parts, in this order: (1) today's date and time; (2) the customer's question; (3) the shop handbook, 40,000 tokens; (4) the system rules; (5) the tool definitions. Put them in a cache-friendly order.

کاروان ئەم بەشانەی هەیە، بەم ڕیزە: ⁦(1)⁩ ڕێکەوت و کاتی ئەمڕۆ؛ ⁦(2)⁩ پرسیاری کڕیار؛ ⁦(3)⁩ ڕێبەری دوکان، ⁦40,000⁩ ؛ ⁦(4)⁩ یاساکانی سیستەم؛ ⁦(5)⁩ پێناسەی ئامرازەکان. بە ڕیزێکی گونجاو بۆ دایان بنێ.

Show answerوەڵامەکە پیشان بدە

Fixed parts first, in the order the API reads them: (5) tools → (4) system rules → (3) handbook, with the cache mark after the handbook. Then the changing parts: (1) date and time → (2) the question. Now 40,000 tokens are read from the cache on every question within the cache time.

بەشە جێگیرەکان یەکەم، بەو ڕیزەی دەیانخوێنێتەوە: ⁦(5)⁩ ئامرازەکان ← ⁦(4)⁩ یاساکانی سیستەم ← ⁦(3)⁩ ڕێبەرەکە. نیشانەی دوای ڕێبەرەکە دادەنرێت. پاشان بەشە گۆڕاوەکان: ⁦(1)⁩ ڕێکەوت و کات ← ⁦(2)⁩ پرسیارەکە. ئێستا لە ماوەی کاتی لە هەموو پرسیارێکدا ⁦40,000⁩ لە دەخوێنرێتەوە.

ExerciseڕاهێنانChoose a model sizeقەبارەی مۆدێل هەڵبژێرە

Choose small, middle or large for each job, and say why: (a) detect whether a message is Kurdish, Arabic or English; (b) write friendly replies to customers; (c) plan next year's stock from three years of sales and many rules; (d) pull the book title and quantity out of an order message.

بۆ هەر کارێک بچووک، ناوەند یان گەورە هەڵبژێرە و بڵێ بۆچی: ⁦(a)⁩ زانینی ئەوەی پەیامێک کوردییە، عەرەبییە یان ئینگلیزییە؛ ⁦(b)⁩ نووسینی وەڵامی دۆستانە بۆ کڕیاران؛ ⁦(c)⁩ پلاندانانی کاڵای ساڵی داهاتوو لە فرۆشی سێ ساڵ و زۆر یاسا؛ ⁦(d)⁩ دەرهێنانی ناونیشانی کتێب و ژمارە لە پەیامێکی داواکردن.

Show answerوەڵامەکە پیشان بدە
  • (a) small: a simple, high-volume label.
  • (b) middle: good writing, especially in Kurdish, at a fair price; test the Kurdish first.
  • (c) large, perhaps a reasoning model: many rules must hold at once, and mistakes are expensive.
  • (d) small, with structured output: copying two fields into fixed keys.
  • ⁦(a)⁩ بچووک: ناونانێکی سادە بە ژمارەیەکی زۆر.
  • ⁦(b)⁩ ناوەند: نووسینی باش، بەتایبەتی بە کوردی، بە نرخێکی گونجاو؛ سەرەتا کوردییەکەی تاقی بکەرەوە.
  • ⁦(c)⁩ گەورە، لەوانەیە : زۆر یاسا دەبێت پێکەوە ڕاست بن و هەڵە گرانە.
  • ⁦(d)⁩ بچووک، بە : کۆپیکردنی دوو خانە بۆ جێگیر.
ExerciseڕاهێنانA 20-turn chatگفتوگۆیەکی ⁦20⁩ نۆرەیی

Change history_growth.py (without the summary) for a 20-turn chat with 300 new tokens a turn. How many tokens are sent in all, and how many in the last turn alone?

history_growth.py (بێ پوختە) بگۆڕە بۆ گفتوگۆیەکی ⁦20⁩ نۆرەیی کە هەر نۆرەیە ⁦300⁩ نوێ زیاد دەکات. بە گشتی چەند دەنێردرێت و تەنیا لە دوایین نۆرەدا چەند؟

Show answerوەڵامەکە پیشان بدە
One possible answerوەڵامێکی گونجاوex_history20.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
TURN = 300
total = 0
for n in range(1, 21):
    total += TURN * n
print("20 turns send", total, "tokens")
print("the last turn alone sends", TURN * 20, "tokens")

Twice as many turns sends almost four times as many tokens (63,000 against 16,500), because each turn re-sends everything before it.

دوو ئەوەندە نۆرە نزیکەی چوار ئەوەندە دەنێرێت (⁦63,000⁩ بەرامبەر ⁦16,500⁩)، چونکە هەر نۆرەیەک هەموو ئەوەی پێشی دووپاتە دەنێرێتەوە.

ExerciseڕاهێنانLive or batch?ڕاستەوخۆ یان کۆمەڵەیی؟

Decide for each job: (a) checking 5,000 old product descriptions for spelling; (b) a chat helper on the shop's website; (c) running Shilan's 20 test cases after every prompt change; (d) a voice assistant at the shop door.

بۆ هەر کارێک بڕیار بدە: ⁦(a)⁩ پشکنینی ڕێنووسی ⁦5,000⁩ وەسفی کۆنی کاڵا؛ ⁦(b)⁩ یارمەتیدەرێکی گفتوگۆ لەسەر دوکان؛ ⁦(c)⁩ تاقیکردنەوەی شیلان (⁦20⁩ دانە) دوای هەموو گۆڕانکارییەکی ؛ ⁦(d)⁩ یارمەتیدەرێکی دەنگی لە بەردەم دەرگای دوکان.

Show answerوەڵامەکە پیشان بدە

(a) batch: thousands of requests, nobody waiting. (b) live: a customer is waiting. (c) batch if the results can wait up to a day, otherwise live; small test sets are often run live during work. (d) live: a person is standing there.

⁦(a)⁩ کۆمەڵەیی: هەزاران و کەس چاوەڕێ نییە. ⁦(b)⁩ ڕاستەوخۆ: کڕیارێک چاوەڕێیە. ⁦(c)⁩ کۆمەڵەیی ئەگەر ئەنجامەکان بتوانن تا ڕۆژێک چاوەڕێ بکەن، ئەگەرنا ڕاستەوخۆ؛ کۆمەڵە تاقیکردنەوەی بچووک زۆرجار لە کاتی کاردا ڕاستەوخۆ دەکرێن. ⁦(d)⁩ ڕاستەوخۆ: کەسێک لەوێ وەستاوە.

Summaryپوختە

  • Tokens cost money, time, space and part of your rate limit. Say everything the model needs, and nothing more.
  • Prices are per million tokens; output costs about 5 × input; thinking and tool tokens are paid too. Date every price (November 2026 here).
  • Estimate with care: English rules do not fit Kurdish (about 4.8 × in Petrov's 2023 count). Count with the provider's counter.
  • Trim padding, ask for the length you need, and choose the smallest model that passes your tests.
  • Prompt caching: fixed parts first, changing parts last. Context compaction stops chats from growing. Batches halve the price of work that can wait.
  • Measure first, fix the biggest cost, re-test quality, and set limits. Fewer tokens also mean less energy.
  • پارە، کات، شوێن و بەشێک لە سنووری داواکاری دەخۆن. هەرچی پێویستیەتی بڵێ، نە زیاتر.
  • نرخەکان بۆ هەر ملیۆن ؛ نزیکەی 5 × گرانە؛ بیرکردنەوە و ئامرازیش پارەیان دەدرێت. ڕێکەوتی هەموو نرخێک دیاری بکە (لێرەدا تشرینی دووەمی ⁦2026⁩).
  • بە وریاییەوە مەزەندە بکە: یاسای ئینگلیزی لە کوردی ناوەشێتەوە (نزیکەی ⁦4.8⁩ × لە ژماردنەکەی Petrov لە ⁦2023⁩). بە ئامرازی ژماردنی دابینکەر بژمێرە.
  • زیادە ببڕە، داوای ئەو درێژییە بکە کە پێویستتە و بچووکترین هەڵبژێرە کە لە تاقیکردنەوەکانت دەردەچێت.
  • : بەشە جێگیرەکان یەکەم، بەشە گۆڕاوەکان کۆتایی. ڕێگە لە گەورەبوونی گفتوگۆ دەگرێت. نرخی کاری چاوەڕوانکراو نیوە دەکات.
  • سەرەتا بیپێوە، گەورەترین تێچوو چاک بکە، کوالیتی دیسان تاقی بکەرەوە و سنوور دابنێ. کەمتر واتە وزەی کەمتریش.

Next: models that call tools and work in loops, where every turn adds tokens: AI agents and loop engineering.

داهاتوو: ئەو ئامراز بانگ دەکەن و لە کار دەکەن، کە هەموو نۆرەیەک زیاد دەکات: ئەیجێنتەکان و ئەندازیاریی خول.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

AI Agents and Loop Engineeringئەیجێنتەکانی ژیریی دەستکرد و ئەندازیاریی خول

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.

Appearanceڕووکار

Modeدۆخ
Styleشێواز
Coloursڕەنگەکان

Saved on this device only.تەنیا لەسەر ئەم ئامێرە پاشەکەوت دەکرێت.