Python
Count Tokens

Count Tokens

The tokens function allows you to count the number of tokens your prompt uses.

anthropic.tokens("Hello World!")

Usage

from bedrock_anthropic import AnthropicBedrock
 
anthropic = AnthropicBedrock(
    region="us-east-1"
)
 
print(anthropic.count_tokens("Hello World!")) # 3

Configuration

text

The text you want to count the tokens for.

anthropic = AnthropicBedrock()
 
anthropic.count_tokens("Hello world!")