token-counting changedbuild-with-claude/token-counting
Nearest release: v2.1.309, published 3 hours before this site recorded the change. Shown because the two are within 24 hours of each other. Nothing here says the release caused the edit.
Recorded here
Lines+145added
Lines−137removed
From line
11
where the diff opens
First seen
14 Aug 2026
this site's first read of the page
Recorded edits19to this page, all time
The whole hunk
from line 11, old and new numbered
/
from line 11
1111 Claude API: ga
1212 Claude Platform on AWS: ga
1313 Amazon Bedrock: ga
14 Google Cloud: ga
14 Claude Models on Google Cloud (Google managed): ga
1515 Microsoft Foundry: ga
1616---
1717
from line 142
142142
143143 public class CountTokensExample {
144144
145 public static void main(String[] args) {
146 AnthropicClient client = AnthropicOkHttpClient.fromEnv();
145 public static void main(String[] args) {
146 AnthropicClient client = AnthropicOkHttpClient.fromEnv();
147147
148 MessageCountTokensParams params = MessageCountTokensParams.builder()
149 .model(Model.CLAUDE_OPUS_5_5)
150 .system("You are a scientist")
151 .addUserMessage("Hello, Claude")
152 .build();
148 MessageCountTokensParams params = MessageCountTokensParams.builder()
149 .model(Model.CLAUDE_OPUS_5_5)
150 .system("You are a scientist")
151 .addUserMessage("Hello, Claude")
152 .build();
153153
154 MessageTokensCount count = client.messages().countTokens(params);
155 System.out.println(count);
156 }
154 MessageTokensCount count = client.messages().countTokens(params);
155 System.out.println(count);
156 }
157157 }
158158 ```
159159
from line 164
164164 messages: [
165165 ['role' => 'user', 'content' => 'Hello, Claude']
166166 ],
167 model: 'claude-opus-5-5',
167 model: Model::CLAUDE_OPUS_5_5,
168168 system: 'You are a scientist',
169169 );
170170
from line 375
375375 import com.anthropic.models.messages.MessageCountTokensParams;
376376 import com.anthropic.models.messages.MessageTokensCount;
377377 // ...
378 AnthropicClient client = AnthropicOkHttpClient.fromEnv();
378 AnthropicClient client = AnthropicOkHttpClient.fromEnv();
379379
380 InputSchema schema = InputSchema.builder()
381 .properties(
382 JsonValue.from(
383 Map.of(
384 "location",
385 Map.of(
386 "type",
387 "string",
388 "description",
389 "The city and state, e.g. San Francisco, CA"
380 InputSchema schema = InputSchema.builder()
381 .properties(
382 JsonValue.from(
383 Map.of(
384 "location",
385 Map.of(
386 "type",
387 "string",
388 "description",
389 "The city and state, e.g. San Francisco, CA"
390 )
391 )
392 )
390393 )
391 )
392 )
393 )
394 .putAdditionalProperty("required", JsonValue.from(List.of("location")))
395 .build();
394 .putAdditionalProperty("required", JsonValue.from(List.of("location")))
395 .build();
396396
397 MessageCountTokensParams params = MessageCountTokensParams.builder()
398 .model(Model.CLAUDE_OPUS_5_5)
399 .addTool(
400 Tool.builder()
401 .name("get_weather")
402 .description("Get the current weather in a given location")
403 .inputSchema(schema)
404 .build()
405 )
406 .addUserMessage("What's the weather like in San Francisco?")
407 .build();
397 MessageCountTokensParams params = MessageCountTokensParams.builder()
398 .model(Model.CLAUDE_OPUS_5_5)
399 .addTool(
400 Tool.builder()
401 .name("get_weather")
402 .description("Get the current weather in a given location")
403 .inputSchema(schema)
404 .build()
405 )
406 .addUserMessage("What's the weather like in San Francisco?")
407 .build();
408408
409 MessageTokensCount count = client.messages().countTokens(params);
410 System.out.println(count);
409 MessageTokensCount count = client.messages().countTokens(params);
410 System.out.println(count);
411411 ```
412412
413413 ```php PHP
from line 417
417417 messages: [
418418 ['role' => 'user', 'content' => "What's the weather like in San Francisco?"]
419419 ],
420 model: 'claude-opus-5-5',
420 model: Model::CLAUDE_OPUS_5_5,
421421 tools: [
422422 [
423423 'name' => 'get_weather',
from line 673
673673 import com.anthropic.models.messages.MessageCountTokensParams;
674674 import com.anthropic.models.messages.MessageTokensCount;
675675 // ...
676 AnthropicClient client = AnthropicOkHttpClient.fromEnv();
676 AnthropicClient client = AnthropicOkHttpClient.fromEnv();
677677
678 String imageUrl =
679 "https://platform.claude.com/docs/images/vision-example.jpg";
680 String imageMediaType = "image/jpeg";
678 String imageUrl =
679 "https://platform.claude.com/docs/images/vision-example.jpg";
680 String imageMediaType = "image/jpeg";
681681
682 HttpClient httpClient = HttpClient.newHttpClient();
683 HttpRequest request = HttpRequest.newBuilder().uri(URI.create(imageUrl)).build();
684 byte[] imageBytes = httpClient
685 .send(request, HttpResponse.BodyHandlers.ofByteArray())
686 .body();
687 String imageBase64 = Base64.getEncoder().encodeToString(imageBytes);
682 HttpClient httpClient = HttpClient.newHttpClient();
683 HttpRequest request = HttpRequest.newBuilder()
684 .uri(URI.create(imageUrl))
685 .build();
686 byte[] imageBytes = httpClient
687 .send(request, HttpResponse.BodyHandlers.ofByteArray())
688 .body();
689 String imageBase64 = Base64.getEncoder().encodeToString(imageBytes);
688690
689 ContentBlockParam imageBlock = ContentBlockParam.ofImage(
690 ImageBlockParam.builder()
691 .source(
692 Base64ImageSource.builder()
693 .mediaType(Base64ImageSource.MediaType.IMAGE_JPEG)
694 .data(imageBase64)
695 .build()
696 )
697 .build()
698 );
691 ContentBlockParam imageBlock = ContentBlockParam.ofImage(
692 ImageBlockParam.builder()
693 .source(
694 Base64ImageSource.builder()
695 .mediaType(Base64ImageSource.MediaType.IMAGE_JPEG)
696 .data(imageBase64)
697 .build()
698 )
699 .build()
700 );
699701
700 ContentBlockParam textBlock = ContentBlockParam.ofText(
701 TextBlockParam.builder().text("Describe this image").build()
702 );
702 ContentBlockParam textBlock = ContentBlockParam.ofText(
703 TextBlockParam.builder()
704 .text("Describe this image")
705 .build()
706 );
703707
704 MessageCountTokensParams params = MessageCountTokensParams.builder()
705 .model(Model.CLAUDE_OPUS_5_5)
706 .addUserMessageOfContentBlockParams(List.of(imageBlock, textBlock))
707 .build();
708 MessageCountTokensParams params = MessageCountTokensParams.builder()
709 .model(Model.CLAUDE_OPUS_5_5)
710 .addUserMessageOfContentBlockParams(List.of(imageBlock, textBlock))
711 .build();
708712
709 MessageTokensCount count = client.messages().countTokens(params);
710 System.out.println(count);
713 MessageTokensCount count = client.messages().countTokens(params);
714 System.out.println(count);
711715 ```
712716
713717 ```php PHP
714 $imageUrl = "https://platform.claude.com/docs/images/vision-example.jpg";
715 $imageMediaType = "image/jpeg";
718 $imageUrl = 'https://platform.claude.com/docs/images/vision-example.jpg';
719 $imageMediaType = 'image/jpeg';
716720 $imageData = base64_encode(file_get_contents($imageUrl));
717721
718722 $client = new Client();
from line 998
994998 import com.anthropic.models.messages.ThinkingBlockParam;
995999 import com.anthropic.models.messages.ThinkingConfigAdaptive;
9961000 // ...
997 AnthropicClient client = AnthropicOkHttpClient.fromEnv();
1001 AnthropicClient client = AnthropicOkHttpClient.fromEnv();
9981002
999 List<ContentBlockParam> assistantBlocks = List.of(
1000 ContentBlockParam.ofThinking(
1001 ThinkingBlockParam.builder()
1002 .thinking(
1003 "This is a nice number theory question. Let's think about it step by step..."
1004 )
1005 .signature(
1006 "EuYBCkQYAiJAgCs1le6/Pol5Z4/JMomVOouGrWdhYNsH3ukzUECbB6iWrSQtsQuRHJID6lWV..."
1007 )
1008 .build()
1009 ),
1010 ContentBlockParam.ofText(
1011 TextBlockParam.builder()
1012 .text("Yes, there are infinitely many prime numbers p such that p mod 4 = 3...")
1013 .build()
1014 )
1015 );
1003 List<ContentBlockParam> assistantBlocks = List.of(
1004 ContentBlockParam.ofThinking(
1005 ThinkingBlockParam.builder()
1006 .thinking(
1007 "This is a nice number theory question. Let's think about it step by step..."
1008 )
1009 .signature(
1010 "EuYBCkQYAiJAgCs1le6/Pol5Z4/JMomVOouGrWdhYNsH3ukzUECbB6iWrSQtsQuRHJID6lWV..."
1011 )
1012 .build()
1013 ),
1014 ContentBlockParam.ofText(
1015 TextBlockParam.builder()
1016 .text("Yes, there are infinitely many prime numbers p such that p mod 4 = 3...")
1017 .build()
1018 )
1019 );
10161020
1017 MessageCountTokensParams params = MessageCountTokensParams.builder()
1018 .model(Model.CLAUDE_OPUS_5_5)
1019 .thinking(ThinkingConfigAdaptive.builder().build())
1020 .addUserMessage("Are there an infinite number of prime numbers such that n mod 4 == 3?")
1021 .addAssistantMessageOfContentBlockParams(assistantBlocks)
1022 .addUserMessage("Can you write a formal proof?")
1023 .build();
1021 MessageCountTokensParams params = MessageCountTokensParams.builder()
1022 .model(Model.CLAUDE_OPUS_5_5)
1023 .thinking(ThinkingConfigAdaptive.builder().build())
1024 .addUserMessage("Are there an infinite number of prime numbers such that n mod 4 == 3?")
1025 .addAssistantMessageOfContentBlockParams(assistantBlocks)
1026 .addUserMessage("Can you write a formal proof?")
1027 .build();
10241028
1025 MessageTokensCount count = client.messages().countTokens(params);
1026 System.out.println(count);
1029 MessageTokensCount count = client.messages().countTokens(params);
1030 System.out.println(count);
10271031 ```
10281032
10291033 ```php PHP
from line 1296
12921296 import com.anthropic.models.messages.MessageCountTokensParams;
12931297 import com.anthropic.models.messages.MessageTokensCount;
12941298 // ...
1295 AnthropicClient client = AnthropicOkHttpClient.fromEnv();
1299 AnthropicClient client = AnthropicOkHttpClient.fromEnv();
12961300
1297 byte[] fileBytes = Files.readAllBytes(Path.of("/path/to/document.pdf"));
1298 String pdfBase64 = Base64.getEncoder().encodeToString(fileBytes);
1301 byte[] fileBytes = Files.readAllBytes(Path.of("/path/to/document.pdf"));
1302 String pdfBase64 = Base64.getEncoder().encodeToString(fileBytes);
12991303
1300 ContentBlockParam documentBlock = ContentBlockParam.ofDocument(
1301 DocumentBlockParam.builder()
1302 .source(Base64PdfSource.builder().data(pdfBase64).build())
1303 .build()
1304 );
1304 ContentBlockParam documentBlock = ContentBlockParam.ofDocument(
1305 DocumentBlockParam.builder()
1306 .source(Base64PdfSource.builder()
1307 .data(pdfBase64)
1308 .build())
1309 .build()
1310 );
13051311
1306 ContentBlockParam textBlock = ContentBlockParam.ofText(
1307 TextBlockParam.builder().text("Please summarize this document.").build()
1308 );
1312 ContentBlockParam textBlock = ContentBlockParam.ofText(
1313 TextBlockParam.builder()
1314 .text("Please summarize this document.")
1315 .build()
1316 );
13091317
1310 MessageCountTokensParams params = MessageCountTokensParams.builder()
1311 .model(Model.CLAUDE_OPUS_5_5)
1312 .addUserMessageOfContentBlockParams(List.of(documentBlock, textBlock))
1313 .build();
1318 MessageCountTokensParams params = MessageCountTokensParams.builder()
1319 .model(Model.CLAUDE_OPUS_5_5)
1320 .addUserMessageOfContentBlockParams(List.of(documentBlock, textBlock))
1321 .build();
13141322
1315 MessageTokensCount count = client.messages().countTokens(params);
1316 System.out.println(count);
1323 MessageTokensCount count = client.messages().countTokens(params);
1324 System.out.println(count);
13171325 ```
13181326
13191327 ```php PHP
13201328 $client = new Client();
13211329
1322 $pdfBase64 = base64_encode(file_get_contents("/path/to/document.pdf"));
1330 $pdfBase64 = base64_encode(file_get_contents('/path/to/document.pdf'));
13231331
13241332 $response = $client->messages->countTokens(
13251333 messages: [
No line in this hunk matches that.