Class: Clacky::Billing::BillingRecord
- Inherits:
-
Struct
- Object
- Struct
- Clacky::Billing::BillingRecord
- 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
-
#cache_read_tokens ⇒ Object
Returns the value of attribute cache_read_tokens.
-
#cache_write_tokens ⇒ Object
Returns the value of attribute cache_write_tokens.
-
#completion_tokens ⇒ Object
Returns the value of attribute completion_tokens.
-
#cost_source ⇒ Object
Returns the value of attribute cost_source.
-
#cost_usd ⇒ Object
Returns the value of attribute cost_usd.
-
#id ⇒ Object
Returns the value of attribute id.
-
#model ⇒ Object
Returns the value of attribute model.
-
#prompt_tokens ⇒ Object
Returns the value of attribute prompt_tokens.
-
#session_id ⇒ Object
Returns the value of attribute session_id.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
Class Method Summary collapse
-
.from_h(hash) ⇒ Object
Create from hash (for deserialization).
-
.parse_timestamp(ts) ⇒ Object
Parse timestamp from string or return as-is if already Time.
Instance Method Summary collapse
-
#to_h ⇒ Object
Convert to hash for JSON serialization.
-
#total_tokens ⇒ Object
Total tokens (input + output).
Instance Attribute Details
#cache_read_tokens ⇒ Object
Returns the value of attribute 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_tokens ⇒ Object
Returns the value of attribute cache_write_tokens
7 8 9 |
# File 'lib/clacky/billing/billing_record.rb', line 7 def cache_write_tokens @cache_write_tokens end |
#completion_tokens ⇒ Object
Returns the value of attribute completion_tokens
7 8 9 |
# File 'lib/clacky/billing/billing_record.rb', line 7 def completion_tokens @completion_tokens end |
#cost_source ⇒ Object
Returns the value of attribute cost_source
7 8 9 |
# File 'lib/clacky/billing/billing_record.rb', line 7 def cost_source @cost_source end |
#cost_usd ⇒ Object
Returns the value of attribute cost_usd
7 8 9 |
# File 'lib/clacky/billing/billing_record.rb', line 7 def cost_usd @cost_usd end |
#id ⇒ Object
Returns the value of attribute id
7 8 9 |
# File 'lib/clacky/billing/billing_record.rb', line 7 def id @id end |
#model ⇒ Object
Returns the value of attribute model
7 8 9 |
# File 'lib/clacky/billing/billing_record.rb', line 7 def model @model end |
#prompt_tokens ⇒ Object
Returns the value of attribute prompt_tokens
7 8 9 |
# File 'lib/clacky/billing/billing_record.rb', line 7 def prompt_tokens @prompt_tokens end |
#session_id ⇒ Object
Returns the value of attribute session_id
7 8 9 |
# File 'lib/clacky/billing/billing_record.rb', line 7 def session_id @session_id end |
#timestamp ⇒ Object
Returns the value of attribute timestamp
7 8 9 |
# File 'lib/clacky/billing/billing_record.rb', line 7 def @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: (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.(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_h ⇒ Object
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: .is_a?(Time) ? .iso8601 : , 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_tokens ⇒ Object
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 |