ExLLM.Core.Cost.Display (ex_llm v0.8.1)

View Source

Utilities for displaying cost information in various formats.

This module provides flexible display functions for cost data, supporting multiple output formats including ASCII tables, markdown, CSV, and specialized CLI displays. It's designed to work with ExLLM.Cost and ExLLM.Core.Cost.Session data structures.

Supported Formats

  • ASCII Tables: Terminal-friendly tables with borders and alignment
  • Markdown Tables: GitHub-flavored markdown tables
  • CSV Output: Comma-separated values for data processing
  • JSON Output: Structured JSON for API responses
  • CLI Summary: Rich terminal output with emojis and formatting
  • Streaming Display: Real-time cost updates during streaming

Usage

# Generate cost breakdown table
table = ExLLM.Core.Cost.Display.cost_breakdown_table(cost_data, format: :ascii)

# CLI-friendly session summary
summary = ExLLM.Core.Cost.Display.cli_summary(session_summary)

# Real-time streaming cost display
display = ExLLM.Core.Cost.Display.streaming_cost_display(0.05, 0.12)

# Cost alerts
alert = ExLLM.Core.Cost.Display.cost_alert(:budget_exceeded, %{current: 1.25, budget: 1.00})

Summary

Functions

Generate cost summary for CLI display.

Generate comparison table for multiple providers or models.

Generate cost alert messages.

Generate cost breakdown table in various formats.

Generate real-time cost display for streaming responses.

Functions

cli_summary(session_summary)

@spec cli_summary(map()) :: String.t()

Generate cost summary for CLI display.

Creates a rich, formatted summary perfect for terminal display with emojis, hierarchical structure, and color-friendly formatting.

Examples

summary_text = ExLLM.Core.Cost.Display.cli_summary(session_summary)
IO.puts(summary_text)

comparison_table(comparisons, opts \\ [])

@spec comparison_table(
  list(),
  keyword()
) :: String.t()

Generate comparison table for multiple providers or models.

Creates a side-by-side comparison table showing costs across different providers or models for the same usage pattern.

Examples

comparison = ExLLM.Core.Cost.Display.comparison_table([
  %{provider: "openai", model: "gpt-4", cost: 0.75},
  %{provider: "anthropic", model: "claude-3-5-sonnet", cost: 0.45},
  %{provider: "openai", model: "gpt-3.5-turbo", cost: 0.15}
])

cost_alert(alert_type, data)

@spec cost_alert(atom(), map()) :: String.t()

Generate cost alert messages.

Creates formatted alert messages for various cost-related events like budget overruns, high costs, or efficiency warnings.

Alert Types

  • :budget_exceeded - When session or message exceeds budget
  • :high_cost_warning - When a single message has unusually high cost
  • :efficiency_warning - When cost efficiency is below expected thresholds
  • :provider_recommended - When suggesting a more cost-effective provider

Examples

# Budget exceeded alert
alert = ExLLM.Core.Cost.Display.cost_alert(:budget_exceeded, %{
  current: 1.25, 
  budget: 1.00,
  session_id: "chat_123"
})

# High cost warning
alert = ExLLM.Core.Cost.Display.cost_alert(:high_cost_warning, %{
  cost: 0.75,
  model: "gpt-4",
  threshold: 0.50
})

cost_breakdown_table(cost_data, opts \\ [])

@spec cost_breakdown_table(
  list() | map(),
  keyword()
) :: String.t()

Generate cost breakdown table in various formats.

Options

  • :format - Output format (:ascii, :markdown, :csv, :json) (default: :ascii)
  • :columns - List of columns to include (default: all available)
  • :sort_by - Column to sort by (default: :total_cost)

Examples

# ASCII table
ExLLM.Core.Cost.Display.cost_breakdown_table(cost_data)

# Markdown table
ExLLM.Core.Cost.Display.cost_breakdown_table(cost_data, format: :markdown)

# CSV with specific columns
ExLLM.Core.Cost.Display.cost_breakdown_table(cost_data, 
  format: :csv, 
  columns: [:provider, :model, :total_cost]
)

streaming_cost_display(current_cost, estimated_final_cost, opts \\ [])

@spec streaming_cost_display(float(), float(), keyword()) :: String.t()

Generate real-time cost display for streaming responses.

Shows current cost with progress indication toward estimated final cost. Perfect for displaying during streaming responses.

Examples

# During streaming
display = ExLLM.Core.Cost.Display.streaming_cost_display(0.05, 0.12)
# => "💰 $0.0500 (41.7% of estimated $0.1200)"

# With custom format
display = ExLLM.Core.Cost.Display.streaming_cost_display(0.05, 0.12, style: :compact)
# => "💰 $0.050 (42%)"