Class: LLM::Tracer

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

Overview

The LLM::Tracer is the superclass of all LLM tracers. It can be helpful for implementing instrumentation and hooking into the lifecycle of an LLM request. See LLM::Tracer::Telemetry, and LLM::Tracer::Logger for example tracer implementations.

Direct Known Subclasses

Logger, Null, Telemetry

Defined Under Namespace

Classes: Logger, Null, Telemetry

Instance Method Summary collapse

Constructor Details

#initialize(provider, options = {}) ⇒ Tracer

Returns a new instance of Tracer.

Parameters:

  • provider (LLM::Provider)

    A provider

  • options (Hash) (defaults to: {})

    A hash of options



21
22
23
24
# File 'lib/llm/tracer.rb', line 21

def initialize(provider, options = {})
  @provider = provider
  @options = {}
end

Instance Method Details

#flush!nil

Note:

This method is only implemented by the Telemetry tracer. It is a noop for other tracers.

Flush the tracer

Returns:

  • (nil)


110
111
112
# File 'lib/llm/tracer.rb', line 110

def flush!
  nil
end

#on_request_start(operation:, model: nil) ⇒ void

This method returns an undefined value.

Called before an LLM provider request is executed.

Parameters:

  • operation (String)
  • model (String) (defaults to: nil)

Raises:

  • (NotImplementedError)


31
32
33
# File 'lib/llm/tracer.rb', line 31

def on_request_start(operation:, model: nil)
  raise NotImplementedError, "#{self.class} does not implement '#{__method__}'"
end

#on_request_finish(operation:, res:, model: nil, span: nil) ⇒ void

This method returns an undefined value.

Called after an LLM provider request succeeds.

Parameters:

  • operation (String)
  • model (String) (defaults to: nil)
  • res (LLM::Response)
  • span (Object, nil) (defaults to: nil)

Raises:

  • (NotImplementedError)


42
43
44
# File 'lib/llm/tracer.rb', line 42

def on_request_finish(operation:, res:, model: nil, span: nil)
  raise NotImplementedError, "#{self.class} does not implement '#{__method__}'"
end

#on_request_error(ex:, span:) ⇒ void

This method returns an undefined value.

Called when an LLM provider request fails.

Parameters:

Raises:

  • (NotImplementedError)


51
52
53
# File 'lib/llm/tracer.rb', line 51

def on_request_error(ex:, span:)
  raise NotImplementedError, "#{self.class} does not implement '#{__method__}'"
end

#on_tool_start(id:, name:, arguments:, model:) ⇒ void

This method returns an undefined value.

Called before a local tool/function executes.

Parameters:

  • id (String)

    The tool call ID assigned by the model/provider

  • name (String)

    The tool (function) name.

  • arguments (Hash)

    The parsed tool arguments.

  • model (String)

    The model name

Raises:

  • (NotImplementedError)


66
67
68
# File 'lib/llm/tracer.rb', line 66

def on_tool_start(id:, name:, arguments:, model:)
  raise NotImplementedError, "#{self.class} does not implement '#{__method__}'"
end

#on_tool_finish(result:, span:) ⇒ void

This method returns an undefined value.

Called after a local tool/function succeeds.

Parameters:

Raises:

  • (NotImplementedError)


77
78
79
# File 'lib/llm/tracer.rb', line 77

def on_tool_finish(result:, span:)
  raise NotImplementedError, "#{self.class} does not implement '#{__method__}'"
end

#on_tool_error(ex:, span:) ⇒ void

This method returns an undefined value.

Called when a local tool/function raises.

Parameters:

  • ex (Exception)

    The raised error.

  • span (Object, nil)

    The span/context object returned by #on_tool_start.

Raises:

  • (NotImplementedError)


88
89
90
# File 'lib/llm/tracer.rb', line 88

def on_tool_error(ex:, span:)
  raise NotImplementedError, "#{self.class} does not implement '#{__method__}'"
end

#inspectString

Returns:

  • (String)


94
95
96
# File 'lib/llm/tracer.rb', line 94

def inspect
  "#<#{self.class.name}:0x#{object_id.to_s(16)} @provider=#{@provider.class} @tracer=#{@tracer.inspect}>"
end

#spansArray

Returns:

  • (Array)


100
101
102
# File 'lib/llm/tracer.rb', line 100

def spans
  []
end