# genai_input_cost



The `genai_input_cost` function calculates the cost of input tokens (prompt tokens) for a GenAI API call based on the model name and number of input tokens. This helps you understand and track the cost of prompts separately from responses.

You can use this function to analyze prompt costs, optimize prompt engineering for cost efficiency, track input spending separately, or create detailed cost breakdowns.

## Usage [#usage]

### Syntax [#syntax]

```kusto
genai_input_cost(model, input_tokens)
```

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

### Returns [#returns]

Returns a real number representing the cost in dollars (USD) for the input tokens based on the model's pricing.

## Example [#example]

Calculate the cost of input tokens for a GenAI chat operation.

**Query**

```kusto
['otel-demo-genai']
| extend model = ['attributes.gen_ai.request.model']
| extend input_tokens = tolong(['attributes.gen_ai.usage.input_tokens'])
| extend input_cost = genai_input_cost(model, input_tokens)
| summarize total_input_cost = sum(input_cost), avg_input_cost = avg(input_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.request.model%27%5D%20%7C%20extend%20input_tokens%20%3D%20tolong\(%5B%27attributes.gen_ai.usage.input_tokens%27%5D\)%20%7C%20extend%20input_cost%20%3D%20genai_input_cost\(model%2C%20input_tokens\)%20%7C%20summarize%20total_input_cost%20%3D%20sum\(input_cost\)%2C%20avg_input_cost%20%3D%20avg\(input_cost\)%22%7D)

**Output**

| total\_input\_cost | avg\_input\_cost |
| ------------------ | ---------------- |
| 45.67              | 0.0187           |

This query calculates the total and average cost of input tokens, helping you understand prompt spending patterns.

## List of related functions [#list-of-related-functions]

* [genai\_output\_cost](/apl/scalar-functions/genai-functions/genai-output-cost): Calculates output token cost. Use this alongside input costs to understand the full cost breakdown.
* [genai\_cost](/apl/scalar-functions/genai-functions/genai-cost): Calculates total cost (input + output). Use this when you need combined costs.
* [genai\_get\_pricing](/apl/scalar-functions/genai-functions/genai-get-pricing): Gets pricing information. Use this to understand the pricing structure behind cost calculations.
* [genai\_estimate\_tokens](/apl/scalar-functions/genai-functions/genai-estimate-tokens): Estimates token count from text. Combine with input cost to predict prompt costs before API calls.

## Other query languages [#other-query-languages]

<LanguageComparisons>
  <Accordion title="Splunk SPL users">
    In Splunk SPL, you would need to lookup pricing and calculate costs manually.

    <CodeGroup>
      ```sql Splunk example
      | lookup model_pricing model OUTPUT input_price
      | eval input_cost=(input_tokens * input_price / 1000000)
      ```

      ```kusto APL equivalent
      ['ai-logs']
      | extend input_cost = genai_input_cost(model, input_tokens)
      ```
    </CodeGroup>
  </Accordion>

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

    <CodeGroup>
      ```sql SQL example
      SELECT
        l.*,
        (l.input_tokens * p.input_price / 1000000) as input_cost
      FROM ai_logs l
      JOIN model_pricing p ON l.model = p.model_name
      ```

      ```kusto APL equivalent
      ['ai-logs']
      | extend input_cost = genai_input_cost(model, input_tokens)
      ```
    </CodeGroup>
  </Accordion>
</LanguageComparisons>
