Class: SwarmCLI::UI::State::UsageTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_cli/ui/state/usage_tracker.rb

Overview

Tracks cumulative usage statistics during swarm execution

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUsageTracker

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_requestsObject (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_callsObject (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_costObject (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_tokensObject (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

#agentsObject

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

#resetObject

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