Token 計算の詳細
Token とは
Token は大規模言語モデルがテキストを処理する最小の基本単位です。モデルは生のテキストを直接処理せず、テキストを token 列に変換して処理します。
Tokenizer(トークナイザー)
Tokenizer はテキストを token 列に変換する役割を担います:
Input text → Tokenizer → Token sequence
"今天天气很好" → [192, 3847, 2093, 3847, 452, 2398] # Chinese tokenization demo (6 tokens)Detokenizer(逆トークナイザー)
Detokenizer は token 列をテキストに戻す役割を担います:
Token sequence → Detokenizer → Output text
[192, 3847, 2093, 3847, 452, 2398] → "今天天气很好" # Chinese tokenization demo (6 tokens)Token 計算ルール
中国語と英語の違い
| テキスト種別 | 平均 Token 数 | 例 |
|---|---|---|
| 英単語 | 1 Token ≈ 0.75 単語 | "hello" → 1 token |
| 中国語の漢字 | 1 Token ≈ 1-2 文字 | "今天" → 2 tokens |
| 句読点 | 1 個 ≈ 1 Token | "。" → 1 token |
| 空白 | 英語の空白も計上 | "hello world" → 2 tokens |
実際の計算例
English: "hello world"
Tokens: [15339, 1917]
Count: 2 tokens
Chinese: "今天天气很好" # Chinese tokenization demo (6 tokens)
Tokens: [192, 3847, 2093, 3847, 452, 2398]
Count: 6 tokens一般的な Token 計算ツール
プラットフォーム内蔵の計算
天枢AI-TokenHub プラットフォームの API レスポンスでは、実際に消費された token 数が返されます:
json
{
"id": "chatcmpl-xxx",
"model": "dola-seed-2-1-turbo-260628",
"usage": {
"prompt_tokens": 28, // input consumption
"completion_tokens": 45, // output consumption
"total_tokens": 73 // total consumption
}
}よく使う Token 計算式
英語テキスト
Token count ≈ characters ÷ 4
Token count ≈ words × 1.33中国語テキスト
Token count ≈ characters × 1.3 ~ 1.5オンライン計算ツール
| ツール | ウェブサイト | 説明 |
|---|---|---|
| OpenAI Tokenizer | platform.openai.com/tokenizer | 公式ツール |
| AiToken | aitoken.fly.dev | オンライン計算 |
天枢AI-TokenHub プラットフォームの Token 課金
課金の構成
| 種別 | 説明 | 課金 |
|---|---|---|
| Prompt Tokens | 入力テキストの消費 | ✅ 課金 |
| Completion Tokens | 出力テキストの消費 | ✅ 課金 |
| Total Tokens | 総消費 | ✅ 課金 |
課金例
python
# User input
user_input = "Please write a poem about spring"
# Assume it converts to 14 tokens
# Model output
model_output = "Spring breeze brushes the face, all things revive.\nFlowers bloom everywhere, greenery abounds."
# Assume output converts to 28 tokens
# Total: 14 + 28 = 42 tokensプラットフォームの Token 計算ページ
天枢AI-TokenHub バックエンドの「用量統計」ページでは、以下を確認できます:
- 日/週/月ごとの Token 消費明細
- 各モデルの Token 消費割合
- Token 消費トレンドグラフ
Token を節約する方法
1. Prompt の簡素化
python
# ❌ Not recommended - redundant description
messages = [
{"role": "user", "content": "Please act as a very professional and experienced programmer to help me write a piece of code. The function of this code is to calculate the sum of two numbers. Please write it in detail, including comments and error handling"}
]
# ✅ Recommended - concise and clear
messages = [
{"role": "user", "content": "Write a Python addition function with error handling"}
]2. システムメッセージの使用
固定のロール設定はシステムメッセージに配置します:
python
messages = [
{"role": "system", "content": "You are a professional Python programmer"},
{"role": "user", "content": "Write a sorting algorithm"}
]3. 返信の長さの制御
返信の形式と長さを明確に指定します:
python
{"role": "user", "content": "Explain quantum computing in one sentence"}4. マルチターン対話の最適化
python
# ❌ Repeating context every time
messages = [
{"role": "user", "content": "Explain what machine learning is, including its definition, principles, and application scenarios"},
{"role": "assistant", "content": "Machine learning is..."},
{"role": "user", "content": "Please explain the definition, principles, and application scenarios of machine learning in more detail"}
]
# ✅ Leverage historical context
messages = [
{"role": "user", "content": "Explain what machine learning is, including its definition, principles, and application scenarios"},
{"role": "assistant", "content": "Machine learning is..."},
{"role": "user", "content": "Give a concrete example"} # Automatically understands context
]Token とコンテキストウィンドウ
Token 数がコンテキストに与える影響
Context window: 4096 tokens
Input: 3000 tokens
Available output: 4096 - 3000 = 1096 tokens
Input: 4000 tokens
Available output: 4096 - 4000 = 96 tokens ⚠️ Almost no room for outputプラットフォームのコンテキストウィンドウ
| モデル | コンテキストウィンドウ | 説明 |
|---|---|---|
| dola-seed-2-1-turbo-260628 | 256K Tokens | 256,000 |
| deepseek-v4-1-flash-260910 | 1024K Tokens | 1,024,000 |
| glm-5-2-260617 | 1024K Tokens | 1,024,000 |
よくある質問
Q: 入力が短いのに Token が多いのはなぜですか?
考えられる原因:
- 特殊文字やコードが含まれている
- 中国語の句読点が多い
- URL やメールアドレスが含まれている
Q: Token 計算に誤差はありますか?
モデルごとに異なる Tokenizer を使用するため、計算結果に多少の差異が生じる可能性があります。API レスポンスで返される usage フィールドを基準とすることを推奨します。
Q: Token を正確に計算するには?
最も正確な方法は API レスポンスから取得することです:
python
response = client.chat.completions.create(
model="dola-seed-2-1-turbo-260628",
messages=[{"role": "user", "content": "Hello"}]
)
# Get the exact count from the response
print(f"Total Tokens: {response.usage.total_tokens}")