Overview

genai_output_cost

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

Usage#

Syntax#

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

['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

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.

  • genai_input_cost: Calculates input token cost. Use this alongside output costs to understand the full cost breakdown.
  • genai_cost: Calculates total cost (input + output). Use this when you need combined costs.
  • genai_get_pricing: Gets pricing information. Use this to understand the pricing structure behind cost calculations.
  • genai_extract_assistant_response: Extracts the response text. Combine with output costs to analyze cost per response.
  • genai_is_truncated: Checks if responses were truncated. Use this to understand if token limits affected output costs.

Other query languages#

Splunk SPL users

In Splunk SPL, you would need to lookup pricing and calculate costs manually.

Splunk example

| lookup model_pricing model OUTPUT output_price
| eval output_cost=(output_tokens * output_price / 1000000)

APL equivalent

['ai-logs']
| extend output_cost = genai_output_cost(model, output_tokens)
ANSI SQL users

In ANSI SQL, you would join with a pricing table and calculate output costs.

SQL example

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

APL equivalent

['ai-logs']
| extend output_cost = genai_output_cost(model, output_tokens)

Updated

Was this page helpful?