Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import tiktoken
- import os
- # Pricing model (as of September 2024)
- PRICE_PER_1K_INPUT_TOKENS = 0.003
- PRICE_PER_1K_OUTPUT_TOKENS = 0.015
- PRICE_PER_API_CALL = 0.0018 # $0.0018 per API call
- def num_tokens_from_string(string: str, encoding_name: str) -> int:
- """Returns the number of tokens in a text string."""
- encoding = tiktoken.get_encoding(encoding_name)
- num_tokens = len(encoding.encode(string))
- return num_tokens
- def calculate_cost(input_tokens, output_tokens, num_calls):
- """Calculate the cost based on the number of input and output tokens, and number of API calls."""
- input_cost = (input_tokens / 1000) * PRICE_PER_1K_INPUT_TOKENS
- output_cost = (output_tokens / 1000) * PRICE_PER_1K_OUTPUT_TOKENS
- call_cost = num_calls * PRICE_PER_API_CALL
- return input_cost + output_cost + call_cost
- def process_chat(chat):
- total_input_tokens = 0
- total_output_tokens = 0
- num_calls = 0
- for message in chat['chat_messages']:
- if message['sender'] == 'human':
- total_input_tokens += num_tokens_from_string(message['text'], "cl100k_base")
- num_calls += 1 # Each human message typically triggers an API call
- elif message['sender'] == 'assistant':
- total_output_tokens += num_tokens_from_string(message['text'], "cl100k_base")
- return total_input_tokens, total_output_tokens, num_calls
- def main(file_path):
- with open(file_path, 'r') as file:
- data = json.load(file)
- total_input_tokens = 0
- total_output_tokens = 0
- total_api_calls = 0
- for chat in data:
- input_tokens, output_tokens, num_calls = process_chat(chat)
- total_input_tokens += input_tokens
- total_output_tokens += output_tokens
- total_api_calls += num_calls
- total_cost = calculate_cost(total_input_tokens, total_output_tokens, total_api_calls)
- print(f"Total input tokens: {total_input_tokens}")
- print(f"Total output tokens: {total_output_tokens}")
- print(f"Total API calls: {total_api_calls}")
- print(f"Estimated cost:")
- print(f" Token cost: ${(total_cost - (total_api_calls * PRICE_PER_API_CALL)):.2f}")
- print(f" API call cost: ${(total_api_calls * PRICE_PER_API_CALL):.2f}")
- print(f" Total cost: ${total_cost:.2f}")
- if __name__ == "__main__":
- file_path = "E:\dev\claude-fees\conversations.json" # Replace with your file path
- main(file_path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement