Class: SwarmCLI::UI::State::UsageTracker
- Inherits:
-
Object
- Object
- SwarmCLI::UI::State::UsageTracker
- Defined in:
- lib/swarm_cli/ui/state/usage_tracker.rb
Overview
Tracks cumulative usage statistics during swarm execution
Instance Attribute Summary collapse
-
#llm_requests ⇒ Object
readonly
Returns the value of attribute llm_requests.
-
#tool_calls ⇒ Object
readonly
Returns the value of attribute tool_calls.
-
#total_cost ⇒ Object
readonly
Returns the value of attribute total_cost.
-
#total_tokens ⇒ Object
readonly
Returns the value of attribute total_tokens.
Instance Method Summary collapse
-
#agents ⇒ Object
Get list of agents seen.
-
#initialize ⇒ UsageTracker
constructor
A new instance of UsageTracker.
-
#reset ⇒ Object
Reset all counters (for testing).
-
#tool_name_for(tool_call_id) ⇒ Object
Get tool name from call ID.
-
#track_agent(agent_name) ⇒ Object
Track agent usage.
-
#track_llm_request(usage_data) ⇒ Object
Track an LLM API call.
-
#track_tool_call(tool_call_id: nil, tool_name: nil) ⇒ Object
Track a tool call.
Constructor Details
#initialize ⇒ UsageTracker
Returns a new instance of UsageTracker.
10 11 12 13 14 15 16 17 |
# File 'lib/swarm_cli/ui/state/usage_tracker.rb', line 10 def initialize @total_cost = 0.0 @total_tokens = 0 @llm_requests = 0 @tool_calls = 0 @agents_seen = Set.new @recent_tool_calls = {} # tool_call_id => tool_name for matching end |
Instance Attribute Details
#llm_requests ⇒ Object (readonly)
Returns the value of attribute llm_requests.
8 9 10 |
# File 'lib/swarm_cli/ui/state/usage_tracker.rb', line 8 def llm_requests @llm_requests end |
#tool_calls ⇒ Object (readonly)
Returns the value of attribute tool_calls.
8 9 10 |
# File 'lib/swarm_cli/ui/state/usage_tracker.rb', line 8 def tool_calls @tool_calls end |
#total_cost ⇒ Object (readonly)
Returns the value of attribute total_cost.
8 9 10 |
# File 'lib/swarm_cli/ui/state/usage_tracker.rb', line 8 def total_cost @total_cost end |
#total_tokens ⇒ Object (readonly)
Returns the value of attribute total_tokens.
8 9 10 |
# File 'lib/swarm_cli/ui/state/usage_tracker.rb', line 8 def total_tokens @total_tokens end |
Instance Method Details
#agents ⇒ Object
Get list of agents seen
41 42 43 |
# File 'lib/swarm_cli/ui/state/usage_tracker.rb', line 41 def agents @agents_seen.to_a end |
#reset ⇒ Object
Reset all counters (for testing)
51 52 53 54 55 56 57 58 |
# File 'lib/swarm_cli/ui/state/usage_tracker.rb', line 51 def reset @total_cost = 0.0 @total_tokens = 0 @llm_requests = 0 @tool_calls = 0 @agents_seen.clear @recent_tool_calls.clear end |
#tool_name_for(tool_call_id) ⇒ Object
Get tool name from call ID
46 47 48 |
# File 'lib/swarm_cli/ui/state/usage_tracker.rb', line 46 def tool_name_for(tool_call_id) @recent_tool_calls[tool_call_id] end |
#track_agent(agent_name) ⇒ Object
Track agent usage
36 37 38 |
# File 'lib/swarm_cli/ui/state/usage_tracker.rb', line 36 def track_agent(agent_name) @agents_seen.add(agent_name) end |
#track_llm_request(usage_data) ⇒ Object
Track an LLM API call
20 21 22 23 24 25 26 27 |
# File 'lib/swarm_cli/ui/state/usage_tracker.rb', line 20 def track_llm_request(usage_data) @llm_requests += 1 if usage_data @total_cost += usage_data[:total_cost] || 0.0 @total_tokens += usage_data[:total_tokens] || 0 end end |
#track_tool_call(tool_call_id: nil, tool_name: nil) ⇒ Object
Track a tool call
30 31 32 33 |
# File 'lib/swarm_cli/ui/state/usage_tracker.rb', line 30 def track_tool_call(tool_call_id: nil, tool_name: nil) @tool_calls += 1 @recent_tool_calls[tool_call_id] = tool_name if tool_call_id && tool_name end |