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

<AgentInstructions>

## Submitting Feedback

If you encounter incorrect, outdated, or confusing documentation on this page, submit feedback:

POST https://axiom.co/docs/feedback

```json
{
  "path": "/apl/scalar-functions/genai-functions/genai-output-cost",
  "feedback": "Description of the issue"
}
```

Only submit feedback when you have something specific and actionable to report.

</AgentInstructions>

# genai_output_cost

> This page explains how to use the genai_output_cost function in APL.

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

You can use this function to analyze generation costs, optimize response length for cost efficiency, track output spending separately, or create detailed cost breakdowns.

## 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 lookup pricing and calculate costs manually.

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

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

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

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

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

## Usage

### Syntax

```kusto theme={null}
genai_output_cost(model, 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'). |
| output\_tokens | long   | Yes      | The number of output tokens (completion tokens) generated by the API call.         |

### Returns

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

## Example

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

**Query**

```kusto theme={null}
['otel-demo-genai']
| extend model = ['attributes.gen_ai.response.model']
| extend output_tokens = tolong(['attributes.gen_ai.usage.output_tokens'])
| extend output_cost = genai_output_cost(model, output_tokens)
| summarize total_output_cost = sum(output_cost), avg_output_cost = avg(output_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%20output_tokens%20%3D%20tolong\(%5B%27attributes.gen_ai.usage.output_tokens%27%5D\)%20%7C%20extend%20output_cost%20%3D%20genai_output_cost\(model%2C%20output_tokens\)%20%7C%20summarize%20total_output_cost%20%3D%20sum\(output_cost\)%2C%20avg_output_cost%20%3D%20avg\(output_cost\)%22%7D)

**Output**

| total\_output\_cost | avg\_output\_cost |
| ------------------- | ----------------- |
| 78.34               | 0.0321            |

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

## List of related functions

* [genai\_input\_cost](/apl/scalar-functions/genai-functions/genai-input-cost): Calculates input token cost. Use this alongside output 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\_extract\_assistant\_response](/apl/scalar-functions/genai-functions/genai-extract-assistant-response): Extracts the response text. Combine with output costs to analyze cost per response.
* [genai\_is\_truncated](/apl/scalar-functions/genai-functions/genai-is-truncated): Checks if responses were truncated. Use this to understand if token limits affected output costs.
