/v1/messages/count_tokens
概覽
Anthropic 相容的 token 計數端點。在將訊息送入模型之前先計算其 token 數。
| 功能 | 支援 | 備註 |
|---|---|---|
| 成本追蹤 | ❌ | 僅進行 token 計數,不產生成本 |
| 記錄 | ✅ | 可跨所有整合使用 |
| 終端使用者追蹤 | ✅ | |
| 支援的提供者 | Anthropic、Vertex AI(Claude)、Bedrock(Claude)、Gemini、Vertex AI | 會自動路由至提供者專屬的 token 計數 API |
快速入門
1. 啟動 LiteLLM Proxy
litellm --config /path/to/config.yaml
# RUNNING on http://0.0.0.0:4000
2. 計算 Token
- curl
- Python (httpx)
curl -X POST "http://localhost:4000/v1/messages/count_tokens" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"model": "claude-3-5-sonnet-20241022",
"messages": [
{"role": "user", "content": "Hello, how are you?"}
]
}'
import httpx
response = httpx.post(
"http://localhost:4000/v1/messages/count_tokens",
headers={
"Content-Type": "application/json",
"Authorization": "Bearer sk-1234"
},
json={
"model": "claude-3-5-sonnet-20241022",
"messages": [
{"role": "user", "content": "Hello, how are you?"}
]
}
)
print(response.json())
# {"input_tokens": 14}
預期回應:
{
"input_tokens": 14
}
LiteLLM Proxy 設定
將模型新增至您的 config.yaml:
model_list:
- model_name: claude-3-5-sonnet
litellm_params:
model: anthropic/claude-3-5-sonnet-20241022
api_key: os.environ/ANTHROPIC_API_KEY
- model_name: claude-vertex
litellm_params:
model: vertex_ai/claude-3-5-sonnet-v2@20241022
vertex_project: my-project
vertex_location: us-east5
vertex_count_tokens_location: us-east5 # Optional: Override location for token counting (count_tokens not available on global location)
- model_name: claude-bedrock
litellm_params:
model: bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0
aws_region_name: us-west-2
請求參數
| 參數 | 類型 | 必填 | 說明 |
|---|---|---|---|
model | string | ✅ | 用於 token 計數的模型 |
messages | array | ✅ | Anthropic 格式的訊息陣列 |
訊息格式
{
"messages": [
{"role": "user", "content": "Hello!"},
{"role": "assistant", "content": "Hi there!"},
{"role": "user", "content": "How are you?"}
]
}
回應格式
{
"input_tokens": <number>
}
| 欄位 | 類型 | 說明 |
|---|---|---|
input_tokens | integer | 輸入訊息中的 token 數量 |
支援的提供者
/v1/messages/count_tokens 端點會自動路由至適當的提供者專屬 token 計數 API:
| 提供者 | Token 計數方法 |
|---|---|
| Anthropic | Anthropic Token Counting API |
| OpenAI | OpenAI Responses API /input_tokens — 參閱 Token Counting |
| Vertex AI(Claude) | Vertex AI Partner Models Token Counter |
| Bedrock(Claude) | AWS Bedrock CountTokens API |
| Gemini | Google AI Studio countTokens API |
| Vertex AI(Gemini) | Vertex AI countTokens API |
範例
透過系統訊息計算 Token
curl -X POST "http://localhost:4000/v1/messages/count_tokens" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"model": "claude-3-5-sonnet-20241022",
"messages": [
{"role": "user", "content": "You are a helpful assistant. Please help me write a haiku about programming."}
]
}'
計算多輪對話的 Token
curl -X POST "http://localhost:4000/v1/messages/count_tokens" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"model": "claude-3-5-sonnet-20241022",
"messages": [
{"role": "user", "content": "What is the capital of France?"},
{"role": "assistant", "content": "The capital of France is Paris."},
{"role": "user", "content": "What is its population?"}
]
}'
搭配 Vertex AI Claude 使用
curl -X POST "http://localhost:4000/v1/messages/count_tokens" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"model": "claude-vertex",
"messages": [
{"role": "user", "content": "Hello, world!"}
]
}'
搭配 Bedrock Claude 使用
curl -X POST "http://localhost:4000/v1/messages/count_tokens" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-1234" \
-d '{
"model": "claude-bedrock",
"messages": [
{"role": "user", "content": "Hello, world!"}
]
}'
與 Anthropic 轉送相比
LiteLLM 提供兩種計算 token 的方式:
| 端點 | 說明 | 使用情境 |
|---|---|---|
/v1/messages/count_tokens | LiteLLM 的 Anthropic 相容端點 | 可與所有支援的提供者(Anthropic、Vertex AI、Bedrock 等)搭配使用 |
/anthropic/v1/messages/count_tokens | 轉送至 Anthropic API | 直接存取 Anthropic API,使用原生標頭 |
轉送範例
若要直接存取 Anthropic API 並保留完整原生標頭:
curl --request POST \
--url http://0.0.0.0:4000/anthropic/v1/messages/count_tokens \
--header "x-api-key: $LITELLM_API_KEY" \
--header "anthropic-version: 2023-06-01" \
--header "anthropic-beta: token-counting-2024-11-01" \
--header "content-type: application/json" \
--data '{
"model": "claude-3-5-sonnet-20241022",
"messages": [
{"role": "user", "content": "Hello, world"}
]
}'