토큰 카운팅 엔드포인트는 시스템 프롬프트, 도구, 이미지, PDF 지원을 포함하여 메시지 생성을 위한 동일한 구조화된 입력 목록을 허용합니다. 응답에는 총 입력 토큰 수가 포함됩니다.
토큰 수는 추정치로 간주되어야 합니다. 경우에 따라 메시지를 생성할 때 사용되는 실제 입력 토큰 수는 소량 차이가 날 수 있습니다.토큰 수에는 시스템 최적화를 위해 Anthropic에서 자동으로 추가한 토큰이 포함될 수 있습니다. 시스템에서 추가한 토큰에 대해서는 요금이 청구되지 않습니다. 청구는 귀하의 콘텐츠만 반영합니다.
import anthropicclient = anthropic.Anthropic()response = client.messages.count_tokens( model="claude-sonnet-4-5", 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())
curl https://api.anthropic.com/v1/messages/count_tokens \ --header "x-api-key: $ANTHROPIC_API_KEY" \ --header "content-type: application/json" \ --header "anthropic-version: 2023-06-01" \ --data '{ "model": "claude-sonnet-4-5", "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. Lets 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?" } ] }'