Class: OllamaAgent::Runtime::CostLedger

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_agent/runtime/cost_ledger.rb

Overview

Persists per-manifest LLM cost rows in runtime.db.

Instance Method Summary collapse

Constructor Details

#initialize(db_registry:) ⇒ CostLedger

Returns a new instance of CostLedger.



7
8
9
# File 'lib/ollama_agent/runtime/cost_ledger.rb', line 7

def initialize(db_registry:)
  @db_registry = db_registry
end

Instance Method Details

#record(manifest_id:, model:, input_tokens:, output_tokens:, cost_usd:, current_epoch:) ⇒ void

This method returns an undefined value.

rubocop:disable Metrics/ParameterLists, Metrics/MethodLength – explicit ledger columns



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ollama_agent/runtime/cost_ledger.rb', line 13

def record(manifest_id:, model:, input_tokens:, output_tokens:, cost_usd:, current_epoch:)
  sql = "INSERT INTO cost_ledger (manifest_id, model, input_tokens, output_tokens, " \
        "cost_usd, created_at_epoch) VALUES (?, ?, ?, ?, ?, ?)"
  binds = [
    manifest_id,
    model.to_s,
    Integer(input_tokens),
    Integer(output_tokens),
    cost_usd.to_f,
    Integer(current_epoch)
  ]
  @db_registry.runtime.execute(sql, binds)
end

#total_for_manifest(manifest_id:) ⇒ Float

Returns:

  • (Float)


29
30
31
32
33
# File 'lib/ollama_agent/runtime/cost_ledger.rb', line 29

def total_for_manifest(manifest_id:)
  sql = "SELECT COALESCE(SUM(cost_usd), 0.0) AS t FROM cost_ledger WHERE manifest_id = ?"
  row = @db_registry.runtime.get_first_row(sql, manifest_id)
  extract_float(row, "t")
end

#total_in_window(since_epoch:, until_epoch:) ⇒ Float

Returns:

  • (Float)


36
37
38
39
40
41
# File 'lib/ollama_agent/runtime/cost_ledger.rb', line 36

def total_in_window(since_epoch:, until_epoch:)
  sql = "SELECT COALESCE(SUM(cost_usd), 0.0) AS t FROM cost_ledger " \
        "WHERE created_at_epoch >= ? AND created_at_epoch <= ?"
  row = @db_registry.runtime.get_first_row(sql, [Integer(since_epoch), Integer(until_epoch)])
  extract_float(row, "t")
end