- Newest
- Most votes
- Most comments
No, Claude 3.7 Sonnet’s count_tokens feature from the Anthropic Python API is not available in the Bedrock Runtime API. Bedrock does not provide a pre-inference token counting endpoint like count_tokens. Instead, you can get token usage from the response after making an inference call, or estimate tokens by dividing character count by 6.
How to Estimate Tokens Before API Call
You can estimate tokens by dividing the character count by 6, as recommended in the AWS documentation:
prompt = "Your input string here" estimated_tokens = len(prompt) // 6 print(f"Estimated tokens: {estimated_tokens}")
This is a rough estimate and may not be exact for all prompts or models.
How to Get Token Counts After an API Call (with boto3)
When you invoke a Claude model through Bedrock, the response includes input and output token counts in the response headers. This is documented in the AWS Bedrock Runtime API Reference:
import boto3 import json RESPONSE_METADATA = "ResponseMetadata" HTTP_HEADERS = "HTTPHeaders" LATENCY = "x-amzn-bedrock-invocation-latency" OUTPUT_TOKENS = "x-amzn-bedrock-output-token-count" INPUT_TOKENS = "x-amzn-bedrock-input-token-count" bedrock_client = boto3.client(service_name="bedrock-runtime") model_id = "us.anthropic.claude-3-7-sonnet-20250219-v1:0" body = { "anthropic_version": "bedrock-2023-05-31", "max_tokens": 1024, "messages": [{"role": "user", "content": "Hello, Claude!"}] } response = bedrock_client.invoke_model( modelId=model_id, body=json.dumps(body), accept="application/json", contentType="application/json" ) # Extract token usage from the response headers headers = response[RESPONSE_METADATA][HTTP_HEADERS] print(f"Input tokens: {headers[INPUT_TOKENS]}") print(f"Output tokens: {headers[OUTPUT_TOKENS]}")
Summary:
- Pre-inference: Estimate tokens by dividing character count by 6 (AWS docs).
- Post-inference: Retrieve exact token counts from the Bedrock API response headers after your call (API Reference).
answered a year ago
Based on the available information, there is no explicit mention of a count_tokens feature being available for Claude 3.7 Sonnet specifically through the Amazon Bedrock Runtime API.
The documentation shows that Claude 3.7 Sonnet is available on Amazon Bedrock and supports various features like tool use (with both auto and none tool choice options), and beta features such as token-efficient tools, computer use, and output tokens up to 128K through the anthropic_beta parameter.
When working with Claude models on Amazon Bedrock, you typically interact with them using either the invoke_model method with a message format or the converse method. The documentation shows examples of these approaches but doesn't specifically mention a count_tokens feature that would be equivalent to what might be available in Anthropic's direct Python API.
If you need to track token usage, the response from Claude models on Bedrock does include a usage field that provides information about the number of input and output tokens used in a request, which could serve a similar purpose for tracking token consumption.
Sources
Tool use - Amazon Bedrock
Request and Response - Amazon Bedrock
Community | Build a Tool Use-Based Agent Loop with Amazon Bedrock
answered a year ago
Relevant content
asked 3 years ago
asked 10 months ago
- AWS OFFICIALUpdated 2 months ago
- AWS OFFICIALUpdated 2 months ago

features supported by Anthropic python API docs