Skip to main content

Token Usage Logging

All voice-session components now emit a standardized token usage log when they stop. Use this to query spending per CommunicationId and provider across LLM, transcribers, and synthesizers.

Log shape

  • Message: Token usage summary
  • Level: info
  • Fields (consistent for every component):
    • ComponentType: llm | transcriber | synthesizer
    • Provider: provider slug (e.g., openai, aws, deepgram, cartesia)
    • Model: provider-specific model/voice identifier (e.g., gpt-4.1, stt-rt-v3, aura-2-thalia-en)
    • Component: fine-grained step (e.g., summary, tags, sentiment, rate_call, identify_speakers, tts, stt, llm_realtime)
    • TotalInputTokens
    • TotalCachedInputTokens
    • TotalOutputTokens
    • InputAudioMinutes (transcribers) — precise input audio duration; TotalInputTokens is the ceiling of this value for parity with token fields
    • OutputAudioMinutes (synthesizers) — precise output audio duration; TotalOutputTokens is the ceiling of this value for parity with token fields
  • Context fields: base log entries already include CommunicationId, TenantId, and component/function names via logger.WithLogger.
Transcribers now emit input audio usage (InputAudioMinutes, with TotalInputTokens = ceil of minutes, TotalOutputTokens = transcript tokens). Synthesizers emit output audio usage (OutputAudioMinutes, with TotalOutputTokens = ceil of minutes).

Emission points

  • OpenAI realtime LLM stop (common/llm/openai/realtime.go).
  • Transcriber stops: AWS, Deepgram, Soniox, Speechmatics (common/transcription/realtime/transcribers/*).
  • Synthesizer stops: Azure OpenAI, Cartesia, DeepDub, Deepgram, ElevenLabs, MiniMax (wonderful-controller/components/synthesizer/providers/*).
  • Post-call enhancer steps (summary, tags, clarify, sentiment, rate call, identify speakers, issues) and rating helpers (wonderful-controller/components/communications/service/enhancer.go, .../rater.go).
  • RAG sessions on dispose (wonderful-controller/components/rags/service/session/logging_decorator.go) with Component = rag and provider gemini.

Implementation helper

Use logger.LogTokenUsage(entry, componentType, provider, model, models.TokensUsage) from common/logger/token_usage.go to emit the log with the standard message and fields.
import (
  "github.com/wonderfulcx/wonderful/common/logger"
  "github.com/wonderfulcx/wonderful/common/models"
)

logger.LogTokenUsage(logEntry, "llm", "openai", "gpt-4.1", models.TokensUsage{
  TotalInputTokens:       1200,
  TotalCachedInputTokens: 200,
  TotalOutputTokens:      900,
})

Query examples

  • Tokens by provider: "Token usage summary" @Provider:openai
  • Tokens for a call: "Token usage summary" @CommunicationId:<comm-id>
Add provider-specific token values as soon as upstream APIs expose them to keep the log accurate for cost tracking.