Class: Roast::Services::TokenCountingService

Inherits:
Object
  • Object
show all
Defined in:
lib/roast/services/token_counting_service.rb

Constant Summary collapse

CHARS_PER_TOKEN =

Approximate character-to-token ratio for English text Based on OpenAI’s rule of thumb: ~4 characters per token

4.0
MESSAGE_OVERHEAD_TOKENS =

Base token overhead for message structure

3

Instance Method Summary collapse

Instance Method Details

#count_messages(messages) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/roast/services/token_counting_service.rb', line 13

def count_messages(messages)
  return 0 if messages.nil? || messages.empty?

  messages.sum do |message|
    count_message(message)
  end
end