Hitung token dalam Message
POST/v1/messages/count_tokens
Hitung jumlah token dalam sebuah Message.
Token Count API dapat digunakan untuk menghitung jumlah token dalam sebuah Message, termasuk alat, gambar, dan dokumen, tanpa membuatnya.
Pelajari lebih lanjut tentang penghitungan token di panduan pengguna kami
Parameters
Returns
Hitung token dalam Message
<?php
require_once dirname(__DIR__) . '/vendor/autoload.php';
$client = new Client(apiKey: 'my-anthropic-api-key');
$messageTokensCount = $client->messages->countTokens(
messages: [['content' => 'Hello, world', 'role' => 'user']],
model: Model::CLAUDE_OPUS_5,
cacheControl: ['type' => 'ephemeral', 'ttl' => '5m'],
outputConfig: [
'effort' => 'low',
'format' => ['schema' => ['foo' => 'bar'], 'type' => 'json_schema'],
],
system: [
[
'text' => 'Today\'s date is 2024-06-01.',
'type' => 'text',
'cacheControl' => ['type' => 'ephemeral', 'ttl' => '5m'],
'citations' => [
[
'citedText' => 'The grass is green. The sky is blue.',
'documentIndex' => 0,
'documentTitle' => 'x',
'endCharIndex' => 0,
'startCharIndex' => 0,
'type' => 'char_location',
],
],
],
],
thinking: ['type' => 'adaptive', 'display' => 'summarized'],
toolChoice: ['type' => 'auto', 'disableParallelToolUse' => true],
tools: [
[
'inputSchema' => [
'type' => 'object',
'properties' => ['location' => 'bar', 'unit' => 'bar'],
'required' => ['location'],
],
'name' => 'name',
'allowedCallers' => ['direct'],
'cacheControl' => ['type' => 'ephemeral', 'ttl' => '5m'],
'deferLoading' => true,
'description' => 'Get the current weather in a given location',
'eagerInputStreaming' => true,
'inputExamples' => [['foo' => 'bar']],
'strict' => true,
'type' => 'custom',
],
],
userProfileID: 'anthropic-user-profile-id',
workspaceID: 'wrkspc_011CZkZaBF1tNoB5wlCeusgy',
);
var_dump($messageTokensCount);Response 200
{
"input_tokens": 2095
}Returns Examples
Response 200
{
"input_tokens": 2095
}