Reputation: 5169
How can I count the number of tokens before sending it to Anthropic?
I'm working with Anthropic's Claude models and need to accurately count the number of tokens in my prompts and responses. I'm using the anthropic_bedrock
Python client but recently came across an alternative method using the anthropic
client. I'm looking for advice on which approach is better and the proper way to implement token counting.
Here are the two approaches I've found:
anthropic_bedrock
Clientfrom anthropic_bedrock import AnthropicBedrock
client = AnthropicBedrock()
prompt = "Hello, world!"
token_count = client.count_tokens(prompt)
print(token_count)
anthropic
Clientimport anthropic
client = anthropic.Client()
token_count = client.count_tokens("Sample text")
print(token_count)
anthropic_bedrock
or anthropic
) is better for counting tokens in prompts for Claude models?anthropic_bedrock
or anthropic
) using pip
.count_tokens
method to count tokens in a given prompt.They give different outputs!
>>> client = AnthropicBedrock()
prompt = "Hello, world!"
token_count = client.count_tokens(prompt)
print(token_count)>>> prompt = "Hello, world!"
>>> token_count = client.count_tokens(prompt)
>>> print(token_count)
4
>>> import anthropic
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'anthropic'
>>>
>>> client = anthropic.Client()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'anthropic' is not defined
>>>
>>> token_count = client.count_tokens("Sample text")
>>> print(token_count)
2
2 vs 4. Which one to use?
Upvotes: 8
Views: 7669
Reputation: 13
You don't want to use either of these approaches any more, as they're only valid for older (2.x) models.
Instead, if you need to get token counts for a message before generating a response, you will want to use the count_tokens endpoint (this link is for the Python SDK but is available in the Typescript one and via HTTP requests).
The other, more accurate option, would be to generate your message, and in the resulting Message object, get the exact number of tokens used for the input and output with message.usage.input_tokens and message.usage.output_tokens.
Upvotes: 1
Reputation: 141
It looks like anthropic-bedrock
(https://github.com/anthropics/anthropic-bedrock-python/tree/main) is no longer an actively maintained repo. It has been merged into the main anthropic
repo at (
https://github.com/anthropics/anthropic-sdk-python/tree/main)
Upvotes: 1
Reputation: 3351
I'd recommend using the client for whichever provider you're using. If there are any differences in versioning (e.g. if Anthropic releases an update and Bedrock doesn't have it yet) that's going to be frustrating.
Upvotes: 0
Reputation: 205
def count_tokens(
self,
text: str,
) -> int:
"""Count the number of tokens in a given string.
Note that this is only accurate for older models, e.g. `claude-2.1`. For newer
models this can only be used as a _very_ rough estimate, instead you should rely
on the `usage` property in the response for exact counts.
"""
# Note: tokenizer is untyped
tokenizer = self.get_tokenizer()
encoded_text = tokenizer.encode(text) # type: ignore
return len(encoded_text.ids) # type: ignore
So I'd say that you should expect better results from anthropic_bedrock
, but if you are able to find the code to compare to this one, it would be best.
In other places, I've seen the suggestion to use the ChatGPT-4 token counter to have another estimate of the number of tokens, you could try it too.
Upvotes: 1