Was this page helpful?
Penghitungan token memungkinkan Anda menentukan jumlah token dalam pesan sebelum mengirimnya ke Claude, membantu Anda membuat keputusan berdasarkan informasi tentang prompt dan penggunaan Anda. Dengan penghitungan token, Anda dapat
This feature is eligible for Zero Data Retention (ZDR). When your organization has a ZDR arrangement, data sent through this feature is not stored after the API response is returned.
Titik akhir penghitungan token menerima daftar input terstruktur yang sama untuk membuat pesan, termasuk dukungan untuk prompt sistem, alat, gambar, dan PDF. Respons berisi jumlah total token input.
Penghitungan token harus dianggap sebagai perkiraan. Dalam beberapa kasus, jumlah sebenarnya dari token input yang digunakan saat membuat pesan mungkin berbeda dalam jumlah kecil.
Penghitungan token mungkin mencakup token yang ditambahkan secara otomatis oleh Anthropic untuk optimasi sistem. Anda tidak ditagih untuk token yang ditambahkan sistem. Penagihan hanya mencerminkan konten Anda.
Semua model aktif mendukung penghitungan token.
{ "input_tokens": 14 }Penghitungan token alat server hanya berlaku untuk panggilan sampling pertama.
{ "input_tokens": 403 }{ "input_tokens": 1551 }Lihat bagaimana jendela konteks dihitung dengan pemikiran yang diperluas untuk detail lebih lanjut
{ "input_tokens": 88 }Penghitungan token mendukung PDF dengan batasan yang sama seperti Messages API.
{ "input_tokens": 2188 }Penghitungan token gratis digunakan tetapi tunduk pada batas laju permintaan per menit berdasarkan tingkat penggunaan Anda. Jika Anda memerlukan batas yang lebih tinggi, hubungi penjualan melalui Claude Console.
| Tingkat penggunaan | Permintaan per menit (RPM) |
|---|---|
| 1 | 100 |
| 2 | 2.000 |
| 3 | 4.000 |
| 4 | 8.000 |
Penghitungan token dan pembuatan pesan memiliki batas laju yang terpisah dan independen. Penggunaan salah satu tidak dihitung terhadap batas yang lain.
client = anthropic.Anthropic()
response = client.messages.count_tokens(
model="claude-opus-4-7",
system="You are a scientist",
messages=[{"role": "user", "content": "Hello, Claude"}],
)
print(response.json())client = anthropic.Anthropic()
response = client.messages.count_tokens(
model="claude-opus-4-7",
tools=[
{
"name": "get_weather",
"description": "Get the current weather in a given location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
}
},
"required": ["location"],
},
}
],
messages=[{"role": "user", "content": "What's the weather like in San Francisco?"}],
)
print(response.json())import base64
import httpx
image_url = "https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"
image_media_type = "image/jpeg"
image_data = base64.standard_b64encode(httpx.get(image_url).content).decode("utf-8")
client = anthropic.Anthropic()
response = client.messages.count_tokens(
model="claude-opus-4-7",
messages=[
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": image_media_type,
"data": image_data,
},
},
{"type": "text", "text": "Describe this image"},
],
}
],
)
print(response.json())client = anthropic.Anthropic()
response = client.messages.count_tokens(
model="claude-sonnet-4-6",
thinking={"type": "enabled", "budget_tokens": 16000},
messages=[
{
"role": "user",
"content": "Are there an infinite number of prime numbers such that n mod 4 == 3?",
},
{
"role": "assistant",
"content": [
{
"type": "thinking",
"thinking": "This is a nice number theory question. Let's think about it step by step...",
"signature": "EuYBCkQYAiJAgCs1le6/Pol5Z4/JMomVOouGrWdhYNsH3ukzUECbB6iWrSQtsQuRHJID6lWV...",
},
{
"type": "text",
"text": "Yes, there are infinitely many prime numbers p such that p mod 4 = 3...",
},
],
},
{"role": "user", "content": "Can you write a formal proof?"},
],
)
print(response.json())import base64
import anthropic
client = anthropic.Anthropic()
with open("document.pdf", "rb") as pdf_file:
pdf_base64 = base64.standard_b64encode(pdf_file.read()).decode("utf-8")
response = client.messages.count_tokens(
model="claude-opus-4-7",
messages=[
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": pdf_base64,
},
},
{"type": "text", "text": "Please summarize this document."},
],
}
],
)
print(response.json())