נקודת הקצה count-tokens מאפשרת לכם לקבוע את מספר הטוקנים בהודעה לפני שאתם שולחים אותה ל-Claude, וכך לקבל החלטות מושכלות לגבי ההנחיות והשימוש שלכם.
השימוש בנקודת הקצה count-tokens לא כרוך בתשלום.
המודלים הנתמכים של Claude
המודלים הבאים תומכים בספירת טוקנים:
- Claude Sonnet 5.5 של Anthropic ב-Google Cloud
- Claude Fable 5.2 של Anthropic ב-Google Cloud
- thought Claude Fable 5.1 של Anthropic ב-Google Cloud
- Anthropic Claude Sonnet 5 ב-Google Cloud
- Claude Opus 5.5 של Anthropic ב-Google Cloud
- Claude Opus 5 של Anthropic ב-Google Cloud
- Claude Fable 5 של Anthropic ב-Google Cloud
- Claude Opus 4.8 של Anthropic ב-Google Cloud
- Claude Opus 4.7 של Anthropic ב-Google Cloud
- Claude Opus 4.6 של Anthropic ב-Google Cloud
- Claude Sonnet 4.6 של Anthropic ב-Google Cloud
- Claude Opus 4.5 של Anthropic ב-Google Cloud
- Claude Sonnet 4.5 של Anthropic ב-Google Cloud
- Claude Haiku 5.5 של Anthropic ב-Google Cloud
- Claude Haiku 4.5 של Anthropic ב-Google Cloud
- thought Claude 3.5 Haiku ב-Google Cloud
אזורים נתמכים
אפשר לספור טוקנים באזורים הבאים:
globaluseuasia-southeast1
ספירת טוקנים בהודעות בסיסיות
כדי לספור טוקנים, שולחים בקשת rawPredict לנקודת הקצה (endpoint) של count-tokens. גוף הבקשה צריך להכיל את מזהה המודל שאתם רוצים לספור את הטוקנים שלו.
REST
לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:
- LOCATION: אזור שתומך במודלים של Anthropic Claude. כדי להשתמש בנקודת הקצה הגלובלית, קראו את המאמר ציון נקודת הקצה הגלובלית.
- MODEL: המודל שבו רוצים לספור טוקנים.
- ROLE: התפקיד שמשויך להודעה. אפשר לציין
userאוassistant. ההודעה הראשונה חייבת להשתמש בתפקידuser. המודלים של Claude פועלים בתורות מתחלפים שלuserו-assistant. אם ההודעה האחרונה משתמשת בתפקידassistant, תוכן התשובה ממשיך מיד מהתוכן של ההודעה הזו. אפשר להשתמש בזה כדי להגביל חלק מהתשובה של המודל. - CONTENT: התוכן, כמו טקסט, של ההודעה ב-
userאו ב-assistant.
ה-method של ה-HTTP וכתובת ה-URL:
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/anthropic/models/count-tokens:rawPredict
גוף בקשת JSON:
{
"model": "MODEL",
"messages": [
{
"role": "user",
"content":"how many tokens are in this request?"
}
],
}
כדי לשלוח את הבקשה עליכם לבחור אחת מהאפשרויות הבאות:
curl
שומרים את גוף הבקשה בקובץ בשם request.json ומריצים את הפקודה הבאה:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/anthropic/models/count-tokens:rawPredict"
PowerShell
שומרים את גוף הבקשה בקובץ בשם request.json ומריצים את הפקודה הבאה:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/anthropic/models/count-tokens:rawPredict" | Select-Object -Expand Content
אתם אמורים לקבל תגובת JSON שדומה לזו:
במסמכי התיעוד של Anthropic מוסבר איך סופרים טוקנים בהודעות עם כלים, תמונות וקובצי PDF.
מכסות
כברירת מחדל, המכסה של נקודת הקצה count-tokens היא 2,000 בקשות לדקה.