Token Calculate

Model Pricing

The ModelPricing class was created to represent a pricing model for token-based natural language models, allowing the cost associated with processing inputs and outputs in different language models to be calculated.

On this page

    #Overview

    The ModelPricing class was created to represent a pricing model for token-based natural language models, allowing the cost associated with processing inputs and outputs in different language models to be calculated. It solves the problem of pricing the use of language models, converting costs per million tokens into unit costs, making financial estimation easier for API calls or model usage.

    In practice, this class can be used to get the cost of a request involving a certain number of input and output tokens quickly and directly, just by providing the desired model. This is useful for anyone working with language processing APIs, allowing them to estimate spending and control token-based usage.

    #Execution Flow

    1. An instance of the ModelPricing class is initialized by passing the model name as a parameter. If the model is not supported, an exception is raised.
    2. Through the input_rate_per_token and output_rate_per_token methods, the unit cost per input and output token is obtained, respectively, converting the values that are originally defined per million tokens.
    3. These values are multiplied by the actual number of tokens processed to calculate the total input and output cost.
    4. The final result is the estimated cost in dollars for using the model for the given number of tokens.

    #Class Methods Table

    MethodDescription
    __init__Initializes the object with the chosen model, validating it.
    input_rate_per_tokenReturns the unit cost per input token.
    output_rate_per_tokenReturns the unit cost per output token.

    #Key Architecture Points and Insights

    • Pricing is stored in COST_MODELS, a dictionary that maps the model name to the costs per million tokens for input and output.
    • The calculation method converts the cost per million to a cost per token by dividing the value by 1,000,000, ensuring flexibility for different scales.
    • This approach encapsulates pricing in a class, making maintenance and extension to new models easier, just by adding entries to the dictionary.
    • By validating the model at initialization, later errors during calculation are avoided, ensuring safe use of the class.

    #Class and Methods Description

    #ModelPricing Class

    #Description

    Class for calculating the cost of using language models, based on the number of input and output tokens and on the prices defined per model. It provides a simple interface to convert prices per million tokens into unit prices.

    #Constructor Arguments

    ArgumentTypeDescriptionDefault Value
    modelstrThe name of the model to calculate costs for.None

    #Methods

    #1. __init__

    Description

    Initializes the class instance by checking that the given model exists in the base of supported prices, and assigns the corresponding prices.

    Arguments

    • model (str): name of the language model used to look up prices.

    Returns

    • Returns no value.

    Raises

    • ValueError: If the given model is not listed among the supported models.

    Examples

    model_pricing = ModelPricing("gpt-4.1-mini")

    #2. input_rate_per_token

    Description

    Calculates and returns the cost per input token, based on the configured price per million tokens.

    Arguments

    • None.

    Returns

    • float: cost in dollars per input token.

    Raises

    • None.

    Examples

    input_cost_per_token = model_pricing.input_rate_per_token()
    # Retorna algo como 0.0000004 para "gpt-4.1-mini"

    #3. output_rate_per_token

    Description

    Calculates and returns the cost per output token, based on the configured price per million tokens.

    Arguments

    • None.

    Returns

    • float: cost in dollars per output token.

    Raises

    • None.

    Examples

    output_cost_per_token = model_pricing.output_rate_per_token()
    # Retorna algo como 0.0000016 para "gpt-4.1-mini"

    #Complete Usage Example

    model = "gpt-4.1-mini"
    input_token_count = 946
    output_token_count = 255
    
    model_pricing = ModelPricing(model)
    input_cost = model_pricing.input_rate_per_token() * input_token_count
    output_cost = model_pricing.output_rate_per_token() * output_token_count
    
    print(f"Custo de entrada (USD): {input_cost:.6f}")
    print(f"Custo de saída (USD): {output_cost:.6f}")
    
    # python -m src.tokens_calculate.model_pricing

    Expected output:

    Custo de entrada (USD): 0.000378
    Custo de saída (USD): 0.000408

    This example illustrates how to calculate the total cost based on real token counts for a specific model, making financial control and cost analysis easier for calls to language models.

    Source: src/tokens_calculate/model_pricing.py

    Esc
    ↑↓navigate Enteropen