### Calculate LLM API Costs with OpenAI Integration in Python Source: https://context7.com/agentops-ai/tokencost/llms.txt Complete integration example demonstrating cost estimation before API calls and actual cost calculation after receiving completions from OpenAI. Combines calculate_prompt_cost, calculate_completion_cost, and calculate_all_costs_and_tokens functions to provide detailed breakdowns of token counts and costs, with comparison against API-reported usage. ```python from openai import OpenAI from tokencost import calculate_prompt_cost, calculate_completion_cost, calculate_all_costs_and_tokens client = OpenAI() model = "gpt-4o-mini" # Prepare prompt prompt = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Explain machine learning in simple terms."} ] # Calculate cost before making the request estimated_prompt_cost = calculate_prompt_cost(prompt, model) print(f"Estimated prompt cost: ${estimated_prompt_cost}") # Make API call response = client.chat.completions.create( messages=prompt, model=model, max_tokens=150 ) # Get completion and calculate actual cost completion = response.choices[0].message.content completion_cost = calculate_completion_cost(completion, model) total_cost = estimated_prompt_cost + completion_cost print(f"Completion: {completion}") print(f"Completion cost: ${completion_cost}") print(f"Total cost: ${total_cost}") # Alternative: Calculate everything at once result = calculate_all_costs_and_tokens(prompt, completion, model) print(f"\nDetailed breakdown:") print(f"Prompt tokens: {result['prompt_tokens']} (${result['prompt_cost']})") print(f"Completion tokens: {result['completion_tokens']} (${result['completion_cost']})") print(f"Total: {result['prompt_tokens'] + result['completion_tokens']} tokens, " f"${result['prompt_cost'] + result['completion_cost']}") # Compare with API reported usage api_prompt_tokens = response.usage.prompt_tokens api_completion_tokens = response.usage.completion_tokens print(f"\nAPI reported: {api_prompt_tokens} prompt tokens, {api_completion_tokens} completion tokens") ``` -------------------------------- ### Register Model Pricing with Wildcard Patterns (Python) Source: https://context7.com/agentops-ai/tokencost/llms.txt Enables dynamic pricing assignment for multiple models that match a specified wildcard pattern. Useful for managing pricing for families of models or specific providers like Bedrock and fine-tuned models. ```python from tokencost import register_model_pattern, calculate_prompt_cost # Register pricing for all models matching a pattern register_model_pattern( pattern="bedrock/anthropic.claude-3-5-sonnet-*", input_cost_per_1k_tokens=0.003, output_cost_per_1k_tokens=0.015, max_input_tokens=200000, max_output_tokens=4096, litellm_provider="bedrock", mode="chat" ) # Now any model matching the pattern will use this pricing prompt = "Test prompt" cost1 = calculate_prompt_cost(prompt, "bedrock/anthropic.claude-3-5-sonnet-20241022-v2:0") cost2 = calculate_prompt_cost(prompt, "bedrock/anthropic.claude-3-5-sonnet-20250101-v1:0") print(f"Sonnet variant 1 cost: ${cost1}") print(f"Sonnet variant 2 cost: ${cost2}") # Register pricing for fine-tuned models register_model_pattern( pattern="ft:gpt-4o-*", input_cost_per_1k_tokens=0.005, output_cost_per_1k_tokens=0.020, mode="chat" ) # Any fine-tuned GPT-4o model will match ft_cost = calculate_prompt_cost("Fine-tuned prompt", "ft:gpt-4o-my-org-123") print(f"Fine-tuned model cost: ${ft_cost}") ``` -------------------------------- ### Calculate All Costs and Tokens with TokenCost Source: https://context7.com/agentops-ai/tokencost/llms.txt Provides a comprehensive calculation of both costs and token counts for both the prompt and completion. It returns a dictionary containing prompt cost, prompt tokens, completion cost, and completion tokens. This is useful for a complete cost and usage overview. ```python from tokencost import calculate_all_costs_and_tokens prompt = "Hello world" completion = "How may I assist you today?" model = "gpt-3.5-turbo" result = calculate_all_costs_and_tokens(prompt, completion, model) print(result) # Output: { # 'prompt_cost': Decimal('0.0000030'), # 'prompt_tokens': 2, # 'completion_cost': Decimal('0.000014'), # 'completion_tokens': 7 # } # Use with message format messages = [ {"role": "user", "content": "What is Python?"}, {"role": "assistant", "content": "Python is a high-level programming language."} ] response = "It's known for its simple syntax and versatility." result = calculate_all_costs_and_tokens(messages, response, "gpt-4o") print(f"Prompt tokens: {result['prompt_tokens']}, cost: ${result['prompt_cost']}") print(f"Completion tokens: {result['completion_tokens']}, cost: ${result['completion_cost']}") print(f"Total tokens: {result['prompt_tokens'] + result['completion_tokens']}") print(f"Total cost: ${result['prompt_cost'] + result['completion_cost']}") ``` -------------------------------- ### Calculate Prompt and Completion Costs (Python) Source: https://github.com/agentops-ai/tokencost/blob/main/README.md Calculates the estimated cost of a prompt and a completion for a given model. It takes the prompt (as a list of messages or a string) and completion (as a string) and the model name as input, returning the respective costs in USD. It relies on the `tokencost` library. ```python from tokencost import calculate_prompt_cost, calculate_completion_cost model = "gpt-3.5-turbo" prompt = [{ "role": "user", "content": "Hello world"}] completion = "How may I assist you today?" prompt_cost = calculate_prompt_cost(prompt, model) completion_cost = calculate_completion_cost(completion, model) print(f"{prompt_cost} + {completion_cost} = {prompt_cost + completion_cost}") # 0.0000135 + 0.000014 = 0.0000275 ``` ```python from openai import OpenAI from tokencost import calculate_prompt_cost, calculate_completion_cost client = OpenAI() model = "gpt-3.5-turbo" prompt = [{ "role": "user", "content": "Say this is a test"}] chat_completion = client.chat.completions.create( messages=prompt, model=model ) completion = chat_completion.choices[0].message.content # "This is a test." prompt_cost = calculate_prompt_cost(prompt, model) completion_cost = calculate_completion_cost(completion, model) print(f"{prompt_cost} + {completion_cost} = {prompt_cost + completion_cost}") # 0.0000180 + 0.000010 = 0.0000280 ``` ```python from tokencost import calculate_prompt_cost prompt_string = "Hello world" response = "How may I assist you today?" model= "gpt-3.5-turbo" prompt_cost = calculate_prompt_cost(prompt_string, model) print(f"Cost: ${prompt_cost}") # Cost: $3e-06 ``` -------------------------------- ### Refresh Pricing Data and Access TOKEN_COSTS in Python Source: https://context7.com/agentops-ai/tokencost/llms.txt Updates the pricing database with the latest costs from LiteLLM's cost tracker and retrieves pricing information for specific models. The refresh_prices function can optionally write updated costs to a local file. TOKEN_COSTS dictionary provides direct access to model pricing data including input/output costs per token and token limits. ```python from tokencost import refresh_prices, TOKEN_COSTS # Refresh prices and write to local file updated_costs = refresh_prices(write_file=True) print(f"Updated {len(updated_costs)} model prices") # Refresh without writing to file updated_costs = refresh_prices(write_file=False) # Access pricing data directly gpt4o_pricing = TOKEN_COSTS.get("gpt-4o") if gpt4o_pricing: print(f"GPT-4o input cost per token: ${gpt4o_pricing['input_cost_per_token']}") print(f"GPT-4o output cost per token: ${gpt4o_pricing['output_cost_per_token']}") print(f"Max input tokens: {gpt4o_pricing['max_input_tokens']}") print(f"Max output tokens: {gpt4o_pricing['max_output_tokens']}") # Check if a model is supported model_name = "gpt-5-turbo" if model_name.lower() in TOKEN_COSTS: print(f"{model_name} is supported") else: print(f"{model_name} is not in the database") ``` -------------------------------- ### Configure Custom Model Pricing (Python) Source: https://context7.com/agentops-ai/tokencost/llms.txt Allows developers to register or override pricing for specific models, including custom-named models or existing ones like GPT-4o. Supports defining costs per token, max token limits, and provider information. ```python from tokencost import configure_model, calculate_prompt_cost # Add custom model pricing configure_model( model_name="my-custom-gpt", input_cost_per_1k_tokens=0.002, # $2.00 per 1M tokens output_cost_per_1k_tokens=0.006, # $6.00 per 1M tokens max_input_tokens=8192, max_output_tokens=4096, mode="chat" ) # Now use your custom model prompt = "Hello custom model" cost = calculate_prompt_cost(prompt, "my-custom-gpt") print(f"Custom model cost: ${cost}") # Override existing model pricing configure_model( model_name="gpt-4o", input_cost_per_1k_tokens=0.003, # Override with custom pricing output_cost_per_1k_tokens=0.012, litellm_provider="openai" ) # Configure Bedrock model configure_model( model_name="bedrock/custom-model-v1", input_cost_per_1k_tokens=0.001, output_cost_per_1k_tokens=0.003, litellm_provider="bedrock", max_input_tokens=100000 ) ``` -------------------------------- ### Count Tokens in Plain Text Strings (Python) Source: https://context7.com/agentops-ai/tokencost/llms.txt Shows how to count tokens in a simple string without message formatting overhead. Useful for direct text analysis. Supports various OpenAI models and highlights potential differences in tokenization across models. Note that Claude models do not support this function. ```python from tokencost import count_string_tokens # Simple string prompt = "Hello world" tokens = count_string_tokens(prompt, "gpt-3.5-turbo") print(f"String tokens: {tokens}") # Output: String tokens: 2 # Longer text long_text = "This is a longer piece of text that will be tokenized." tokens = count_string_tokens(long_text, "gpt-4o") print(f"Tokens in long text: {tokens}") # Different models may have different tokenization text = "The quick brown fox jumps over the lazy dog." gpt35_tokens = count_string_tokens(text, "gpt-3.5-turbo") gpt4_tokens = count_string_tokens(text, "gpt-4") print(f"GPT-3.5 tokens: {gpt35_tokens}, GPT-4 tokens: {gpt4_tokens}") # Note: Claude models don't support string token counting # Use count_message_tokens instead for Claude ``` -------------------------------- ### Calculate Prompt Cost with TokenCost Source: https://context7.com/agentops-ai/tokencost/llms.txt Estimates the USD cost of a given prompt before sending it to an LLM API. Supports both message list format (recommended for chat models) and plain string inputs. Works with various models including OpenAI, Azure, and Anthropic (requires ANTHROPIC_API_KEY). ```python from tokencost import calculate_prompt_cost # Message format (recommended for chat models) prompt = [ {"role": "user", "content": "Hello world"}, {"role": "assistant", "content": "How may I assist you today?"} ] model = "gpt-3.5-turbo" prompt_cost = calculate_prompt_cost(prompt, model) print(f"Prompt cost: ${prompt_cost}") # Output: Prompt cost: $0.0000300 # String format prompt_string = "Hello world" prompt_cost = calculate_prompt_cost(prompt_string, "gpt-4o") print(f"Prompt cost: ${prompt_cost}") # Output: Prompt cost: $0.000005 # Works with Azure models azure_prompt_cost = calculate_prompt_cost(prompt, "azure/gpt-4o") print(f"Azure prompt cost: ${azure_prompt_cost}") # Claude models (requires ANTHROPIC_API_KEY) claude_prompt = [{"role": "user", "content": "What is machine learning?"}] claude_cost = calculate_prompt_cost(claude_prompt, "claude-3-5-sonnet-20241022") print(f"Claude cost: ${claude_cost}") ``` -------------------------------- ### Calculate API Call Costs by Token Count (Python) Source: https://context7.com/agentops-ai/tokencost/llms.txt Enables cost calculation when the exact token count is already known, often from API response usage data. Supports input, output, and cached tokens for specified models. ```python from tokencost import calculate_cost_by_tokens # Calculate input token cost num_tokens = 1000 model = "gpt-4o" input_cost = calculate_cost_by_tokens(num_tokens, model, "input") print(f"Cost for {num_tokens} input tokens: ${input_cost}") # Output: Cost for 1000 input tokens: $0.0025 # Calculate output token cost output_cost = calculate_cost_by_tokens(num_tokens, model, "output") print(f"Cost for {num_tokens} output tokens: ${output_cost}") # Output: Cost for 1000 output tokens: $0.01 # Cached token cost (for models supporting prompt caching) cached_cost = calculate_cost_by_tokens(num_tokens, model, "cached") print(f"Cost for {num_tokens} cached tokens: ${cached_cost}") # Calculate costs from API response usage data api_usage = {"prompt_tokens": 150, "completion_tokens": 75} total_cost = ( calculate_cost_by_tokens(api_usage["prompt_tokens"], "gpt-3.5-turbo", "input") + calculate_cost_by_tokens(api_usage["completion_tokens"], "gpt-3.5-turbo", "output") ) print(f"Total API call cost: ${total_cost}") ``` -------------------------------- ### Count Tokens for Named Messages and Claude Models (Python) Source: https://context7.com/agentops-ai/tokencost/llms.txt Demonstrates how to count tokens for messages that include a 'name' field and for Claude models. Requires the 'tokencost' library and potentially an ANTHROPIC_API_KEY for Claude models. ```python from tokencost import count_message_tokens # Messages with names named_messages = [ {"role": "user", "content": "Hello", "name": "John"}, {"role": "assistant", "content": "Hi there!"} ] tokens_with_name = count_message_tokens(named_messages, "gpt-4-turbo") print(f"Tokens with name field: {tokens_with_name}") # Claude models (requires ANTHROPIC_API_KEY) claude_messages = [{"role": "user", "content": "Explain quantum computing"}] claude_tokens = count_message_tokens(claude_messages, "claude-3-5-sonnet-20241022") print(f"Claude tokens: {claude_tokens}") ``` -------------------------------- ### Calculate Completion Cost with TokenCost Source: https://context7.com/agentops-ai/tokencost/llms.txt Calculates the USD cost for a model's completion or response text. This function is useful for estimating the cost of generated output from LLM APIs. It accepts the completion text and the model name as input. ```python from tokencost import calculate_completion_cost completion = "How may I assist you today?" model = "gpt-3.5-turbo" completion_cost = calculate_completion_cost(completion, model) print(f"Completion cost: ${completion_cost}") # Output: Completion cost: $0.000014 # With different models gpt4_cost = calculate_completion_cost("This is a longer response with more tokens.", "gpt-4o") print(f"GPT-4o completion cost: ${gpt4_cost}") # Calculate total cost prompt = [{"role": "user", "content": "Say this is a test"}] prompt_cost = calculate_prompt_cost(prompt, "gpt-4o-mini") completion = "This is a test." completion_cost = calculate_completion_cost(completion, "gpt-4o-mini") total_cost = prompt_cost + completion_cost print(f"Total cost: ${total_cost}") ``` -------------------------------- ### Count Message Tokens with TokenCost Source: https://context7.com/agentops-ai/tokencost/llms.txt Counts the total number of tokens in a prompt formatted as a message list. This function accounts for message formatting overhead and is essential for accurately predicting token usage in chat-based LLM interactions. Supports various models. ```python from tokencost import count_message_tokens # Basic message counting message_prompt = [{"role": "user", "content": "Hello world"}] token_count = count_message_tokens(message_prompt, "gpt-3.5-turbo") print(f"Token count: {token_count}") # Output: Token count: 9 # Multi-message conversation messages = [ {"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Hi there!"} ] tokens = count_message_tokens(messages, "gpt-4o") print(f"Conversation tokens: {tokens}") # Output: Conversation tokens: 15 ``` -------------------------------- ### Count Tokens (Python) Source: https://github.com/agentops-ai/tokencost/blob/main/README.md Counts the number of tokens in a given prompt, either formatted as a list of messages or as a plain string. It utilizes the `tokencost` library and supports specifying the model for accurate counting. Returns the token count as an integer. ```python from tokencost import count_message_tokens, count_string_tokens message_prompt = [{ "role": "user", "content": "Hello world"}] # Counting tokens in prompts formatted as message lists print(count_message_tokens(message_prompt, model="gpt-3.5-turbo")) # 9 # Alternatively, counting tokens in string prompts print(count_string_tokens(prompt="Hello world", model="gpt-3.5-turbo")) # 2 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.