Advertisement
Guest User

claudeFees.py

a guest
Sep 16th, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. import json
  2. import tiktoken
  3. import os
  4.  
  5. # Pricing model (as of September 2024)
  6. PRICE_PER_1K_INPUT_TOKENS = 0.003
  7. PRICE_PER_1K_OUTPUT_TOKENS = 0.015
  8. PRICE_PER_API_CALL = 0.0018 # $0.0018 per API call
  9.  
  10. def num_tokens_from_string(string: str, encoding_name: str) -> int:
  11. """Returns the number of tokens in a text string."""
  12. encoding = tiktoken.get_encoding(encoding_name)
  13. num_tokens = len(encoding.encode(string))
  14. return num_tokens
  15.  
  16. def calculate_cost(input_tokens, output_tokens, num_calls):
  17. """Calculate the cost based on the number of input and output tokens, and number of API calls."""
  18. input_cost = (input_tokens / 1000) * PRICE_PER_1K_INPUT_TOKENS
  19. output_cost = (output_tokens / 1000) * PRICE_PER_1K_OUTPUT_TOKENS
  20. call_cost = num_calls * PRICE_PER_API_CALL
  21. return input_cost + output_cost + call_cost
  22.  
  23. def process_chat(chat):
  24. total_input_tokens = 0
  25. total_output_tokens = 0
  26. num_calls = 0
  27.  
  28. for message in chat['chat_messages']:
  29. if message['sender'] == 'human':
  30. total_input_tokens += num_tokens_from_string(message['text'], "cl100k_base")
  31. num_calls += 1 # Each human message typically triggers an API call
  32. elif message['sender'] == 'assistant':
  33. total_output_tokens += num_tokens_from_string(message['text'], "cl100k_base")
  34.  
  35. return total_input_tokens, total_output_tokens, num_calls
  36.  
  37. def main(file_path):
  38. with open(file_path, 'r') as file:
  39. data = json.load(file)
  40.  
  41. total_input_tokens = 0
  42. total_output_tokens = 0
  43. total_api_calls = 0
  44.  
  45. for chat in data:
  46. input_tokens, output_tokens, num_calls = process_chat(chat)
  47. total_input_tokens += input_tokens
  48. total_output_tokens += output_tokens
  49. total_api_calls += num_calls
  50.  
  51. total_cost = calculate_cost(total_input_tokens, total_output_tokens, total_api_calls)
  52.  
  53. print(f"Total input tokens: {total_input_tokens}")
  54. print(f"Total output tokens: {total_output_tokens}")
  55. print(f"Total API calls: {total_api_calls}")
  56. print(f"Estimated cost:")
  57. print(f" Token cost: ${(total_cost - (total_api_calls * PRICE_PER_API_CALL)):.2f}")
  58. print(f" API call cost: ${(total_api_calls * PRICE_PER_API_CALL):.2f}")
  59. print(f" Total cost: ${total_cost:.2f}")
  60.  
  61. if __name__ == "__main__":
  62. file_path = "E:\dev\claude-fees\conversations.json" # Replace with your file path
  63. main(file_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement