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.
Answer from Kyle on Stack Overflow
» pip install anthropic
Videos
Anthropic released an official Python SDK for Claude Code
python - Best way to count tokens for Anthropic Claude Models using the API? - Stack Overflow
Ask HN: Python Meta-Client for OpenAI, Anthropic, Gemini and other LLM API-s?
Anthropic's Python SDK (safety-first language model APIs)
Anthropic has officially released a Python SDK for Claude Code, and it’s built specifically with developers in mind. This makes it way easier to bring Claude’s code generation and tool use capabilities into your own Python projects
What it offers:
Tool use support
Streaming output
Async & sync support
File support
Built-in chat structure
GitHub repo: https://github.com/anthropics/claude-code-sdk-python
I'd love to hear your ideas on how you plan to put this to use
» pip install anthropic
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.
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.