Skip to main content
GenAI functions in APL help you analyze and process GenAI conversation data, including messages, token usage, costs, and conversation metadata. These functions are useful when working with logs or data from large language models (LLMs) and AI systems.

When to use GenAI functions

Use GenAI functions when you need to:
  • Extract specific information from AI conversation logs, such as user prompts, assistant responses, or system prompts
  • Calculate token costs and usage metrics for LLM API calls
  • Analyze conversation structure and flow, including turn counts and message roles
  • Process and filter conversation messages based on roles or content
  • Determine pricing information for different AI models
  • Detect truncation or tool calls in AI responses

Available GenAI functions

FunctionDescription
genai_concat_contentsConcatenates message contents from a conversation array
genai_conversation_turnsCounts the number of conversation turns
genai_costCalculates the total cost for input and output tokens
genai_estimate_tokensEstimates the number of tokens in a text string
genai_extract_assistant_responseExtracts the assistant’s response from a conversation
genai_extract_function_resultsExtracts function call results from messages
genai_extract_system_promptExtracts the system prompt from a conversation
genai_extract_tool_callsExtracts tool calls from messages
genai_extract_user_promptExtracts the user prompt from a conversation
genai_get_content_by_indexGets message content by index position
genai_get_content_by_roleGets message content by role
genai_get_pricingGets pricing information for a specific model
genai_get_roleGets the role of a message at a specific index
genai_has_tool_callsChecks if messages contain tool calls
genai_input_costCalculates the cost for input tokens
genai_is_truncatedChecks if a response was truncated
genai_message_rolesExtracts all message roles from a conversation
genai_output_costCalculates the cost for output tokens

Common use cases

Analyzing conversation costs

Calculate the total cost of AI conversations across different models and usage patterns.
['ai-logs']
| extend total_cost = genai_cost(model, input_tokens, output_tokens)
| summarize sum(total_cost) by model

Extracting conversation components

Extract specific parts of conversations for analysis or debugging.
['ai-logs']
| extend user_query = genai_extract_user_prompt(messages)
| extend ai_response = genai_extract_assistant_response(messages)
| project _time, user_query, ai_response

Monitoring token usage

Track and analyze token consumption patterns.
['ai-logs']
| extend estimated_tokens = genai_estimate_tokens(content)
| summarize avg(estimated_tokens), max(estimated_tokens) by model