सामग्री पर जाएँ
16/30अध्याय 16 / 30

Context Window, Tokens और बिल: माप के साथ

40-turn बातचीत में input tokens अपनी लंबाई से 22 गुना लगे। caching ने 68% घटाया; गलत जगह timestamp ने 20% बढ़ाया।

इस पेज पर

यहाँ चालीस-turn की support बातचीत है, turn दर turn bill की गई। इसमें कुछ भी असामान्य नहीं है: एक developer API के बारे में पूछ रहा है, assistant एक-दो paragraph में जवाब दे रहा है। पूरा exchange 5,090 tokens के text का है — लगभग आठ pages।

turnprompt tokensनया textoutputइस turn की लागतrunning total
121318183$0.002622$0.002622
589214123$0.003260$0.014354
101,65619114$0.004680$0.035530
202,86818103$0.006972$0.094426
303,9412099$0.009070$0.174170
404,94717142$0.011598$0.274386

दूसरे और तीसरे columns को साथ पढ़िए। turn 40 पर user ने सत्रह tokens type किए और उससे 4,947 tokens का charge लिया गया। सवाल पहले वाले से ज्यादा मुश्किल नहीं था; वह छोटा था। जो बदला वह यह था कि request अपने साथ पूरी बातचीत लेकर आई, फिर से, चालीसवीं बार।

उन चालीस calls में bill हुए कुल input tokens: 112,617। बातचीत 5,090 tokens लंबी है। आपने इसके लिए बाईस गुना भुगतान किया।

यह chapter इसी बारे में है कि ऐसा क्यों होता है, हर provider के invoice पर इसे क्या कहा जाता है, और जिन पाँच चीज़ों के लिए आपसे charge लिया जा रहा है उनमें से किनके बारे में आप कुछ कर सकते हैं।

विवरण दिखाएँ

इस chapter को Part II से क्या चाहिए।

  • Chapter 7 ने tokenizer बनाया था। यहाँ भी token ही unit है — वही unit, अब priced।
  • Chapter 9 ने self-attention और उसकी O(n2)O(n^2) cost निकाली थी, asymptotic-notation box में। वही cost कारण है कि कोई limit है, और उसे यहाँ दोबारा समझाने के बजाय link किया गया है।
  • Chapter 13 ने prefill को decode के विरुद्ध मापा और हिसाब लगाया कि KV cache कितनी जगह लेता है। ऊपर के input और output columns असल में उन्हीं दो phases को खरीद रहे हैं।

बाकी सब TypeScript है, क्योंकि यह remote call की accounting है, model की mathematics नहीं।

इस business में सबसे महँगी गलतफहमी यह है कि model बातचीत को याद रखता है।

ऐसा नहीं है, और Chapter 13 का mechanism ठीक-ठीक बताता है क्यों। generation के दौरान transformer की state KV cache होती है: sequence के हर token के लिए compute किए गए keys और values। वह cache एक request की अवधि तक रहता है। जब request खत्म होती है, उसे रखने वाली process किसी और को serve करने के लिए free होती है, और cache चला जाता है। दूसरी तरफ कोई per-user store नहीं है, और कोई session नहीं।

इसलिए अगली request को वह सब कुछ साथ लेकर आना पड़ता है जो model को पता होना चाहिए, और model एक भी नया token emit करने से पहले पूरे prompt पर forward pass चलाकर उस state को फिर से बनाता है। Chapter 15 ने prompt को "पूरी state" कहा था। भौतिक कारण यह है: prompt पूरी state है क्योंकि call के बाद कुछ और बचता ही नहीं।

context window उस prompt plus उसके answer की अधिकतम लंबाई है। यह इस बात की ceiling है कि आप कितनी state rebuild कर सकते हैं, ऐसा container नहीं जो requests के बीच कुछ hold करता हो। इसे "model की memory" कहना causality की दिशा उलट देता है — आप memory भर नहीं रहे हैं, आप उसे फिर से स्थापित करने का भुगतान कर रहे हैं।

बाईस यहीं से आता है। Turn nn अपने साथ पिछले सभी n1n-1 turns ले जाता है, इसलिए nn turns की बातचीत में कुल input एक बढ़ती हुई series का sum है, जो quadratic है:

total input  =  i=1n(s+hi)  =  Θ(n2)\text{total input} \;=\; \sum_{i=1}^{n} \big(s + h_i\big) \;=\; \Theta(n^2)

जहाँ ss system prompt है और hih_i turn ii पर history है। चालीस turns पर measured cumulative input को an2+bnan^2 + bn पर fit करने से 60.22n2+432.25n60.22\,n^2 + 432.25\,n मिलता है, जो turn 40 पर 112,617 measured के विरुद्ध 113,645 tokens predict करता है। quadratic term dominate करता है और linear term वह है जो user ने सच में type किया।

इस chapter से याद रखने वाली बात यह है: आपका bill बातचीत के square के साथ बढ़ता है, आखिरी सवाल के साथ नहीं। वही चालीस सवाल अगर बिल्कुल history के बिना पूछे जाएँ तो $0.066036 लगते। history रखने पर $0.274386 लगा। History ने bill को 4.2 से multiply किया, और यह multiply करता रहेगा, क्योंकि multiplier बातचीत की लंबाई है।

window finite है, दो कारणों से जो एक ही दिशा में खींचते हैं। पहला Chapter 9 वाला है: attention हर token की तुलना हर दूसरे token से करता है, इसलिए उस layer का work sequence length के square के साथ बढ़ता है। दूसरा memory है: KV cache sequence length के साथ linearly बढ़ता है, और Chapter 13 ने वह arithmetic किया था — लंबे sequences पर यह weights से बड़ा हो जाता है।

दोनों limits पर हमला हुआ है और दोनों में से कोई हटाई नहीं गई। FlashAttention1 computation को इस तरह reorganise करता है कि यह high-bandwidth memory में बहुत कम read और write करे, जिससे asymptotic cost बदले बिना लंबे sequences practical बनते हैं। Position Interpolation2 और YaRN3 Chapter 9 के positional encodings को rescale करके trained model की usable window बढ़ाते हैं, retrain करके नहीं। इसी वजह से windows पाँच साल में 2K से 1M हो गईं।

उन्होंने long contexts को free नहीं बनाया। उन्होंने ceiling ऊँची की और slope को नरम किया। slope अभी भी है, और इस chapter में आगे के price tiers उसी को माप रहे हैं।

Internet पर लगभग हर cost calculator API call को input tokens × input price plus output tokens × output price मानता है। 2023 में यह सही था। अब यह इस तरह गलत है कि दोनों दिशाओं में bills दो गुना या उससे अधिक तक चूक जाते हैं।

पाँच billable token categories हैं:

bucketयह क्या हैtypical price, input के सापेक्ष
uncached inputprompt tokens जिन्हें model को fresh process करना पड़ा
cache readstored prefix से serve हुए prompt tokens0.1×
cache writeइस call पर cache में store किए गए prompt tokens1.25× से 2×
outputवे tokens जिन्हें model ने generate करके आपको भेजा5× से 6×
reasoningवे tokens जिन्हें model ने generate किया और आपको नहीं भेजाoutput rate

इन पाँच में से तीन दो साल पहले अलग lines के रूप में मौजूद नहीं थे, और दो cache lines वही हैं जहाँ लोग गलती करते हैं, क्योंकि cache write ordinary input से ज्यादा cost करता है, कम नहीं। आप कुछ store करने के लिए premium देते हैं ताकि उसे वापस पढ़ते समय discount दे सकें, और यह trade अच्छा है या नहीं पूरी तरह इस पर निर्भर है कि आप उसे कितनी बार पढ़ते हैं।

reasoning bucket Chapter 12 वाला है, अब उस पर price लगा है, और इसमें एक detail साफ-साफ कहने लायक है: Google की documentation कहती है pricing "is based on the full thought tokens the model needs to generate, despite only the summary being output from the API."4 आप उन tokens के लिए billed होते हैं जो आपको कभी transmit नहीं किए जाते। यह इकलौता bucket है जिसकी contents आप count, inspect या verify नहीं कर सकते।

अब वह हिस्सा जो इसे multiplication problem के बजाय normalisation problem बनाता है। हर provider इन buckets को अलग names के तहत report करता है, और — यही trap है — उनमें से दो अलग quantities के लिए वही word use करते हैं।

एक call लीजिए: cache से पढ़े गए 4,837 tokens, 110 fresh, 142 visible output tokens, 300 reasoning tokens।

three usage payloads, one callJSON
// OpenAI-compatible
{ "usage": { "prompt_tokens": 4947,
             "prompt_tokens_details": { "cached_tokens": 4837 },
             "completion_tokens": 442,
             "completion_tokens_details": { "reasoning_tokens": 300 } } }

// Anthropic
{ "usage": { "input_tokens": 110,
             "cache_read_input_tokens": 4837,
             "cache_creation_input_tokens": 0,
             "output_tokens": 442 } }

// Gemini
{ "usageMetadata": { "promptTokenCount": 4947,
                     "cachedContentTokenCount": 4837,
                     "candidatesTokenCount": 142,
                     "thoughtsTokenCount": 300 } }

prompt_tokens: 4947 और input_tokens: 110 को देखिए। दोनों fields same prompt के input token count हैं। OpenAI के count में cached tokens शामिल हैं; Anthropic के count में वे शामिल नहीं हैं — इसकी documentation identity को explicitly बताती है, total_input_tokens = cache_read_input_tokens + cache_creation_input_tokens + input_tokens.5 Anthropic का input_tokens मतलब है "आपके last cache breakpoint के बाद के tokens"।

और output को देखिए। OpenAI और Anthropic दोनों 442 report करते हैं, जिसमें पहले से 300 reasoning tokens शामिल हैं। Gemini 142 report करता है और 300 को अपने अलग field में रखता है। Chapter 12 ने इसे same work को count करने के दो तरीकों के बीच incompatibility के रूप में flag किया था; यहाँ उसका cost है।

normaliser तीस lines का है और optional नहीं है:

normalise.tsTS
export interface Usage {
  promptTokens?: number;        // input, NOT cached
  cachedInputTokens?: number;   // read from cache
  cacheWriteTokens?: number;    // written to cache on this call
  completionTokens?: number;    // output
  reasoningTokens?: number;     // billed apart from output (Gemini only)
}

const num = (v: unknown) => (typeof v === "number" && isFinite(v) ? v : 0);

export const fromOpenAI = (raw: any): Usage => {
  const u = raw.usage ?? {}, d = u.prompt_tokens_details ?? {};
  const cached = num(d.cached_tokens), write = num(d.cache_write_tokens);
  return {
    promptTokens: Math.max(0, num(u.prompt_tokens) - cached - write), 
    cachedInputTokens: cached,
    cacheWriteTokens: write,
    completionTokens: num(u.completion_tokens),   // reasoning already inside
    reasoningTokens: 0,
  };
};

export const fromAnthropic = (raw: any): Usage => {
  const u = raw.usage ?? {};
  return {
    promptTokens: num(u.input_tokens),            // already excludes cache
    cachedInputTokens: num(u.cache_read_input_tokens),
    cacheWriteTokens: num(u.cache_creation_input_tokens),
    completionTokens: num(u.output_tokens),
    reasoningTokens: 0,
  };
};

export const fromGemini = (raw: any): Usage => {
  const m = raw.usageMetadata ?? {}, cached = num(m.cachedContentTokenCount);
  return {
    promptTokens: Math.max(0, num(m.promptTokenCount) - cached),
    cachedInputTokens: cached,
    cacheWriteTokens: 0,
    completionTokens: num(m.candidatesTokenCount), // EXCLUDES thinking
    reasoningTokens: num(m.thoughtsTokenCount),    // billed at output rate
  };
};

ऊपर के तीन payloads को तीन readers से चलाइए और तीनों वही Usage produce करते हैं, और इसलिए वही number: $0.006491। layer लिखने का पूरा point यही agreement है।

इसे गलत कीजिए और वही call कितना cost करेगा:

गलतीbillederror
cached_tokens को prompt_tokens से additional मानना$0.0161652.49× — आप prompt को दो बार charge करते हैं
cache reads को 0.1× के बजाय free मानना$0.0055240.85× — आप 15 % खा जाते हैं
candidatesTokenCount पढ़ना और thoughtsTokenCount ignore करना$0.002891call का 55 % गायब हो जाता है

तीसरा dangerous है, क्योंकि यह good news की दिशा में silently fail करता है। आपका dashboard reasoning model को उसकी actual cost के आधे से भी कम पर दिखाता है, और कहीं कोई error नहीं उठता।

buckets normalise होने के बाद cost function छोटा है। केवल non-obvious हिस्सा tier lookup है, जिसे अगला section समझाता है:

cost.tsTS
export interface Tier { maxPromptTokens: number | null; price: number }
export interface Pricing {
  input: Tier[]; output: Tier[];
  cachedInput?: Tier[]; cacheWrite?: Tier[]; reasoning?: Tier[];
}

const tierPrice = (tiers: Tier[] | undefined, contextSize: number, fallback?: Tier[]) => {
  const table = tiers ?? fallback;
  if (!table?.length) return 0;
  const sorted = [...table].sort(
    (a, b) => (a.maxPromptTokens ?? Infinity) - (b.maxPromptTokens ?? Infinity));
  for (const t of sorted)
    if (t.maxPromptTokens === null || contextSize <= t.maxPromptTokens) return t.price;
  return sorted[sorted.length - 1].price;
};

export function computeCost(pricing: Pricing, usage: Usage): number {
  const fresh = usage.promptTokens ?? 0;
  const read  = usage.cachedInputTokens ?? 0;
  const write = usage.cacheWriteTokens ?? 0;
  const out   = usage.completionTokens ?? 0;
  const think = usage.reasoningTokens ?? 0;
  const contextSize = fresh + read + write;   // the tier depends on the WHOLE prompt
  return fresh * tierPrice(pricing.input, contextSize)
       + read  * tierPrice(pricing.cachedInput, contextSize, pricing.input)
       + write * tierPrice(pricing.cacheWrite,  contextSize, pricing.input)
       + out   * tierPrice(pricing.output, contextSize)
       + think * tierPrice(pricing.reasoning, contextSize, pricing.output);
}

वहाँ दो design decisions के लिए argument करना चाहिए। fallbacks — cache prices का input पर fallback, reasoning का output पर — यह encode करते हैं कि missing table का अर्थ क्या है: Gemini पर reasoning tokens output rate पर billed होते हैं, इसलिए absent reasoning price zero नहीं, output price है। और contextSize तीनों input buckets को sum करता है, fresh ones को नहीं, क्योंकि tier इस बात से चुना जाता है कि prompt कितना लंबा है, इस बात से नहीं कि उसमें से कितना full price पर charge हुआ।

prompt cache आपके prompt के prefix के लिए model की computed state store करता है, ताकि same prefix वाली later request उसे recompute करना skip कर दे। "prefix" word से चार properties निकलती हैं और चारों लोगों को चौंकाती हैं।

cache rendered prompt की शुरुआत से आगे match करता है, और पहले अलग byte पर रुक जाता है। बाद में किसी अलग order में आने वाले content के लिए partial credit नहीं है। OpenAI इसे साफ कहता है: "cache reuse requires the entire rendered prefix to match."6

उससे नीचे कुछ cache नहीं होता और कोई error return नहीं होता। OpenAI पर minimum GPT-5.6 और later के लिए 1,024 tokens है और older models के लिए 2,048। Anthropic पर यह model के हिसाब से 512 से 4,096 तक है — Claude Sonnet 4.5 के लिए 1,024, Claude Haiku 4.5 के लिए 4,096। अगर दोनों cache fields zero वापस आते हैं, आम तौर पर वजह यही होती है।

Writing, reading से और caching न करने से ज्यादा cost करता है

सेक्शन का लिंक: Writing, reading से और caching न करने से ज्यादा cost करता है

OpenAI और Anthropic पर cache write short-lived cache के लिए uncached input rate का 1.25× है, और Anthropic का one-hour cache 2× है। read 0.1× है। Google write के लिए कुछ charge नहीं करता पर storage rent करता है: Gemini 2.5 Pro पर $4.50 per million tokens per hour।

यह expire होता है, और एक machine पर रहता है

सेक्शन का लिंक: यह expire होता है, और एक machine पर रहता है

Anthropic की default entry पाँच मिनट रहती है, हर hit पर free में refreshed। OpenAI की latest write या reuse के बाद कम से कम तीस मिनट। और OpenAI note करता है कि cached states individual machines पर रहती हैं, इसलिए request तभी hit करती है जब वह entry रखने वाली machine पर routed हो — यही वह चीज़ है जिसे prompt_cache_key influence करता है, guarantee नहीं।

break-even इतना छोटा है कि दिमाग में रखा जा सकता है, और OpenAI की documentation arithmetic कर देती है: prefix को एक बार write करके एक बार reuse करने की cost उसकी ordinary input cost की 1.35× है, जबकि उसे दो बार uncached process करने की cost 2× है; दस requests में, one write और nine reads की cost 2.15× है, 10× के विरुद्ध। एक reuse write का खर्च निकाल देता है। Anthropic भी वहीं पहुँचता है: five-minute cache के लिए one read, one-hour cache के लिए two।

अब वही चालीस-turn conversation, caching on और prefix stable के साथ:

uncached inputcache readscache writestotal
no cache112,617$0.274386
caching2,887104,7834,947$0.088250

अड़सठ प्रतिशत सस्ता, और उस table में तीन numbers ध्यान के लायक हैं।

cache turn 6 तक engage नहीं करता। prompt तब तक 1,024 tokens तक नहीं पहुँचता, इसलिए पहले पाँच turns बिल्कुल पहले जैसे billed होते हैं — और छठा worse billed होता है, 1.25× write premium पर, क्योंकि वही turn cache भरता है। पहला read turn 7 पर आता है। table में 2,887 uncached tokens का arithmetic यही है: पाँच turns का, छह नहीं। Caching long prompts पर discount है, और short conversation को इससे कुछ नहीं मिलता।

write premium $0.002474 है, जो cached bill का 2.8 % है। हर turn अपनी नई tail लिखता है, चालीस बार, और पूरा write premium reads ने जो बचाया उसके मुकाबले rounding error है। write charge को precisely समझना इसलिए worth है ताकि आप इसके बारे में चिंता बंद करें।

112,617 में से केवल 2,887 tokens full input price पर charge हुए। working cache का shape यही है: लगभग सब कुछ read है।

prompt का order तय करता है कि यह सब होगा भी या नहीं

सेक्शन का लिंक: prompt का order तय करता है कि यह सब होगा भी या नहीं

यह failure असली पैसा cost करता है, और यह one-line bug है।

prompt के front के पास कुछ ऐसा रखिए जो हर call पर बदलता है — timestamp, request id, user का नाम, "today is" line, freshly retrieved document — और prefix byte one से ही अलग हो जाता है। कुछ match नहीं करता। हर call miss है। और क्योंकि हर call novel prefix present करता है, हर call write भी करता है।

वही conversation, वही चालीस turns, caching enabled, system prompt के top पर per-call timestamp के साथ:

totalversus
no caching at all$0.274386
caching, stable prefix$0.088250−67.8 %
caching, volatile prefix$0.329251+20.0 %

prompt caching enable करने से conversation, उसे enable न करने की तुलना में बीस प्रतिशत ज्यादा महँगी हो गई। आपने 109,730 tokens पर 1.25× write premium दिया और zero वापस पढ़ा। कोई error नहीं, कोई warning नहीं, और feature switched on है।

तो rule, और एक line में prompt caching का पूरा सार: stable content आगे, variable content पीछे। System instructions, tool definitions और reference material पहले; timestamps, user identity और current question last। Anthropic hierarchy explicit बनाता है — cache toolssystemmessages follow करता है, और किसी level पर change उस level और उसके बाद सब कुछ invalidate कर देता है, इसलिए एक single tool description edit करना entire cache invalidate कर देता है।5

दो consequences हैं जिन पर लोग अटकते हैं। कौन-से tools enabled हैं यह बदलना tool definitions बदलता है, इसलिए कोई feature flag जो कुछ users के लिए tool जोड़ता है आपका cache दो भागों में split कर देता है। और Anthropic पर web search या citations toggle करना system prompt modify करता है, जो system और message caches को invalidate कर देता है, भले आप अपने text की एक line न छुएँ।

quadratic bill पर obvious response है पूरी history भेजना बंद करना: last dozen messages रखें और बाकी drop कर दें। इससे bill घटता है, और यह आम तौर पर wrong move है, और measurement बताता है क्यों।

strategytotalfull history + cache के विरुद्ध
full history, no cache$0.274386+211 %
full history, caching$0.088250
last 12 messages, no cache$0.118712+35 %
last 12 messages, caching on$0.122546+39 %

twelve-message window तक truncate करना सब कुछ uncached भेजने से 57 % सस्ता है — वही comparison जो हर कोई करता है, और इसी वजह से technique popular है। लेकिन working cache के साथ सब कुछ भेजने की तुलना में यह 39 % ज्यादा महँगा है, और truncation के साथ caching on करना इसे बेहतर नहीं बल्कि थोड़ा worse बनाता है।

mechanism फिर prefix है। sliding window हर turn पर oldest message drop करती है, इसलिए prompt अब वहाँ से शुरू नहीं होता जहाँ पिछली बार शुरू हुआ था और हर turn नया prefix present करता है। OpenAI की guidance ठीक यही कहती है: "summarisation, compaction, or context truncation can change the prefix and reset cache reuse."6 turn 40 तक windowed prompt 813 tokens है, 1,024-token minimum से नीचे, इसलिए इसे cache किया ही नहीं जा सकता।

और money cost का cheap half है। आपने जो drop किया वह user का turn 2 वाला instruction है जिसकी model को turn 40 पर जरूरत थी। Truncation ऐसा bill trade करता है जिसे आप देख सकते हैं, ऐसी failure के लिए जिसे आप नहीं देख सकते, और इसे properly करना — compaction, window के बाहर रखे structured notes, history को on demand retrieve करना — Chapter 24 का subject है।

tier पार करने पर पूरी request फिर से priced होती है

सेक्शन का लिंक: tier पार करने पर पूरी request फिर से priced होती है

Long contexts केवल इसलिए ज्यादा महँगे नहीं हैं कि वे लंबे हैं। threshold के बाद वे per token ज्यादा महँगे हैं, और threshold retroactively पूरे prompt पर apply होता है।

gpt-5.6-terra के लिए OpenAI का model page इसे एक sentence में कहता है: "Prompts with >272K input tokens are priced at 2x input and 1.5x output for the full request."7 excess के लिए नहीं। पूरी चीज़ के लिए।

the most expensive token you will ever sendTEXT
prompt 271,999 + 500 output  ->  $0.5500
prompt 272,000 + 500 output  ->  $0.5500
prompt 272,001 + 500 output  ->  $1.0970

एक token, पचपन cents। अगर आपकी service retrieved documents से prompts बनाती है जिनका size आप control नहीं करते, तो आपके cost model में ऐसी boundary पर cliff है जिसे आपकी team में किसी ने लिखा नहीं है।

Google pricing भी 200,000-token threshold के साथ इसी तरह काम करती है: Gemini 2.5 Pro 200K तक के prompts के लिए $1.25 per million input tokens है और उससे ऊपर $2.50, output $10.00 से $15.00 हो जाता है।8 Anthropic दूसरी दिशा में गया — 6 September 2026 तक इसकी documentation कहती है कि Claude 4.6 और later full one-million-token window को standard pricing में include करते हैं, इसलिए "a 900k-token request is billed at the same per-token rate as a 9k-token request."9 Earlier models में surcharge था।

इसीलिए price एक number नहीं है। price tiers की table है जो prompt length से keyed है, और cost function में Tier[] इसी के लिए है, और इसी वजह से computeCost हर bucket separately नहीं बल्कि whole prompt से tier select करता है।

Prefill, decode, और output input से छह गुना क्यों cost करता है

सेक्शन का लिंक: Prefill, decode, और output input से छह गुना क्यों cost करता है

पाँच buckets Chapter 13 के दो phases पर map होते हैं, और mapping दिखते ही price ratios arbitrary लगना बंद हो जाते हैं।

Input tokens prefill हैं। पूरा prompt एक pass में model से गुजरता है, parallel में processed — बड़े matrix multiplications, compute-bound। प्रति token cost low है, और यही phase time to first token set करता है: 4,947-token prompt में first word आने से पहले 4,947 tokens का prefill करना है।

Output tokens decode हैं। वे एक-एक करके produce होते हैं, हर एक full forward pass जो पूरे KV cache को read करता है, GPU mostly compute करने के बजाय memory की प्रतीक्षा करता है। यही phase tokens per second set करता है, इसे एक response के भीतर parallelise नहीं किया जा सकता, और इसी वजह से यहाँ priced model पर output input से लगभग छह गुना cost करता है: $12.00 बनाम $2.00 per million tokens।

तीन consequences सीधे निकलते हैं। cache read prefill work को replace करता है, इसलिए यह latency और money दोनों खरीदता है — वही discount lower bill और first token के लिए shorter wait के रूप में दिखता है। Reasoning tokens decode हैं जिन्हें आप कभी नहीं देखते, इसी वजह से reasoning model कई seconds तक कुछ stream नहीं करता और फिर जल्दी answer देता है: Chapter 12 ने interface consequence के बारे में warning दी थी, और यह invoice consequence है। और stream abort करना generation को stop नहीं करताChapter 14 ने cancellation बनाया और price को इस chapter के लिए छोड़ा, और price full output count है, क्योंकि tokens produce और billed होते हैं चाहे कोई सुन रहा हो या नहीं। उसी answer के लिए भी यही सच है जिसे कोई रखता नहीं: turn 40 answer को पाँच बार regenerate करने में screen पर बचे one answer के लिए $0.057990 लगते हैं।

Chapter 7 का tokenizer Python था और वहीं रहा। Budgeting उस server में होती है जो request बनाता है, इसलिए यह यहाँ होनी चाहिए, और accuracy के ठीक तीन levels उपलब्ध हैं।

Level one: locally count करें। js-tiktoken वही BPE merge tables ship करता है जो Python tiktoken, इसलिए OpenAI encodings के लिए byte-for-byte identical count, बिना network call:

count.tsTS
import { getEncoding } from "js-tiktoken";

const enc = getEncoding("o200k_base");
const PER_MESSAGE = 4;   // role and delimiters added by the chat template
const PER_REPLY = 3;     // priming for the assistant turn

export function promptTokens(messages: { role: string; content: string }[]) {
  return messages.reduce(
    (sum, m) => sum + enc.encode(m.content).length + PER_MESSAGE, PER_REPLY);
}

दो constants मायने रखते हैं और local counts यहीं drift करते हैं। आपका text वह नहीं है जो tokenize होता है — Chapter 11 का chat template पहले हर message को role markers में wrap करता है, और वे tokens हैं जिनके लिए आप pay करते हैं। हर message के लिए four और reply priming के लिए three OpenAI chat models के लिए conventional approximation है; ऊपर की conversation के eighty-one messages में वे 324 tokens बनते हैं, इसकी length का 6.4 %। यहाँ के counts Chapter 7 के Python tiktoken के against सभी eighty-one strings पर cross-check किए गए और identical हैं।

Level two: provider से पूछें। Anthropic /v1/messages/count_tokens expose करता है और Google count_tokens, दोनों real call जैसी same request shape accept करते हैं और free में input token count return करते हैं। इन्हें तब use करें जब आप locally count नहीं कर सकते — और Anthropic के लिए आप locally count नहीं कर सकते, जिसका tokenizer published नहीं है। Anthropic की documentation सावधानी से बताती है कि वह आपको क्या दे रही है: count "is an estimate" है, और यह "may include tokens added automatically by Anthropic for system optimizations", जिनके लिए "you are not billed".10

Level three: response में usage पढ़ें। वही truth है, और यह पैसे खर्च होने के बाद आता है। ठीक इसी वजह से पहले दो levels मौजूद हैं — request भेजनी है या नहीं decide करने के लिए, bill करने के लिए नहीं।

वे चीज़ें जिनके लिए आप pay करते हैं और जिन्हें कोई दिखाता नहीं

सेक्शन का लिंक: वे चीज़ें जिनके लिए आप pay करते हैं और जिन्हें कोई दिखाता नहीं

चार line items जो line items के रूप में appear नहीं होते।

system prompt, हर call पर paid। ऊपर वाला अपने template overhead के साथ 192 tokens है। चालीस calls में यह 7,680 tokens है — इस conversation के पूरे bill का 5.6 %, आठ lines के लिए जो एक बार लिखी गईं। यह best possible cache candidate भी है, क्योंकि stable और first दोनों है।

Tool definitions। हर tool का name, description और JSON schema हर request पर जाता है, और providers उसके ऊपर scaffolding add करते हैं। Anthropic number publish करता है: tools enable करना ही Claude Sonnet 4.5 पर tool_choice को auto set करने के साथ 496 tokens का hidden system prompt add करता है, या any या named tool के साथ 588।9 यह आपके own schemas से पहले है। Chapter 18 catalogue बनाता है; Chapter 24 measure करता है कि यह कितना खाता है।

हर generation, उनमें भी जिन्हें आप discard करते हैं। पाँच regenerations पाँच गुना cost करती हैं। chat एक दिखाता है।

Thoughts जो आपको नहीं दिखाए जाते। Billing full thought tokens पर based है, हालांकि केवल summary return होती है, और आपकी कोई accounting उस number को audit नहीं कर सकती।

close करने के लिए एक warning, क्योंकि natural next thought यही है और answer obvious नहीं है।

million-token window का मतलब million usable tokens नहीं है। Retrieval accuracy position के साथ degrade होती है: Liu et al. ने पाया कि models long input की शुरुआत और अंत में information reliably locate करते हैं और middle में बहुत कम reliably।11 बड़ी window ज्यादा भेजने की ability खरीदती है, read होने की certainty नहीं।

इस phenomenon को इस course में एक बार measure किया गया है — same 853-token prompt में nine positions पर retrieval rate — और यह Chapter 24 में belong करता है, जहाँ यह बदलता है कि agent क्या करता है। इसे यहाँ इसलिए cite किया गया है क्योंकि यह बदलता है कि आपको क्या खरीदना चाहिए: सबसे सस्ता token वह है जिसे आपने भेजा ही नहीं।

अब आप call करने से पहले predict कर सकते हैं कि उसका cost क्या होगा, बाद में पढ़ सकते हैं कि actual cost क्या हुआ, और दोनों के बीच फर्क बता सकते हैं। यह request के बारे में सब कुछ cover करता है सिवाय उस हिस्से के जिसे आपने अभी छुआ नहीं है: knobs।

Chapter 17 sampling है — temperature, top-p, top-k, penalties, और वह determinism जो आपके पास नहीं है। यह field की सबसे widespread error dismantle करके शुरू करता है, कि temperature creativity dial है। ऐसा नहीं है: temperature Chapter 4 के logits को softmax से पहले divide करता है, और इसे raise करने से model imaginative नहीं होता, यह उन tokens की probability बढ़ाता है जिन्हें model ने खुद worse score किया था। वहाँ से, greedy decoding sampling से measurably worse text क्यों produce करता है, top-k और top-p distribution की opposite shapes पर क्यों fail करते हैं, और chapter को खत्म करने वाला experiment: temperature 0 पर बीस identical forward passes bit-for-bit identical लौटते हैं जब model अकेला चलता है, और वही prompt किसी और की requests के साथ batch में रखने से उसके 97 % logits move हो जाते हैं।

वे सब match नहीं करते। वजह Chapter 2 के floating-point box से शुरू होती है।


इस chapter में सभी prices, thresholds और multipliers providers के अपने pages से 6 September 2026 को read किए गए और उस date के साथ stated हैं क्योंकि वे बदलेंगे। method numbers से ज्यादा matter करता है: buckets, prefix rule और tier arithmetic दो साल से stable हैं जबकि उनमें हर figure move हुआ है।

Stanford CS336 lecture 2, Resource accounting, इस material का closest academic treatment है और सही next read है: यह training side पर वही arithmetic करता है जो यह chapter inference side पर करता है। यहाँ token counts js-tiktoken 1.0.21 से o200k_base और cl100k_base encodings use करके produce किए गए, 5,090 tokens की forty-turn conversation पर; per-message template overhead conventional four-plus-three approximation है और जहाँ भी include है वहाँ stated है। cache, tier और truncation figures documented pricing rules हैं जो उन measured token counts पर applied हैं, live API responses के observations नहीं — इस chapter को produce करने के लिए कोई paid call नहीं किया गया, जो यह honest reason भी है कि latency claims qualitative हैं और cost claims नहीं।

  1. Dao, T., Fu, D. Y., Ermon, S., Rudra, A. and Ré, C. FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness. arXiv:2205.14135 (2022). asymptotic cost बदले बिना ceiling क्यों move हुई।

  2. Chen, S., Wong, S., Chen, L. and Tian, Y. Extending Context Window of Large Language Models via Positional Interpolation. arXiv:2306.15595 (2023).

  3. Peng, B., Quesnelle, J., Fan, H. and Shippole, E. YaRN: Efficient Context Window Extension of Large Language Models. arXiv:2309.00071 (2023).

  4. Google, Thinking, ai.google.dev/gemini-api/docs/thinking, और Token counting, ai.google.dev/gemini-api/docs/tokens, दोनों accessed 2026-09-06. "Pricing is based on the full thought tokens the model needs to generate, despite only the summary being output from the API." usage object total_input_tokens, total_output_tokens, total_thought_tokens, total_cached_tokens, total_tool_use_tokens और total_tokens report करता है — छह buckets, thoughts और tool use output count के बाहर। same quantity का earlier field name, जो अभी भी generateContent surface द्वारा returned है, thoughtsTokenCount है, जो तीसरे page ai.google.dev/gemini-api/docs/generate-content/thinking पर documented है।

  5. Anthropic, Prompt caching, docs.anthropic.com/en/docs/build-with-claude/prompt-caching, accessed 2026-09-06. toolssystemmessages invalidation hierarchy और उसकी table का source; per-model minimum cacheable lengths; identity total_input_tokens = cache_read_input_tokens + cache_creation_input_tokens + input_tokens; और five-minute default lifetime जो हर hit पर बिना charge refresh होता है। 2

  6. OpenAI, Prompt caching, platform.openai.com/docs/guides/prompt-caching, accessed 2026-09-06. Source of: entire-rendered-prefix rule; minimum cacheable prefix (GPT-5.6 और later पर 1,024 visible input tokens, earlier पर 2,048); 1.25× write और 0.1× read multipliers, और GPT-5.5 और earlier पर किसी write charge का absence; 30-minute lifetime; four-writes-per-request और fifty-breakpoint limits; machine-affinity note और prompt_cache_key; 1.35×, 2.15× और 10× break-even worked examples; और statement कि summarisation, compaction या truncation cache reuse reset करता है। 2

  7. OpenAI, Pricing (platform.openai.com/docs/pricing) और gpt-5.6-terra के लिए model page, दोनों accessed 2026-09-06. gpt-5.6-terra, standard service tier, per million tokens: input $2.00, cached input $0.20, cache writes $2.50, output $12.00; long context input $4.00, cached $0.40, writes $5.00, output $18.00; "prompts with >272K input tokens are priced at 2x input and 1.5x output for the full request"; context window 1,050,000 tokens with a maximum of 922,000 input tokens. वही table gpt-6-astra को $10.00/$1.00/$12.50/$50.00 पर और gpt-5.6-luna को $0.20/$0.02/$0.25/$1.20 पर list करती है। इस chapter की हर worked cost gpt-5.6-terra standard short-context rates use करती है।

  8. Google, Gemini Developer API pricing, ai.google.dev/gemini-api/docs/pricing, accessed 2026-09-06. Gemini 2.5 Pro, per million tokens: 200K तक prompts के लिए input $1.25 और ऊपर $2.50; output $10.00 और $15.00, दोनों cases में "including thinking tokens" labelled; context caching $0.125 और $0.25, plus $4.50 per million tokens per hour storage charge. Gemini 3.1 Pro Preview same 200K threshold use करता है $2.00/$4.00 input और $12.00/$18.00 output पर।

  9. Anthropic, Pricing, docs.anthropic.com/en/docs/about-claude/pricing, accessed 2026-09-06. Per million tokens, base input / 5-minute cache write / 1-hour cache write / cache read / output: Claude Sonnet 4.5 $3 / $3.75 / $6 / $0.30 / $15; Claude Haiku 4.5 $1 / $1.25 / $2 / $0.10 / $5; Claude Opus 5 $5 / $6.25 / $10 / $0.50 / $25. Multipliers: five-minute write के लिए 1.25×, one-hour write के लिए 2×, read के लिए 0.1×। long-context statement का भी source ("Claude 4.6 and later models... include the full 1M token context window at standard pricing"), tool-use system prompt token counts (Claude Sonnet 4.5 पर tool_choice of auto or none के साथ 496 tokens, any या named tool के साथ 588), और note कि Claude 4.7 और later newer tokenizer use करते हैं जो "approximately 30 % more tokens for the same text" produce करता है। 2 3

  10. Anthropic, Token counting, docs.anthropic.com/en/docs/build-with-claude/token-counting, accessed 2026-09-06. /v1/messages/count_tokens endpoint message जैसे same inputs लेता है और input token count return करता है; documentation states कि count estimate है, कि इसमें system optimisations के लिए Anthropic द्वारा add किए गए tokens शामिल हो सकते हैं, और वे billed नहीं होते।

  11. Liu, N. F., Lin, K., Hewitt, J., Paranjape, A., Bevilacqua, M., Petroni, F. and Liang, P. Lost in the Middle: How Language Models Use Long Contexts. arXiv:2307.03172 (2023). यहाँ cited, Chapter 24 में measured।


निर्माता

David Vicente Campos

NeuraLIA Labs के संस्थापक और MyRealFood के सह-संस्थापक

मैं लेओन विश्वविद्यालय से कंप्यूटर इंजीनियर हूँ। मैंने MyRealFood की सह-स्थापना की, जहाँ CTO के रूप में मैंने वह ऐप बनाया जिसे लाखों लोग बेहतर खान-पान के लिए इस्तेमाल कर चुके हैं, और मैंने NeuraLIA Labs की स्थापना की, जहाँ मैं AI प्रोडक्ट्स बनाता हूँ। यहाँ मैं उन बातों के बारे में लिखता हूँ जो इस सफ़र में मुझे समझनी पड़ीं, उस तरह जिस तरह काश किसी ने मुझे समझाई होतीं।

लेखक के बारे में और जानें

NeuraLIA Labs द्वारा प्रकाशित।

नए पोस्ट अपने इनबॉक्स में पाएं

AI समाचार, गाइड और प्रोडक्ट अपडेट — जब भी हम कुछ उपयोगी प्रकाशित करें, एक छोटा ईमेल।

कोर्स सूची

Abstract software decision engine with branching paths, probability nodes, and glowing gates.
jev13 मिनट पढ़ें

Jev AI मॉडल गद्य के लिए नहीं, निर्णयों के लिए बना है

TypeSafe AI का Jev ध्यान खींच रहा है क्योंकि यह सॉफ्टवेयर इंटेलिजेंस को संभावना की समस्या मानता है: सही शाखा चुनें, भरोसे का स्तर जोड़ें, और जब कोड को निर्णय चाहिए तो LLM से टेक्स्ट लिखवाने पर खर्च न करें।

Abstract agent runtime sorting documents, memory blocks and pointer nodes inside a bounded context frame.
context-engineering14 मिनट पढ़ें

लॉन्ग-होराइजन AI एजेंट्स के लिए कॉन्टेक्स्ट इंजीनियरिंग

लंबे समय तक चलने वाले एजेंट सिर्फ इसलिए असफल नहीं होते कि विंडो छोटी है। वे तब असफल होते हैं जब फ़ाइलें, टूल आउटपुट और पुराना इतिहास उस काम को ही पीछे धकेल देते हैं जिसे एजेंट को पूरा करना था।

मॉडल चुनने का काम LIA पर छोड़ने के लिए तैयार हैं?

हर AI मॉडल एक ही जगह — आज ही मुफ़्त शुरू करें।