Python 88.3%
TypeScript 7.6%
Shell 4.1%
1#!/usr/bin/env python32"""count_tokens with the Python SDK: plain, +system, +tool, +thinking (free endpoint).34STATUS: LIVE_VERIFIED 2026-09-18 with anthropic 1.7.0 (.venv). Run: .venv/bin/python examples/anthropic/messages/count_tokens.py5Key comes from .env (loaded by scripts/live.py); never printed.6"""7import sys8from pathlib import Path910sys.path.insert(0, str(Path(__file__).resolve().parents[3]))11from scripts import live # noqa: E402 (loads .env, gives log_request)12from anthropic import Anthropic # noqa: E4021314client = Anthropic()15MODEL = "claude-haiku-4-5-20251001"16msgs = [{"role": "user", "content": "Reply with OK."}]17tool = {"name": "get_weather", "description": "Get current weather for a city.",18 "input_schema": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}}19print("plain :", client.messages.count_tokens(model=MODEL, messages=msgs).input_tokens)20print("+system :", client.messages.count_tokens(model=MODEL, messages=msgs, system="You are a helpful assistant.").input_tokens)21print("+tool :", client.messages.count_tokens(model=MODEL, messages=msgs, tools=[tool]).input_tokens)22print("+thinking:", client.messages.count_tokens(model=MODEL, messages=msgs, thinking={"type": "enabled", "budget_tokens": 1024}).input_tokens)23for _ in range(4):24 live.log_request("anthropic", "POST", "/v1/messages/count_tokens", 200, 0, "example count_tokens.py")25