메시지의 토큰 수 계산
POST/v1/messages/count_tokens
메시지의 토큰 수를 계산합니다.
Token Count API를 사용하면 메시지를 생성하지 않고도 도구, 이미지, 문서를 포함한 메시지의 토큰 수를 계산할 수 있습니다.
토큰 계산에 대한 자세한 내용은 사용자 가이드를 참조하세요.
Parameters
Returns
메시지의 토큰 수 계산
<?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
}