Module: RubynCode::Observability::TokenCounter
- Defined in:
- lib/rubyn_code/observability/token_counter.rb
Overview
Estimates token counts from text using a character-based heuristic.
This provides a fast approximation (~4 characters per token) suitable for budget tracking and context-window management. For exact counts, use the API’s reported usage fields instead.
Constant Summary collapse
- CHARS_PER_TOKEN =
Average characters per token for English text and source code.
4
Class Method Summary collapse
-
.estimate(text) ⇒ Integer
Estimates the token count for a given string.
-
.estimate_messages(messages) ⇒ Integer
Estimates the token count for an array of messages by serializing them to JSON first.
Class Method Details
.estimate(text) ⇒ Integer
Estimates the token count for a given string.
21 22 23 24 25 |
# File 'lib/rubyn_code/observability/token_counter.rb', line 21 def estimate(text) return 0 if text.nil? || text.empty? (text.bytesize.to_f / CHARS_PER_TOKEN).ceil end |
.estimate_messages(messages) ⇒ Integer
Estimates the token count for an array of messages by serializing them to JSON first. Accounts for the structural overhead of message formatting (role tags, separators, etc.).
33 34 35 36 37 38 |
# File 'lib/rubyn_code/observability/token_counter.rb', line 33 def () return 0 if .nil? || .empty? json = JSON.generate() estimate(json) end |