Class: ActiveAgent::Delegation::Ledger

Inherits:
Object
  • Object
show all
Defined in:
lib/active_agent/delegation/ledger.rb

Overview

Running tally of what delegated work has consumed.

One ledger is kept per delegation plus one for the agent as a whole, and both live on the agent instance — which is created fresh for every generation. A budget is therefore scoped to a single generation and its entire tool loop, with no cross-request bleed and nothing to reset.

Examples:

Inspecting spend after a generation

agent = ResearchAgent.new
agent.process(:research, topic: "hydrogen storage")
agent.process_prompt
agent.delegation_ledger.to_h
#=> { calls: 2, tokens: 1_840, cost: 0.0004, duration: 3.1 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLedger

Returns a new instance of Ledger.



28
29
30
31
32
33
# File 'lib/active_agent/delegation/ledger.rb', line 28

def initialize
  @calls    = 0
  @tokens   = 0
  @cost     = 0.0
  @duration = 0.0
end

Instance Attribute Details

#callsInteger (readonly)

Returns delegated calls completed.

Returns:

  • (Integer)

    delegated calls completed



20
21
22
# File 'lib/active_agent/delegation/ledger.rb', line 20

def calls
  @calls
end

#costFloat (readonly)

Returns cumulative USD spend (0.0 when no rates are known).

Returns:

  • (Float)

    cumulative USD spend (0.0 when no rates are known)



24
25
26
# File 'lib/active_agent/delegation/ledger.rb', line 24

def cost
  @cost
end

#durationFloat (readonly)

Returns cumulative wall-clock seconds.

Returns:

  • (Float)

    cumulative wall-clock seconds



26
27
28
# File 'lib/active_agent/delegation/ledger.rb', line 26

def duration
  @duration
end

#tokensInteger (readonly)

Returns cumulative total tokens.

Returns:

  • (Integer)

    cumulative total tokens



22
23
24
# File 'lib/active_agent/delegation/ledger.rb', line 22

def tokens
  @tokens
end

Instance Method Details

#record(tokens: 0, cost: nil, duration: 0.0) ⇒ self

Records one delegated call.

Parameters:

  • tokens (Integer, nil) (defaults to: 0)
  • cost (Float, nil) (defaults to: nil)

    nil when the model's rates are unknown

  • duration (Float) (defaults to: 0.0)

    seconds

Returns:

  • (self)


41
42
43
44
45
46
47
48
# File 'lib/active_agent/delegation/ledger.rb', line 41

def record(tokens: 0, cost: nil, duration: 0.0)
  @calls    += 1
  @tokens   += tokens.to_i
  @cost     += cost.to_f
  @duration += duration.to_f

  self
end

#to_hHash

Returns:

  • (Hash)


51
52
53
# File 'lib/active_agent/delegation/ledger.rb', line 51

def to_h
  { calls: calls, tokens: tokens, cost: cost, duration: duration }
end