Class: RubynCode::Observability::TokenAnalytics
- Inherits:
-
Object
- Object
- RubynCode::Observability::TokenAnalytics
- Defined in:
- lib/rubyn_code/observability/token_analytics.rb
Overview
Tracks detailed token usage breakdown by category (system prompt, skills, context files, conversation, tool output) and reports savings from efficiency features. Powers the enhanced /cost command.
Constant Summary collapse
- CHARS_PER_TOKEN =
4- CATEGORIES =
%i[ system_prompt skills_loaded context_files conversation tool_output code_written explanations tool_calls ].freeze
Instance Attribute Summary collapse
-
#input_breakdown ⇒ Object
readonly
Returns the value of attribute input_breakdown.
-
#output_breakdown ⇒ Object
readonly
Returns the value of attribute output_breakdown.
-
#savings ⇒ Object
readonly
Returns the value of attribute savings.
Instance Method Summary collapse
-
#initialize ⇒ TokenAnalytics
constructor
A new instance of TokenAnalytics.
-
#record_input(category, tokens) ⇒ Object
Record input token usage by category.
-
#record_output(category, tokens) ⇒ Object
Record output token usage by category.
-
#record_savings(feature, tokens) ⇒ Object
Record tokens saved by an efficiency feature.
-
#record_turn! ⇒ Object
Increment the turn counter.
-
#report ⇒ Object
Format a complete analytics report.
-
#session_minutes ⇒ Object
Session duration in minutes.
-
#total_input_tokens ⇒ Object
Total input tokens across all categories.
-
#total_output_tokens ⇒ Object
Total output tokens across all categories.
-
#total_tokens_saved ⇒ Object
Total tokens saved across all features.
Constructor Details
#initialize ⇒ TokenAnalytics
Returns a new instance of TokenAnalytics.
19 20 21 22 23 24 25 |
# File 'lib/rubyn_code/observability/token_analytics.rb', line 19 def initialize @input_breakdown = Hash.new(0) @output_breakdown = Hash.new(0) @savings = Hash.new(0) @start_time = Time.now @turn_count = 0 end |
Instance Attribute Details
#input_breakdown ⇒ Object (readonly)
Returns the value of attribute input_breakdown.
17 18 19 |
# File 'lib/rubyn_code/observability/token_analytics.rb', line 17 def input_breakdown @input_breakdown end |
#output_breakdown ⇒ Object (readonly)
Returns the value of attribute output_breakdown.
17 18 19 |
# File 'lib/rubyn_code/observability/token_analytics.rb', line 17 def output_breakdown @output_breakdown end |
#savings ⇒ Object (readonly)
Returns the value of attribute savings.
17 18 19 |
# File 'lib/rubyn_code/observability/token_analytics.rb', line 17 def savings @savings end |
Instance Method Details
#record_input(category, tokens) ⇒ Object
Record input token usage by category.
28 29 30 |
# File 'lib/rubyn_code/observability/token_analytics.rb', line 28 def record_input(category, tokens) @input_breakdown[category.to_sym] += tokens.to_i end |
#record_output(category, tokens) ⇒ Object
Record output token usage by category.
33 34 35 |
# File 'lib/rubyn_code/observability/token_analytics.rb', line 33 def record_output(category, tokens) @output_breakdown[category.to_sym] += tokens.to_i end |
#record_savings(feature, tokens) ⇒ Object
Record tokens saved by an efficiency feature.
38 39 40 |
# File 'lib/rubyn_code/observability/token_analytics.rb', line 38 def record_savings(feature, tokens) @savings[feature.to_sym] += tokens.to_i end |
#record_turn! ⇒ Object
Increment the turn counter.
43 44 45 |
# File 'lib/rubyn_code/observability/token_analytics.rb', line 43 def record_turn! @turn_count += 1 end |
#report ⇒ Object
Format a complete analytics report.
68 69 70 71 72 73 74 75 76 |
# File 'lib/rubyn_code/observability/token_analytics.rb', line 68 def report(**) lines = [header] lines.concat(input_section) lines << '' lines.concat(output_section) lines << '' lines.concat(savings_section) if @savings.any? lines.join("\n") end |
#session_minutes ⇒ Object
Session duration in minutes.
63 64 65 |
# File 'lib/rubyn_code/observability/token_analytics.rb', line 63 def session_minutes ((Time.now - @start_time) / 60.0).round(1) end |
#total_input_tokens ⇒ Object
Total input tokens across all categories.
48 49 50 |
# File 'lib/rubyn_code/observability/token_analytics.rb', line 48 def total_input_tokens @input_breakdown.values.sum end |
#total_output_tokens ⇒ Object
Total output tokens across all categories.
53 54 55 |
# File 'lib/rubyn_code/observability/token_analytics.rb', line 53 def total_output_tokens @output_breakdown.values.sum end |
#total_tokens_saved ⇒ Object
Total tokens saved across all features.
58 59 60 |
# File 'lib/rubyn_code/observability/token_analytics.rb', line 58 def total_tokens_saved @savings.values.sum end |