Cost Awareness: Token Counting and Budgets
Count tokens with tiktoken, estimate cost before you send, and enforce per-request and per-day budgets.
Why Counting Tokens Matters
You pay per token. A runaway agent loop with no token budget can spend hundreds of dollars in minutes.
Token counting is the single most important cost-control habit.
What Is a Token?
A token is a chunk of text the model processes. Rough rules of thumb for English:
- 1 token ~ 4 characters
- 1 token ~ 0.75 words
- 100 tokens ~ 75 words
Code, emoji, and non-English text use more tokens per character.
All lessons in this course
- Calling OpenAI API: chat.completions
- Calling Anthropic API: messages
- Streaming Responses (SSE)
- Cost Awareness: Token Counting and Budgets