Charlie Parker
Charlie Parker

Reputation: 5169

Best way to count tokens for Anthropic Claude Models using the API?

Summary

How can I count the number of tokens before sending it to Anthropic?


Question content

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:

Approach 1: Using anthropic_bedrock Client

from anthropic_bedrock import AnthropicBedrock

client = AnthropicBedrock()
prompt = "Hello, world!"
token_count = client.count_tokens(prompt)
print(token_count)

Approach 2: Using anthropic Client

import anthropic

client = anthropic.Client()
token_count = client.count_tokens("Sample text")
print(token_count)

My Questions:

  1. Which client (anthropic_bedrock or anthropic) is better for counting tokens in prompts for Claude models?
  2. Are there any significant differences in how these clients handle token counting or any other functionalities that might influence the choice?
  3. Are there best practices I should follow when counting tokens and managing token usage in my applications?

Steps to Reproduce:

  1. Install the appropriate client (anthropic_bedrock or anthropic) using pip.
  2. Authenticate the client with your AWS credentials.
  3. Use the count_tokens method to count tokens in a given prompt.
  4. Print or log the token count for analysis.

References:


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

Answers (4)

Kyle
Kyle

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

metasea
metasea

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

szeitlin
szeitlin

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

adr
adr

Reputation: 205

From Anthropic's Python SDK:

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

Related Questions