Python, from Zero, for AILesson 59 of 89

Module 6 · Talking to a service: HTTP from Python, done properly

Knowing what each call cost: usage figures, token counting, and a budget that stops the program

In this lesson

  • 9 min read
  • 7 reading sections
  • Knowledge check
  • Lesson 11 of 11 in this chapter

This chapter’s goalWrite a client for a paid model API that reuses connections, times out, retries only what is safe to retry, walks pagination with a generator, consumes a streamed response as it arrives, runs many requests concurrently under a rate limit, validates the JSON the model returns before trusting it, and reports the cost of every call from the usage figures in the response.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch2:28Not available
Full audiobookNot availableNot available
How far a thousand characters goes, by what you sendEnglish prose250Hinglish in Latin letters320JSON with keys andindentation400A table of long numbers450Hindi in Devanagari660tokens per 1,000 charactersThe four-characters-per-token rule is an English rule. The same message costs about two and a halftimes as much in Devanagari, which changes the bill, how much fits in the context window, and where aprompt gets truncated. For anything you are billed for, count with the real tokeniser rather than therule.
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.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 9Free · no account needed
Lesson steps

The response tells you

Every model API returns the token counts for the call it just served. Anthropic:

Python
resp = client.messages.create(model=m, max_tokens=500, messages=msgs)
print(resp.usage.input_tokens, resp.usage.output_tokens)

OpenAI:

Python
resp = client.chat.completions.create(model=m, messages=msgs)
print(resp.usage.prompt_tokens, resp.usage.completion_tokens)

With 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.

These are the numbers you are billed for. Not your estimate, not the character count, these.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to 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).