Class: Payloop::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/payloop/client.rb

Overview

Main Payloop client for tracking AI costs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, collector_url: nil, api_url: nil, timeout: nil) ⇒ Client

Returns a new instance of Client.

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/payloop/client.rb', line 10

def initialize(api_key: nil, collector_url: nil, api_url: nil, timeout: nil)
  api_key ||= ENV.fetch("PAYLOOP_API_KEY", nil)
  raise MissingAPIKeyError if api_key.nil? || api_key.empty?

  @config = Config.new(
    api_key: api_key,
    collector_url: collector_url,
    api_url: api_url,
    timeout: timeout
  )
  @collector = Collector.new(@config)
  @sentinel = Sentinel.new(@config)
end

Instance Attribute Details

#collectorObject (readonly)

Returns the value of attribute collector.



8
9
10
# File 'lib/payloop/client.rb', line 8

def collector
  @collector
end

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/payloop/client.rb', line 8

def config
  @config
end

#sentinelObject (readonly)

Returns the value of attribute sentinel.



8
9
10
# File 'lib/payloop/client.rb', line 8

def sentinel
  @sentinel
end

Instance Method Details

#anthropicObject

Anthropic provider wrapper



30
31
32
# File 'lib/payloop/client.rb', line 30

def anthropic
  @anthropic ||= Wrappers::Anthropic.new(@config, @collector, @sentinel)
end

#attribution(parent_id:, parent_name: nil, subsidiary_id: nil, subsidiary_name: nil) ⇒ Object

Set attribution for cost tracking



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/payloop/client.rb', line 63

def attribution(parent_id:, parent_name: nil, subsidiary_id: nil, subsidiary_name: nil)
  attr = Attribution.new(
    parent_id: parent_id,
    parent_name: parent_name,
    subsidiary_id: subsidiary_id,
    subsidiary_name: subsidiary_name
  )

  @config.attribution = attr
  self
end

#geminiaiObject

Google GenAI provider wrapper (Tied to gemini-ai Gem version 4.3.0) Google doesn’t have an official Ruby library, so this is seems to be the most popular unofficial one.



45
46
47
# File 'lib/payloop/client.rb', line 45

def geminiai
  @geminiai ||= Wrappers::GeminiAI.new(@config, @collector, @sentinel)
end

#googleObject

Google GenAI provider wrapper (Tied to google-genai Gem) This is a port of the official Python library, but it does not support system prompts which is used for sentinel. This still works for non-sentinel use cases.



38
39
40
# File 'lib/payloop/client.rb', line 38

def google
  @google ||= Wrappers::Google.new(@config, @collector, @sentinel)
end

#groqObject

Groq provider wrapper (groq gem, drnic/groq-ruby) Groq hosts third-party models (Meta Llama, OpenAI gpt-oss, Qwen, etc.). Telemetry sets provider=“groq” and title from the model-ID prefix.



58
59
60
# File 'lib/payloop/client.rb', line 58

def groq
  @groq ||= Wrappers::Groq.new(@config, @collector, @sentinel)
end

#new_transactionObject

Start a new transaction



76
77
78
79
# File 'lib/payloop/client.rb', line 76

def new_transaction
  @config.new_transaction
  self
end

#openaiObject

OpenAI provider wrapper



25
26
27
# File 'lib/payloop/client.rb', line 25

def openai
  @openai ||= Wrappers::OpenAI.new(@config, @collector, @sentinel)
end

#ruby_llmObject

RubyLLM provider wrapper (ruby_llm gem) Supports any provider available via RubyLLM (Anthropic, OpenAI, Google, etc.)



51
52
53
# File 'lib/payloop/client.rb', line 51

def ruby_llm
  @ruby_llm ||= Wrappers::RubyLLM.new(@config, @collector, @sentinel)
end

#shutdownObject

Gracefully shutdown the client



91
92
93
# File 'lib/payloop/client.rb', line 91

def shutdown
  @collector.shutdown
end

#workflowsObject

Get workflows API client



82
83
84
85
86
87
88
# File 'lib/payloop/client.rb', line 82

def workflows
  @workflows ||= API::Workflows.new(
    @config.api_url,
    @config.api_key,
    @config.timeout
  )
end