diff --git a/pyproject.toml b/pyproject.toml index 6331132..950d18d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ tokencost = ["model_prices.json"] [project] name = "tokencost" -version = "0.1.16" +version = "0.1.17" authors = [ { name = "Trisha Pan", email = "trishaepan@gmail.com" }, { name = "Alex Reibman", email = "areibman@gmail.com" }, diff --git a/tokencost/costs.py b/tokencost/costs.py index efdaffd..abdcd78 100644 --- a/tokencost/costs.py +++ b/tokencost/costs.py @@ -18,18 +18,26 @@ def get_anthropic_token_count(messages: List[Dict[str, str]], model: str) -> int: if not any( - supported_model in model for supported_model in [ - "claude-3-5-sonnet", "claude-3-5-haiku", "claude-3-haiku", "claude-3-opus" + supported_model in model + for supported_model in [ + "claude-3-5-sonnet", + "claude-3-5-haiku", + "claude-3-haiku", + "claude-3-opus", ] ): raise ValueError( f"{model} is not supported in token counting (beta) API. Use the `usage` property in the response for exact counts." ) try: - return anthropic.Anthropic().beta.messages.count_tokens( - model=model, - messages=messages, - ).input_tokens + return ( + anthropic.Anthropic() + .beta.messages.count_tokens( + model=model, + messages=messages, + ) + .input_tokens + ) except TypeError as e: raise e except Exception as e: @@ -286,9 +294,7 @@ def calculate_all_costs_and_tokens( ) if "claude-" in model: - logger.warning( - "Warning: Token counting is estimated for " - ) + logger.warning("Warning: Token counting is estimated for ") completion_list = [{"role": "assistant", "content": completion}] # Anthropic appends some 13 additional tokens to the actual completion tokens completion_tokens = count_message_tokens(completion_list, model) - 13