> ## Documentation Index
> Fetch the complete documentation index at: https://axiom.co/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 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.

## For users of other query languages

If you come from other query languages, this section explains how to adjust your existing queries to achieve the same results in APL.

<AccordionGroup>
  <Accordion title="Splunk SPL users">
    In Splunk SPL, you would need to manually calculate costs using eval and lookup tables.

    <CodeGroup>
      ```sql Splunk example theme={null}
      | lookup model_pricing model OUTPUT input_price output_price
      | eval total_cost=(input_tokens * input_price / 1000000) + (output_tokens * output_price / 1000000)
      ```

      ```kusto APL equivalent theme={null}
      ['ai-logs']
      | extend total_cost = genai_cost(model, input_tokens, output_tokens)
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="ANSI SQL users">
    In ANSI SQL, you would need to join with a pricing table and calculate costs manually.

    <CodeGroup>
      ```sql SQL example theme={null}
      SELECT 
        l.*,
        (l.input_tokens * p.input_price / 1000000) + 
        (l.output_tokens * p.output_price / 1000000) as total_cost
      FROM ai_logs l
      JOIN model_pricing p ON l.model = p.model_name
      ```

      ```kusto APL equivalent theme={null}
      ['ai-logs']
      | extend total_cost = genai_cost(model, input_tokens, output_tokens)
      ```
    </CodeGroup>
  </Accordion>
</AccordionGroup>

## Usage

### Syntax

```kusto theme={null}
genai_cost(model, input_tokens, output_tokens)
```

### 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**

```kusto theme={null}
['otel-demo-genai']
| extend model = ['attributes.gen_ai.response.model']
| extend input_tokens = tolong(['attributes.gen_ai.usage.input_tokens'])
| extend output_tokens = tolong(['attributes.gen_ai.usage.output_tokens'])
| extend api_cost = genai_cost(model, input_tokens, output_tokens)
| summarize total_cost = sum(api_cost), avg_cost = avg(api_cost)
```

[Run in Playground](https://play.axiom.co/axiom-play-qf1k/query?initForm=%7B%22apl%22%3A%22%5B%27otel-demo-genai%27%5D%20%7C%20extend%20model%20%3D%20%5B%27attributes.gen_ai.response.model%27%5D%20%7C%20extend%20input_tokens%20%3D%20tolong\(%5B%27attributes.gen_ai.usage.input_tokens%27%5D\)%20%7C%20extend%20output_tokens%20%3D%20tolong\(%5B%27attributes.gen_ai.usage.output_tokens%27%5D\)%20%7C%20extend%20api_cost%20%3D%20genai_cost\(model%2C%20input_tokens%2C%20output_tokens\)%20%7C%20summarize%20total_cost%20%3D%20sum\(api_cost\)%2C%20avg_cost%20%3D%20avg\(api_cost\)%22%7D)

**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](/apl/scalar-functions/genai-functions/genai-input-cost): Calculates only the input token cost. Use this when you need to separate input and output costs.
* [genai\_output\_cost](/apl/scalar-functions/genai-functions/genai-output-cost): Calculates only the output token cost. Use this when analyzing generation costs separately.
* [genai\_get\_pricing](/apl/scalar-functions/genai-functions/genai-get-pricing): Gets the pricing structure for a model. Use this to understand or display pricing information.
* [genai\_estimate\_tokens](/apl/scalar-functions/genai-functions/genai-estimate-tokens): Estimates tokens from text. Use this with genai\_cost to predict costs before making API calls.
