Skip to content

Instantly share code, notes, and snippets.

@thorwhalen
Last active December 6, 2023 10:32
Show Gist options
  • Save thorwhalen/4e888c4ede4bbd4a1a134ee90ccadcdc to your computer and use it in GitHub Desktop.
Save thorwhalen/4e888c4ede4bbd4a1a134ee90ccadcdc to your computer and use it in GitHub Desktop.
OpenAI API Cost calculator
api_costs = {
'gpt-4-1106-preview': {"input": 0.01, "output": 0.03, "unit": "$/1K tokens"},
'gpt-4-1106-vision-preview': {"input": 0.01, "output": 0.03, "unit": "$/1K tokens"},
'gpt-4': {"input": 0.03, "output": 0.06, "unit": "$/1K tokens"},
'gpt-4-32k': {"input": 0.06, "output": 0.12, "unit": "$/1K tokens"},
'gpt-3.5-turbo-1106': {"input": 0.0010, "output": 0.0020, "unit": "$/1K tokens"},
'gpt-3.5-turbo-instruct': {"input": 0.0015, "output": 0.0020, "unit": "$/1K tokens"},
'gpt-3.5-turbo': {"training": 0.0080, "input": 0.0030, "output": 0.0060, "unit": "$/1K tokens"},
'davinci-002': {"training": 0.0060, "input": 0.0120, "output": 0.0120, "unit": "$/1K tokens"},
'babbage-002': {"training": 0.0004, "input": 0.0016, "output": 0.0016, "unit": "$/1K tokens"},
'ada v2': {"usage": 0.0001, "unit": "$/1K tokens"},
'davinci-002-base': {"usage": 0.0020, "unit": "$/1K tokens"},
'babbage-002-base': {"usage": 0.0004, "unit": "$/1K tokens"},
'dalle-3-standard-1024x1024': {"price": 0.040, "unit": "$/image"},
'dalle-3-standard-1024x1792-1792x1024': {"price": 0.080, "unit": "$/image"},
'dalle-3-hd-1024x1024': {"price": 0.080, "unit": "$/image"},
'dalle-3-hd-1024x1792-1792x1024': {"price": 0.120, "unit": "$/image"},
'dalle-2-1024x1024': {"price": 0.020, "unit": "$/image"},
'dalle-2-512x512': {"price": 0.018, "unit": "$/image"},
'dalle-2-256x256': {"price": 0.016, "unit": "$/image"},
'whisper': {"usage": 0.006, "unit": "$/minute"},
'tts': {"usage": 0.015, "unit": "$/1K characters"},
'tts-hd': {"usage": 0.030, "unit": "$/1K characters"}
}
# Synonyms
api_costs['embeddings'] = api_costs['ada v2']
def estimate_api_cost(
n_users=1,
n_of_requests_per_user=10,
n_words_per_request=10,
n_tokens_per_word=0.75,
cost_per_token=api_costs['embeddings']['usage'] / 1000
):
"""
This function estimates the cost of using a token-based API based on the input parameters.
Parameters:
n_users (int): The number of users who will be using the API. Default is 1.
n_of_requests_per_user (int): The number of requests each user will make. Default is 10.
n_words_per_request (int): The number of words in each request. Default is 100.
n_tokens_per_word (int): The number of tokens per word. Default is 4.
cost_per_token (float): The cost per token in dollars. Default is 0.01.
Returns:
float: The estimated cost of using the API in dollars.
"""
n_tokens_per_request = n_words_per_request * n_tokens_per_word
n_tokens_total = n_users * n_of_requests_per_user * n_tokens_per_request
cost_total = n_tokens_total * cost_per_token
print('asdf')
return cost_total
@thorwhalen
Copy link
Author

estimate_api_cost(
    n_users=1000, 
    n_of_requests_per_user=1000,
    n_words_per_request=13,
    cost_per_token=api_costs['embeddings']['usage'] / 1000
)
# 0.975

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment