Skip to content

Commit 1e97c4d

Browse files
committed
add exception handling and api key loading
1 parent 090177a commit 1e97c4d

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

tokencost/costs.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Costs dictionary and utility tool for counting tokens
44
"""
55

6+
import os
67
import tiktoken
78
import anthropic
89
from typing import Union, List, Dict
@@ -40,12 +41,11 @@ def count_message_tokens(messages: List[Dict[str, str]], model: str) -> int:
4041
model = model.lower()
4142
model = strip_ft_model_name(model)
4243

44+
# Anthropic token counting requires a valid API key
4345
if "claude-" in model:
4446
logger.warning(
4547
"Warning: Anthropic token counting API is currently in beta. Please expect differences in costs!"
4648
)
47-
client = anthropic.Client()
48-
4949
if "claude-3-sonnet" in model:
5050
logger.warning(
5151
f"Token counting (beta) is not supported for {model}. Returning num tokens using count from the string."
@@ -54,14 +54,19 @@ def count_message_tokens(messages: List[Dict[str, str]], model: str) -> int:
5454
prompt = "".join(message["content"] for message in messages)
5555
return count_string_tokens(prompt, model)
5656

57+
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
58+
5759
try:
60+
client = anthropic.Client(api_key=ANTHROPIC_API_KEY)
5861
num_tokens = client.beta.messages.count_tokens(
5962
model=model,
6063
messages=messages,
6164
).input_tokens
6265
return num_tokens
66+
except TypeError as e:
67+
raise e
6368
except Exception as e:
64-
raise Exception(f"An error occured - {e}") from e
69+
raise e
6570

6671
try:
6772
encoding = tiktoken.encoding_for_model(model)

0 commit comments

Comments
 (0)