0Pricing
AI Agents · Lesson

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

  1. Calling OpenAI API: chat.completions
  2. Calling Anthropic API: messages
  3. Streaming Responses (SSE)
  4. Cost Awareness: Token Counting and Budgets
← Back to AI Agents