genai_cost
This page explains how to use the genai_cost function in APL.
The genai_cost function calculates the total cost of a GenAI API call based on the model name, input tokens, and output tokens. This function uses current pricing information for various AI models to provide accurate cost estimates.
You can use this function to track AI spending, analyze cost per conversation, identify expensive queries, or create cost reports and budgets for AI services.
Usage
Syntax
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| model | string | Yes | The name of the AI model (for example, 'gpt-4', 'claude-3-opus', 'gpt-3.5-turbo'). |
| input_tokens | long | Yes | The number of input tokens (prompt tokens) used in the API call. |
| output_tokens | long | Yes | The number of output tokens (completion tokens) generated by the API call. |
Returns
Returns a real number representing the total cost in dollars (USD) for the API call based on the model's pricing.
Example
Calculate the total cost of a GenAI API call based on model and token usage.
Query
Output
| total_cost | avg_cost |
|---|---|
| 12.45 | 0.0125 |
This query calculates total and average spending on AI API calls, helping you track costs and identify spending trends.
List of related functions
- genai_input_cost: Calculates only the input token cost. Use this when you need to separate input and output costs.
- genai_output_cost: Calculates only the output token cost. Use this when analyzing generation costs separately.
- genai_get_pricing: Gets the pricing structure for a model. Use this to understand or display pricing information.
- genai_estimate_tokens: Estimates tokens from text. Use this with genai_cost to predict costs before making API calls.