Class: Langfuse::Agent

Inherits:
BaseObservation show all
Defined in:
lib/langfuse/observations.rb

Overview

Observation for tracking agent-based workflows that make decisions and use tools.

Examples:

Block-based API

Langfuse.observe("agent-workflow", as_type: :agent) do |agent|
  agent.input = { task: "Find weather for NYC" }
  # Agent makes decisions and uses tools
  agent.start_observation("tool-call", { tool_name: "weather_api" }, as_type: :tool) do |tool|
    weather = fetch_weather("NYC")
    tool.update(output: weather)
  end
  agent.update(output: { result: "Sunny, 72°F" })
end

Stateful API

agent = Langfuse.start_observation("agent-workflow", {
  input: { task: "Research topic" }
}, as_type: :agent)
# Agent logic here
agent.update(output: { result: "Research complete" })
agent.end

Instance Attribute Summary

Attributes inherited from BaseObservation

#otel_span, #otel_tracer, #type

Instance Method Summary collapse

Methods inherited from BaseObservation

#current_span, #end, #event, #id, #input=, #level=, #metadata=, #output=, #score_trace, #start_observation, #trace_id, #trace_url, #update_trace

Constructor Details

#initialize(otel_span, otel_tracer, attributes: nil) ⇒ Agent

Returns a new instance of Agent.

Parameters:

  • otel_span (OpenTelemetry::SDK::Trace::Span)

    The underlying OTel span

  • otel_tracer (OpenTelemetry::SDK::Trace::Tracer)

    The OTel tracer

  • attributes (Hash, Types::SpanAttributes, nil) (defaults to: nil)

    Optional initial attributes



432
433
434
# File 'lib/langfuse/observations.rb', line 432

def initialize(otel_span, otel_tracer, attributes: nil)
  super(otel_span, otel_tracer, attributes: attributes, type: OBSERVATION_TYPES[:agent])
end

Instance Method Details

#update(attrs) ⇒ self

Parameters:

Returns:

  • (self)


438
439
440
441
# File 'lib/langfuse/observations.rb', line 438

def update(attrs)
  update_observation_attributes(attrs)
  self
end