import anthropicclient = anthropic.Anthropic()response = client.messages.count_tokens( model="claude-opus-4-20250514", 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())
확장 사고를 사용할 때 컨텍스트 윈도우가 계산되는 방법에 대한 자세한 내용은 여기를 참조하세요.
이전 어시스턴트 턴의 사고 블록은 무시되며 입력 토큰에 포함되지 않습니다
현재 어시스턴트 턴의 사고는 입력 토큰에 포함됩니다
Copy
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-opus-4-20250514", "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?" } ] }'