Module: DSPy::Instrumentation::TokenTracker

Extended by:
TokenTracker
Included in:
TokenTracker
Defined in:
lib/dspy/instrumentation/token_tracker.rb

Overview

Utility for extracting token usage from different LM adapters Uses actual token counts from API responses for accuracy

Instance Method Summary collapse

Instance Method Details

#extract_token_usage(response, provider) ⇒ Object

Extract actual token usage from API responses



11
12
13
14
15
16
17
18
19
20
# File 'lib/dspy/instrumentation/token_tracker.rb', line 11

def extract_token_usage(response, provider)
  case provider.to_s.downcase
  when 'openai'
    extract_openai_tokens(response)
  when 'anthropic'
    extract_anthropic_tokens(response)
  else
    {} # No token information for other providers
  end
end