AI tokens बनाम शब्द: लागत से पहले request मापें
Token किसी खास tokenizer की बनाई इकाई है, शब्दों या characters की निश्चित संख्या नहीं। Exact text count के लिए matching tokenizer और settings लें। पूरी API request में message structure तथा tools भी गिनें और अनुमान को provider के returned usage से मिलाएं।
इस मार्गदर्शिका में
लेख की length requirement के लिए word counter उपयोगी है, लेकिन उससे AI request के context में fit होने या लागत का भरोसेमंद पता नहीं चलता। एक ही visible text अलग tokenizers में अलग टूट सकता है। Request में pasted user message से अधिक सामग्री भी रहती है।
यह workflow raw text, पूरी request और billed usage अलग मापता है। स्थानीय counting example और वास्तविक results वाली worksheet दी गई है। किसी न चलाए गए model के exact counts गढ़े नहीं गए; measurements को उन्हीं settings के साथ दर्ज करना जरूरी है।
Words, Unicode characters और tokens अलग समझें
एक दिखाई देने वाला शब्द एक token, कई subword tokens या punctuation तथा spaces समेत combination बन सकता है। Hugging Face word, character और subword approaches समझाता है। Language, spelling, code, whitespace और vocabulary विभाजन बदल सकते हैं।
Unicode count दूसरा सवाल है। कुछ characters कई code units या bytes लेते हैं; एक visible symbol कई code points भी हो सकता है। इनका model tokens में सार्वभौमिक conversion नहीं है। Characters-per-token को किसी stated sample की rough heuristic मानें, सभी भाषाओं का नियम नहीं।
| काम | उपयोगी माप | उसकी सीमा |
|---|---|---|
| लेख की length जांच | शब्द या Unicode characters | model tokens स्थापित नहीं होते |
| एक tokenizer से raw file मापना | उसी tokenizer और settings के token IDs | chat formatting और overhead छूट सकते हैं |
| Structured API request का अनुमान | provider-supported count या exact format | final usage से फर्क संभव |
| Billed usage का मिलान | वास्तविक usage fields और current billing definitions | दूसरे provider का estimate नहीं |
ज्ञात tokenizer से स्थानीय sample गिनें
Hugging Face tokenizers format के लिए सही local tokenizer.json लोड करके UTF-8 file encode करें। Model/tokenizer identity, file revision, package version और special tokens की setting दर्ज करें। उदाहरण tokenizers 0.23.2 API का है, padding तथा truncation बंद करता है और extra special tokens बिना raw text गिनता है।
Tokenizer file intended model के official distribution से लें। यह example model download या text upload नहीं करता और hosted chat API का पूरा format नहीं बनाता। Count तभी सही सवाल का उत्तर देता है जब tokenizer तथा settings उस माप से मेल खाएं; local count अपने आप bill नहीं है।
# Example environment: tokenizers==0.23.2
from pathlib import Path
from importlib.metadata import version
from tokenizers import Tokenizer
# Use the matching tokenizer.json already saved locally.
tokenizer = Tokenizer.from_file("tokenizer.json")
tokenizer.no_padding()
tokenizer.no_truncation()
text = Path("sample.txt").read_text(encoding="utf-8")
encoded = tokenizer.encode(text, add_special_tokens=False)
print("tokenizers version:", version("tokenizers"))
print("raw-text tokens:", len(encoded.ids))
# Record tokenizer identity/revision separately.
# This is not a complete chat-request or billing count.Context planning में पूरी request शामिल करें
Chat request में system instructions, history, retrieved passages, tool definitions और formatting user message के अलावा हो सकते हैं। Chat template roles तथा boundaries के आसपास control tokens जोड़ सकता है। सिर्फ आखिरी pasted paragraph गिनना input कम दिखा सकता है।
Hosted provider की documented counting facility आपके input types को support करे तो वही लें। Claude documentation structured inputs तथा उन स्थितियों का वर्णन करता है जहां actual usage reporting चाहिए। Tools, images और documents के लिए provider की घोषित estimation limitations मानें।
Unknown online counters में credentials या sensitive customer text न डालें। Local tokenizer raw text अपने device पर गिनता है; provider endpoint को supplied request भेजी जाती है। डेटा के लिए उचित workflow पहले चुनें, उसके बाद measurement करें।
दोहराने योग्य comparison worksheet बनाएं
Support message, code snippet, table और application की हर भाषा का passage जैसे representative samples लें। Exact input bytes सुरक्षित रखें ताकि runs के बीच line endings, punctuation या whitespace अनजाने न बदलें। बदली sample को tokenizer का फर्क न कहें।
हर sample के लिए tokenizer identity/revision, special-token setting, raw count और उपलब्ध full-request estimate लिखें। Unmeasured value को guessed ratio से भरने के बजाय खाली छोड़ें। दो tokenizers का अलग count इन samples का result है, उनकी language quality की ranking नहीं।
| नमूना | Tokenizer और revision | Raw-text count का माप | Full-request count या estimate |
|---|---|---|---|
| सहायता का संदेश | exact identifier लिखें | स्थानीय रूप से मापें | supported request format में मापें |
| Code का अंश | exact identifier लिखें | स्थानीय रूप से मापें | supported request format में मापें |
| दूसरी भाषा का passage | exact identifier लिखें | स्थानीय रूप से मापें | supported request format में मापें |
अधिक input से पहले output की जगह बचाएं
काल्पनिक योजना में request type के लिए combined context 32,000 tokens मानें। Output 4,000 और buffer 2,000 रखें तो measured input के लिए 26,000 बचते हैं। Instructions, history और tools 8,000 लें तो retrieved content के लिए 18,000 बचेंगे।
ये स्पष्ट hypothetical numbers हैं, किसी current model की specification नहीं। Providers context, output, reasoning और tool usage अलग परिभाषित कर सकते हैं। Current contract पढ़कर actual quantities context-budget calculator में डालें। सीमा में फिट request की हर passage उपयोगी हो, यह जरूरी नहीं।
Hypothetical planning budget:
32,000 combined context
− 4,000 output reserve
− 2,000 planning buffer
= 26,000 input budget
26,000 − 8,000 instructions/history/tools
= 18,000 available for retrieved contentअनुमान को actual usage और cost से मिलाएं
छोटे वास्तविक test के बाद provider के returned usage fields उसी request के estimate से मिलाएं। Documented cached input, output, reasoning और दूसरी billable categories अलग रखें। Tool definition छूटने की गलती word-to-token ratio बदलकर नहीं सुधरती।
AI token-cost calculator में measured counts और आपके दिए current prices लें। Input/output अलग और cost per million tokens जैसी price unit स्पष्ट रखें। उपयोगी worksheet stated rates और actual usage से पुनः बन सके; उसे universal word conversion पर निर्भर न करें।
आम सवाल
क्या एक token हमेशा चार characters है?
नहीं। कुछ text और tokenizers के लिए यह rough heuristic हो सकती है। भाषा, code, punctuation, whitespace और design परिणाम बदलते हैं।
Local count और API usage अलग क्यों है?
Request में formatting, instructions, history, tools और raw sample से अलग input types हो सकते हैं। Tokenizer या special-token settings भी अलग हो सकती हैं। पूरी समान request और provider definitions तुलना करें।
क्या इस site का word counter exact model tokens देता है?
नहीं। वह words तथा Unicode characters जैसे text measurements देता है। Matching tokenizer या supported provider counter लें, फिर measured inputs cost और context tools में डालें।
स्रोत और परिभाषाएँ
- Hugging Face LLM Course: Tokenizers
- Claude Platform: Token counting
- Hugging Face tokenizers 0.23.2: published Python package and API examples
- Hugging Face Transformers: Chat templates
विधि की जाँच की गई: