{"course":{"slug":"python-for-ai","title":"Python, from Zero, for AI","url":"https://addaly.com/learn/python-for-ai","level":"beginner","track":"build"},"lesson":{"slug":"counting-tokens-and-cost","title":"Knowing what each call cost: usage figures, token counting, and a budget that stops the program","url":"https://addaly.com/learn/python-for-ai/counting-tokens-and-cost","position":59,"of":89,"minutes":9,"answer":"Every response carries its own token counts; multiply them by the price table, keep a running total, and raise before the cap is crossed, and count Hindi or code prompts before sending.","keyIdea":"Every response carries its own token counts; multiply them by the price table, keep a running total, and raise before the cap is crossed, and count Hindi or code prompts before sending, because the four-characters-per-token rule of thumb is off by two to three times for them."},"body":{"format":"markdown","text":"## The response tells you\n\nEvery model API returns the token counts for the call it just served. Anthropic:\n\n```python\nresp = client.messages.create(model=m, max_tokens=500, messages=msgs)\nprint(resp.usage.input_tokens, resp.usage.output_tokens)\n```\n\nOpenAI:\n\n```python\nresp = client.chat.completions.create(model=m, messages=msgs)\nprint(resp.usage.prompt_tokens, resp.usage.completion_tokens)\n```\n\nWith raw `requests`, it is `r.json()[\"usage\"]`. With a stream, it arrives in the final event (Anthropic's `message_delta`, OpenAI's last chunk when you pass `stream_options={\"include_usage\": True}`) which is one more reason to consume a stream to its end.\n\nThese are the numbers you are billed for. Not your estimate, not the character count, these.\n\n## Turning tokens into money\n\nPrices are per million tokens, input and output priced differently, and they change. Keep them in one place, dated:\n\n```python\n# prices per million tokens, USD, checked 2026-09\nPRICES = {\n    \"claude-sonnet-4-5\":  {\"in\": 3.00,  \"out\": 15.00},\n    \"gpt-4o-mini\":        {\"in\": 0.15,  \"out\": 0.60},\n}\n\ndef cost_usd(model, usage_in, usage_out):\n    p = PRICES[model]\n    return (usage_in * p[\"in\"] + usage_out * p[\"out\"]) / 1_000_000\n```\n\nA 2,000-token prompt with a 500-token reply on the first model: `(2000 × 3 + 500 × 15) / 1,000,000` = $0.0135. On the second: $0.0006. The ratio between those two is the ratio between \"run it over the whole dataset tonight\" and \"think first\". Output tokens cost five times input on most models, so a verbose reply costs more than a long prompt; asking for brevity is a cost control.\n\n## A running total, and a stop\n\n```python\nclass Budget:\n    def __init__(self, cap_usd):\n        self.cap = cap_usd\n        self.spent = 0.0\n        self.calls = 0\n\n    def charge(self, model, usage_in, usage_out):\n        c = cost_usd(model, usage_in, usage_out)\n        self.spent += c\n        self.calls += 1\n        log.info(\n            \"call %d: %d in, %d out, $%.4f, total $%.2f\",\n            self.calls,\n            usage_in,\n            usage_out,\n            c,\n            self.spent,\n        )\n        if self.spent > self.cap:\n            raise RuntimeError(\n                (\n                    f\"budget of ${self.cap} exceeded \"\n                    f\"after {self.calls} calls\"\n                )\n            )\n```\n\nCall `budget.charge(...)` after every response. The `raise` is the important line. A loop that logs the total but never stops is a loop that emails you a bill; a loop that raises at the cap is one that emails you a traceback, which is cheaper. Set the cap below the provider-side spending limit you configured in the dashboard, so your code stops before the account does.\n\nLog the cost per call at `INFO`. When a job costs more than expected, the log shows which calls were large and whether it was input or output.\n\n## Counting before you send\n\nSometimes you need the count before the call: to check a prompt fits the context window, to estimate a batch, to decide whether to chunk. Each provider has a way.\n\nOpenAI's tokeniser is open, as the `tiktoken` package:\n\n```python\nimport tiktoken\nenc = tiktoken.encoding_for_model(\"gpt-4o-mini\")\nprint(len(enc.encode(text)))\n```\n\nAnthropic offers a counting endpoint that costs nothing:\n\n```python\nn = client.messages.count_tokens(model=m, messages=msgs).input_tokens\n```\n\nOpen models ship their tokeniser with the weights; `transformers` loads it with `AutoTokenizer.from_pretrained(name)` and `len(tok.encode(text))`. Each model's tokeniser is different, so a count from one is only approximately right for another.\n\n> Figure: How far a thousand characters goes, by what you send. Bars (tokens per 1,000 characters): English prose 250; Hinglish in Latin letters 320; JSON with keys and indentation 400; A table of long numbers 450; Hindi in Devanagari 660. The four-characters-per-token rule is an English rule. The same message costs about two and a half times as much in Devanagari, which changes the bill, how much fits in the context window, and where a prompt gets truncated. For anything you are billed for, count with the real tokeniser rather than the rule.\n\n## The rule of thumb, and where it fails\n\n\"About four characters per token\" is the number everyone quotes. It is roughly true for English prose, and it fails badly in three cases your program will meet.\n\n**Non-Latin scripts.** Tokenisers are trained on data dominated by English, so they have short pieces for English and long strings of Devanagari, Tamil, Arabic or Chinese get split into many small pieces. Hindi text commonly comes out at two to three times the tokens of an English translation of the same meaning. Hinglish in Latin script is closer to English. Measure your own text with the real tokeniser before estimating a batch; a job priced on the four-character rule for Indian-language input will come in far over.\n\n**Code and JSON.** Punctuation, indentation and brackets each tend to be their own token. A JSON document can run at two to three characters per token.\n\n**Numbers.** Long digit strings are split into small groups. A table of figures is expensive per character.\n\nThe honest rule is: the rule of thumb is for a quick sanity check on English; for anything you will be billed for, count with the tokeniser.\n\n## Where the tokens hide\n\nThe input count is more than your prompt. It includes the system prompt, every earlier turn you sent back for context, any tool definitions, and provider formatting around all of it. A chat loop that appends every turn grows its input count on every call; by turn thirty the input is thirty turns long and the cost per call has climbed with it. Module 9 handles trimming history; the reason to do it is here.\n\nPrompt caching, where a provider offers it, changes the arithmetic: a cached prefix is billed at a fraction of the rate, and the usage figures break it out into separate fields. Read them the same way and price them at the cached rate, or your cost figure will be wrong in the pessimistic direction.\n\n## Try this now\n\nTake twenty messages in whatever languages your users write, count each with a real tokeniser, and compute characters per token for each. Then compute what the four-character rule would have predicted. The gap for your own data is the number to remember."},"check":{"question":"A developer estimates the cost of a batch job by taking each prompt's length in characters, dividing by four, and multiplying by the per-token price. The prompts are customer messages, about half in Hindi written in Devanagari. The actual bill comes in at roughly two and a half times the estimate. What is the mechanism?","options":[{"text":"The provider charges Hindi text at a higher per-token rate than English.","correct":false,"tempting":"Pricing is per token regardless of language; the difference is in how many tokens the text becomes."},{"text":"The estimate forgot that output tokens exist, and the model's replies doubled the count.","correct":false,"tempting":"Replies would add to the bill for every prompt equally, and the question asks what specifically distorted the estimate for this batch."},{"text":"Devanagari characters are stored as multiple bytes in UTF-8, and the API counts bytes rather than characters, so each Hindi character is billed as two or three tokens.","correct":false,"tempting":"Tokens are neither bytes nor characters; a tokeniser works on learned pieces, and byte length is not the unit billed."},{"text":"Tokenisers are trained mostly on English, so Devanagari splits into far more tokens per character, and the four-character rule undercounts it.","correct":true}]},"prev":{"slug":"validating-what-the-model-returns","title":"Validating what the model returns: from a string that looks like JSON to an object you can trust","url":"https://addaly.com/learn/python-for-ai/validating-what-the-model-returns"},"next":{"slug":"classes-and-objects","title":"Classes: when a dict stops being enough","url":"https://addaly.com/learn/python-for-ai/classes-and-objects"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'Knowing what each call cost: usage figures, token counting, and a budget that stops the program', in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/counting-tokens-and-cost (Accessed: 21 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"Knowing what each call cost: usage figures, token counting, and a budget that stops the program","courseTitle":"Python, from Zero, for AI","module":{"index":6,"slug":"talking-to-a-service","title":"Talking to a service: HTTP from Python, done properly"},"answer":null,"keyIdea":"Every response carries its own token counts; multiply them by the price table, keep a running total, and raise before the cap is crossed, and count Hindi or code prompts before sending, because the four-characters-per-token rule of thumb is off by two to three times for them.","hero":{"kind":"figure","figure":{"t":"figure","label":"Figure","figure":{"caption":"The four-characters-per-token rule is an English rule. The same message costs about two and a half times as much in Devanagari, which changes the bill, how much fits in the context window, and where a prompt gets truncated. For anything you are billed for, count with the real tokeniser rather than the rule.","series":[{"label":"English prose","value":250},{"label":"Hinglish in Latin letters","value":320},{"label":"JSON with keys and indentation","value":400},{"label":"A table of long numbers","value":450},{"highlight":true,"label":"Hindi in Devanagari","value":660}],"title":"How far a thousand characters goes, by what you send","type":"bars","unit":"tokens per 1,000 characters"},"alt":"Figure: How far a thousand characters goes, by what you send. Bars (tokens per 1,000 characters): English prose 250; Hinglish in Latin letters 320; JSON with keys and indentation 400; A table of long numbers 450; Hindi in Devanagari 660. The four-characters-per-token rule is an English rule. The same message costs about two and a half times as much in Devanagari, which changes the bill, how much fits in the context window, and where a prompt gets truncated. For anything you are billed for, count with the real tokeniser rather than the rule."}},"blocks":[{"t":"h2","id":"the-response-tells-you","runs":[{"text":"The response tells you"}]},{"t":"p","runs":[{"text":"Every model API returns the token counts for the call it just served. Anthropic:"}]},{"t":"code","lang":"python","label":"Python","text":"resp = client.messages.create(model=m, max_tokens=500, messages=msgs)\nprint(resp.usage.input_tokens, resp.usage.output_tokens)"},{"t":"p","runs":[{"text":"OpenAI:"}]},{"t":"code","lang":"python","label":"Python","text":"resp = client.chat.completions.create(model=m, messages=msgs)\nprint(resp.usage.prompt_tokens, resp.usage.completion_tokens)"},{"t":"p","runs":[{"text":"With raw "},{"text":"requests","code":true},{"text":", it is "},{"text":"r.json()[\"usage\"]","code":true},{"text":". With a stream, it arrives in the final event (Anthropic’s "},{"text":"message_delta","code":true},{"text":", OpenAI’s last chunk when you pass "},{"text":"stream_options={\"include_usage\": True}","code":true},{"text":") which is one more reason to consume a stream to its end."}]},{"t":"p","runs":[{"text":"These are the numbers you are billed for. Not your estimate, not the character count, these."}]},{"t":"h2","id":"turning-tokens-into-money","runs":[{"text":"Turning tokens into money"}]},{"t":"p","runs":[{"text":"Prices are per million tokens, input and output priced differently, and they change. Keep them in one place, dated:"}]},{"t":"code","lang":"python","label":"Python","text":"# prices per million tokens, USD, checked 2026-09\nPRICES = {\n    \"claude-sonnet-4-5\":  {\"in\": 3.00,  \"out\": 15.00},\n    \"gpt-4o-mini\":        {\"in\": 0.15,  \"out\": 0.60},\n}\n\ndef cost_usd(model, usage_in, usage_out):\n    p = PRICES[model]\n    return (usage_in * p[\"in\"] + usage_out * p[\"out\"]) / 1_000_000"},{"t":"p","runs":[{"text":"A 2,000-token prompt with a 500-token reply on the first model: "},{"text":"(2000 × 3 + 500 × 15) / 1,000,000","code":true},{"text":" = $0.0135. On the second: $0.0006. The ratio between those two is the ratio between “run it over the whole dataset tonight” and “think first”. Output tokens cost five times input on most models, so a verbose reply costs more than a long prompt; asking for brevity is a cost control."}]},{"t":"h2","id":"a-running-total-and-a-stop","runs":[{"text":"A running total, and a stop"}]},{"t":"code","lang":"python","label":"Python","text":"class Budget:\n    def __init__(self, cap_usd):\n        self.cap = cap_usd\n        self.spent = 0.0\n        self.calls = 0\n\n    def charge(self, model, usage_in, usage_out):\n        c = cost_usd(model, usage_in, usage_out)\n        self.spent += c\n        self.calls += 1\n        log.info(\n            \"call %d: %d in, %d out, $%.4f, total $%.2f\",\n            self.calls,\n            usage_in,\n            usage_out,\n            c,\n            self.spent,\n        )\n        if self.spent > self.cap:\n            raise RuntimeError(\n                (\n                    f\"budget of ${self.cap} exceeded \"\n                    f\"after {self.calls} calls\"\n                )\n            )"},{"t":"p","runs":[{"text":"Call "},{"text":"budget.charge(...)","code":true},{"text":" after every response. The "},{"text":"raise","code":true},{"text":" is the important line. A loop that logs the total but never stops is a loop that emails you a bill; a loop that raises at the cap is one that emails you a traceback, which is cheaper. Set the cap below the provider-side spending limit you configured in the dashboard, so your code stops before the account does."}]},{"t":"p","runs":[{"text":"Log the cost per call at "},{"text":"INFO","code":true},{"text":". When a job costs more than expected, the log shows which calls were large and whether it was input or output."}]},{"t":"h2","id":"counting-before-you-send","runs":[{"text":"Counting before you send"}]},{"t":"p","runs":[{"text":"Sometimes you need the count before the call: to check a prompt fits the context window, to estimate a batch, to decide whether to chunk. Each provider has a way."}]},{"t":"p","runs":[{"text":"OpenAI’s tokeniser is open, as the "},{"text":"tiktoken","code":true},{"text":" package:"}]},{"t":"code","lang":"python","label":"Python","text":"import tiktoken\nenc = tiktoken.encoding_for_model(\"gpt-4o-mini\")\nprint(len(enc.encode(text)))"},{"t":"p","runs":[{"text":"Anthropic offers a counting endpoint that costs nothing:"}]},{"t":"code","lang":"python","label":"Python","text":"n = client.messages.count_tokens(model=m, messages=msgs).input_tokens"},{"t":"p","runs":[{"text":"Open models ship their tokeniser with the weights; "},{"text":"transformers","code":true},{"text":" loads it with "},{"text":"AutoTokenizer.from_pretrained(name)","code":true},{"text":" and "},{"text":"len(tok.encode(text))","code":true},{"text":". Each model’s tokeniser is different, so a count from one is only approximately right for another."}]},{"t":"h2","id":"the-rule-of-thumb-and-where-it-fails","runs":[{"text":"The rule of thumb, and where it fails"}]},{"t":"p","runs":[{"text":"“About four characters per token” is the number everyone quotes. It is roughly true for English prose, and it fails badly in three cases your program will meet."}]},{"t":"p","runs":[{"text":"Non-Latin scripts.","b":true},{"text":" Tokenisers are trained on data dominated by English, so they have short pieces for English and long strings of Devanagari, Tamil, Arabic or Chinese get split into many small pieces. Hindi text commonly comes out at two to three times the tokens of an English translation of the same meaning. Hinglish in Latin script is closer to English. Measure your own text with the real tokeniser before estimating a batch; a job priced on the four-character rule for Indian-language input will come in far over."}]},{"t":"p","runs":[{"text":"Code and JSON.","b":true},{"text":" Punctuation, indentation and brackets each tend to be their own token. A JSON document can run at two to three characters per token."}]},{"t":"p","runs":[{"text":"Numbers.","b":true},{"text":" Long digit strings are split into small groups. A table of figures is expensive per character."}]},{"t":"p","runs":[{"text":"The honest rule is: the rule of thumb is for a quick sanity check on English; for anything you will be billed for, count with the tokeniser."}]},{"t":"h2","id":"where-the-tokens-hide","runs":[{"text":"Where the tokens hide"}]},{"t":"p","runs":[{"text":"The input count is more than your prompt. It includes the system prompt, every earlier turn you sent back for context, any tool definitions, and provider formatting around all of it. A chat loop that appends every turn grows its input count on every call; by turn thirty the input is thirty turns long and the cost per call has climbed with it. Module 9 handles trimming history; the reason to do it is here."}]},{"t":"p","runs":[{"text":"Prompt caching, where a provider offers it, changes the arithmetic: a cached prefix is billed at a fraction of the rate, and the usage figures break it out into separate fields. Read them the same way and price them at the cached rate, or your cost figure will be wrong in the pessimistic direction."}]},{"t":"h2","id":"try-this-now","runs":[{"text":"Try this now"}],"role":"try"},{"t":"p","runs":[{"text":"Take twenty messages in whatever languages your users write, count each with a real tokeniser, and compute characters per token for each. Then compute what the four-character rule would have predicted. The gap for your own data is the number to remember."}]}],"check":{"question":[{"text":"A developer estimates the cost of a batch job by taking each prompt’s length in characters, dividing by four, and multiplying by the per-token price. The prompts are customer messages, about half in Hindi written in Devanagari. The actual bill comes in at roughly two and a half times the estimate. What is the mechanism?"}],"options":[{"text":[{"text":"The provider charges Hindi text at a higher per-token rate than English."}],"correct":false,"tempting":[{"text":"Pricing is per token regardless of language; the difference is in how many tokens the text becomes."}]},{"text":[{"text":"The estimate forgot that output tokens exist, and the model’s replies doubled the count."}],"correct":false,"tempting":[{"text":"Replies would add to the bill for every prompt equally, and the question asks what specifically distorted the estimate for this batch."}]},{"text":[{"text":"Devanagari characters are stored as multiple bytes in UTF-8, and the API counts bytes rather than characters, so each Hindi character is billed as two or three tokens."}],"correct":false,"tempting":[{"text":"Tokens are neither bytes nor characters; a tokeniser works on learned pieces, and byte length is not the unit billed."}]},{"text":[{"text":"Tokenisers are trained mostly on English, so Devanagari splits into far more tokens per character, and the four-character rule undercounts it."}],"correct":true,"tempting":null}],"order":[1,0,3,2],"why":null},"prev":{"slug":"validating-what-the-model-returns","title":"Validating what the model returns: from a string that looks like JSON to an object you can trust","minutes":9},"next":{"slug":"classes-and-objects","title":"Classes: when a dict stops being enough","minutes":9}}}