Class: Clacky::Billing::BillingRecord

Inherits:
Struct
  • Object
show all
Defined in:
lib/clacky/billing/billing_record.rb

Overview

Data structure for a single billing record Each API call generates one record with token usage and cost

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cache_read_tokensObject

Returns the value of attribute cache_read_tokens

Returns:

  • (Object)

    the current value of cache_read_tokens



7
8
9
# File 'lib/clacky/billing/billing_record.rb', line 7

def cache_read_tokens
  @cache_read_tokens
end

#cache_write_tokensObject

Returns the value of attribute cache_write_tokens

Returns:

  • (Object)

    the current value of cache_write_tokens



7
8
9
# File 'lib/clacky/billing/billing_record.rb', line 7

def cache_write_tokens
  @cache_write_tokens
end

#completion_tokensObject

Returns the value of attribute completion_tokens

Returns:

  • (Object)

    the current value of completion_tokens



7
8
9
# File 'lib/clacky/billing/billing_record.rb', line 7

def completion_tokens
  @completion_tokens
end

#cost_sourceObject

Returns the value of attribute cost_source

Returns:

  • (Object)

    the current value of cost_source



7
8
9
# File 'lib/clacky/billing/billing_record.rb', line 7

def cost_source
  @cost_source
end

#cost_usdObject

Returns the value of attribute cost_usd

Returns:

  • (Object)

    the current value of cost_usd



7
8
9
# File 'lib/clacky/billing/billing_record.rb', line 7

def cost_usd
  @cost_usd
end

#idObject

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



7
8
9
# File 'lib/clacky/billing/billing_record.rb', line 7

def id
  @id
end

#modelObject

Returns the value of attribute model

Returns:

  • (Object)

    the current value of model



7
8
9
# File 'lib/clacky/billing/billing_record.rb', line 7

def model
  @model
end

#prompt_tokensObject

Returns the value of attribute prompt_tokens

Returns:

  • (Object)

    the current value of prompt_tokens



7
8
9
# File 'lib/clacky/billing/billing_record.rb', line 7

def prompt_tokens
  @prompt_tokens
end

#session_idObject

Returns the value of attribute session_id

Returns:

  • (Object)

    the current value of session_id



7
8
9
# File 'lib/clacky/billing/billing_record.rb', line 7

def session_id
  @session_id
end

#timestampObject

Returns the value of attribute timestamp

Returns:

  • (Object)

    the current value of timestamp



7
8
9
# File 'lib/clacky/billing/billing_record.rb', line 7

def timestamp
  @timestamp
end

Class Method Details

.from_h(hash) ⇒ Object

Create from hash (for deserialization)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/clacky/billing/billing_record.rb', line 37

def self.from_h(hash)
  new(
    id: hash[:id] || hash["id"],
    session_id: hash[:session_id] || hash["session_id"],
    timestamp: parse_timestamp(hash[:timestamp] || hash["timestamp"]),
    model: hash[:model] || hash["model"],
    prompt_tokens: hash[:prompt_tokens] || hash["prompt_tokens"] || 0,
    completion_tokens: hash[:completion_tokens] || hash["completion_tokens"] || 0,
    cache_read_tokens: hash[:cache_read_tokens] || hash["cache_read_tokens"] || 0,
    cache_write_tokens: hash[:cache_write_tokens] || hash["cache_write_tokens"] || 0,
    cost_usd: hash[:cost_usd] || hash["cost_usd"] || 0.0,
    cost_source: (hash[:cost_source] || hash["cost_source"])&.to_sym
  )
end

.parse_timestamp(ts) ⇒ Object

Parse timestamp from string or return as-is if already Time



53
54
55
56
57
58
59
# File 'lib/clacky/billing/billing_record.rb', line 53

def self.parse_timestamp(ts)
  return ts if ts.is_a?(Time)
  return Time.now if ts.nil?
  Time.parse(ts)
rescue
  Time.now
end

Instance Method Details

#to_hObject

Convert to hash for JSON serialization



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/clacky/billing/billing_record.rb', line 21

def to_h
  {
    id: id,
    session_id: session_id,
    timestamp: timestamp.is_a?(Time) ? timestamp.iso8601 : timestamp,
    model: model,
    prompt_tokens: prompt_tokens || 0,
    completion_tokens: completion_tokens || 0,
    cache_read_tokens: cache_read_tokens || 0,
    cache_write_tokens: cache_write_tokens || 0,
    cost_usd: cost_usd || 0.0,
    cost_source: cost_source&.to_s
  }
end

#total_tokensObject

Total tokens (input + output)



62
63
64
# File 'lib/clacky/billing/billing_record.rb', line 62

def total_tokens
  (prompt_tokens || 0) + (completion_tokens || 0)
end