Skip to main content
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.
In Splunk SPL, you would need to manually calculate costs using eval and lookup tables.
| lookup model_pricing model OUTPUT input_price output_price
| eval total_cost=(input_tokens * input_price / 1000000) + (output_tokens * output_price / 1000000)
In ANSI SQL, you would need to join with a pricing table and calculate costs manually.
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

Usage

Syntax

genai_cost(model, input_tokens, output_tokens)

Parameters

  • model (string, required): The name of the AI model (for example, ‘gpt-4’, ‘claude-3-opus’, ‘gpt-3.5-turbo’).
  • input_tokens (long, required): The number of input tokens (prompt tokens) used in the API call.
  • output_tokens (long, required): 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.

Use case examples

  • Log analysis
  • OpenTelemetry traces
  • Security logs
Calculate the total cost of AI API calls over time to understand spending patterns and optimize usage.Query
['sample-http-logs']
| where uri contains '/api/openai'
| extend api_cost = genai_cost(todynamic(response_body)['model'], todynamic(response_body)['usage']['prompt_tokens'], todynamic(response_body)['usage']['completion_tokens'])
| summarize total_cost = sum(api_cost), avg_cost = avg(api_cost) by bin(_time, 1h)
Run in PlaygroundOutput
_timetotal_costavg_cost
2024-01-15T10:00:00Z12.450.0125
2024-01-15T11:00:00Z18.670.0187
This query calculates hourly spending on AI API calls, helping you track costs and identify spending trends.
  • 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.