Class: Llmemory::LLM::TrackingClient

Inherits:
Object
  • Object
show all
Defined in:
lib/llmemory/llm/tracking_client.rb

Overview

Transparent wrapper that records token usage to the per-user ledger.

Instance Method Summary collapse

Constructor Details

#initialize(inner, user_id:, store: nil, api_key: nil) ⇒ TrackingClient

Returns a new instance of TrackingClient.



9
10
11
12
13
14
# File 'lib/llmemory/llm/tracking_client.rb', line 9

def initialize(inner, user_id:, store: nil, api_key: nil)
  @inner = inner
  @user_id = user_id
  @store = store
  @api_key = api_key
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/llmemory/llm/tracking_client.rb', line 44

def method_missing(method, *args, &block)
  if inner_client.respond_to?(method)
    inner_client.public_send(method, *args, &block)
  else
    super
  end
end

Instance Method Details

#invoke(prompt) ⇒ Object



16
17
18
19
20
# File 'lib/llmemory/llm/tracking_client.rb', line 16

def invoke(prompt)
  response = inner_client.invoke(prompt)
  record_usage_from_client(:invoke)
  response
end

#invoke_with_json_schema(prompt, json_schema) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/llmemory/llm/tracking_client.rb', line 22

def invoke_with_json_schema(prompt, json_schema)
  return nil unless structured_output_supported?

  result = nil
  begin
    result = inner_client.invoke_with_json_schema(prompt, json_schema)
    result
  ensure
    record_usage_from_client(:invoke) if structured_output_supported?
  end
end

#last_usageObject



34
35
36
37
38
# File 'lib/llmemory/llm/tracking_client.rb', line 34

def last_usage
  return inner_client.last_usage if inner_client.respond_to?(:last_usage)

  Usage.zero
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/llmemory/llm/tracking_client.rb', line 40

def respond_to?(method, include_private = false)
  inner_client.respond_to?(method, include_private) || super
end