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.

Cost Awareness: Token Counting and Budgets is a free AI Agents lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Count Tokens with tiktoken (OpenAI)

OpenAI ships an exact tokenizer:

# pip install tiktoken
import tiktoken

enc = tiktoken.encoding_for_model('gpt-4o-mini')
tokens = enc.encode('Hello, world!')
print(len(tokens))   # 4
print(tokens)        # [9906, 11, 1917, 0]

Count Tokens in Messages

The full count includes message structure overhead:

def count_message_tokens(messages, model='gpt-4o-mini'):
    enc = tiktoken.encoding_for_model(model)
    total = 0
    for m in messages:
        total += 4  # role + structural
        total += len(enc.encode(m['content']))
    return total + 2  # priming

Count Tokens with Anthropic

Anthropic offers a server-side count endpoint:

count = client.messages.count_tokens(
    model='claude-sonnet-4-5',
    system='You are helpful.',
    messages=[{'role': 'user', 'content': 'Hello'}],
)
print(count.input_tokens)

Estimate Cost Before Sending

Multiply token count by price per token (from the provider's pricing page):

INPUT_PRICE = 0.150 / 1_000_000   # gpt-4o-mini: $0.15 / 1M input tokens
OUTPUT_PRICE = 0.600 / 1_000_000

input_tokens = count_message_tokens(messages)
est_input_cost = input_tokens * INPUT_PRICE
print(f'Estimated input cost: ${est_input_cost:.6f}')

Track Actual Cost

After each call, sum the usage:

total_cost = 0

response = client.chat.completions.create(...)
cost = (
    response.usage.prompt_tokens * INPUT_PRICE +
    response.usage.completion_tokens * OUTPUT_PRICE
)
total_cost += cost

Per-Run Budgets

Set a hard cap per agent run:

MAX_COST_PER_RUN = 0.50  # USD

if total_cost > MAX_COST_PER_RUN:
    raise BudgetExceeded(f'Cost {total_cost} exceeds limit')

Per-User Daily Budgets

Track cost per user in Redis and reset daily:

key = f'cost:{user_id}:{today_date}'
current = float(redis.get(key) or 0)
if current + cost > 5.0:
    raise QuotaExceeded()
redis.incrbyfloat(key, cost)
redis.expireat(key, midnight_tomorrow_ts)

Cap max_tokens Per Call

The single biggest accidental-cost source is generating a 50,000-token response. Cap it:

response = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=messages,
    max_tokens=2048,  # never more than 2k output
)

Step Cap for Agents

Combine token caps with a step cap (max iterations):

MAX_STEPS = 20
for step in range(MAX_STEPS):
    response = run_step(...)
    if response.is_final():
        break
else:
    raise StepLimitExceeded()

Output Token Cap

Why set max_tokens explicitly?

Recap

Token counting, per-call max_tokens, per-run budgets, per-user quotas, and step caps. Layer all five for safe production.

Frequently asked questions

Is the “Cost Awareness: Token Counting and Budgets” lesson free?

Yes — the full text of “Cost Awareness: Token Counting and Budgets” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.

What will I learn in “Cost Awareness: Token Counting and Budgets”?

Count tokens with tiktoken, estimate cost before you send, and enforce per-request and per-day budgets. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start AI Agents?

No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Cost Awareness: Token Counting and Budgets” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this AI Agents lesson?

Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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