Module: Langsmith

Defined in:
lib/langsmith.rb,
lib/langsmith/run.rb,
lib/langsmith/client.rb,
lib/langsmith/errors.rb,
lib/langsmith/context.rb,
lib/langsmith/railtie.rb,
lib/langsmith/version.rb,
lib/langsmith/run_tree.rb,
lib/langsmith/evaluation.rb,
lib/langsmith/configuration.rb,
lib/langsmith/batch_processor.rb,
lib/langsmith/evaluation/experiment_runner.rb

Defined Under Namespace

Modules: Context, Evaluation Classes: BatchProcessor, Client, Configuration, ConfigurationError, Error, Railtie, Run, RunTree, TracingError

Constant Summary collapse

VERSION =
"0.4.0"

Class Method Summary collapse

Class Method Details

.batch_processorBatchProcessor

Returns the batch processor (lazily initialized).

Returns:



53
54
55
# File 'lib/langsmith.rb', line 53

def batch_processor
  @batch_processor ||= BatchProcessor.new
end

.clientClient

Returns the HTTP client (lazily initialized).

Returns:



59
60
61
# File 'lib/langsmith.rb', line 59

def client
  @client ||= Client.new
end

.configurationConfiguration

Returns the current configuration.

Returns:



17
18
19
# File 'lib/langsmith.rb', line 17

def configuration
  @configuration ||= Configuration.new
end

.configure {|Configuration| ... } ⇒ void

This method returns an undefined value.

Configure Langsmith with a block.

Examples:

Langsmith.configure do |config|
  config.api_key = "ls_..."
  config.tracing_enabled = true
  config.project = "my-project"
end

Yields:



32
33
34
35
# File 'lib/langsmith.rb', line 32

def configure
  yield(configuration)
  configuration.validate!
end

.current_runRun?

Get the current run from context (if any).

Returns:



131
132
133
# File 'lib/langsmith.rb', line 131

def current_run
  Context.current_run
end

.flushvoid

This method returns an undefined value.

Flush all pending traces (blocking). Useful before application shutdown or in tests.



119
120
121
# File 'lib/langsmith.rb', line 119

def flush
  batch_processor.flush
end

.reset_configuration!void

This method returns an undefined value.

Reset configuration (useful for testing).



39
40
41
42
43
# File 'lib/langsmith.rb', line 39

def reset_configuration!
  @configuration = Configuration.new
  @batch_processor = nil
  @client = nil
end

.shutdownvoid

This method returns an undefined value.

Shutdown the batch processor gracefully.



125
126
127
# File 'lib/langsmith.rb', line 125

def shutdown
  batch_processor.shutdown
end

.trace(name, run_type: "chain", inputs: nil, metadata: nil, tags: nil, extra: nil, tenant_id: nil, project: nil) {|Run| ... } ⇒ Object

Main tracing API - execute a block within a traced context.

Examples:

Basic tracing

Langsmith.trace("my_operation", run_type: "chain") do |run|
  run.(user_id: "123")
  result = do_something()
  result
end

Nested traces

Langsmith.trace("parent", run_type: "chain") do
  Langsmith.trace("child", run_type: "llm") do
    call_llm()
  end
end

Multi-tenant tracing

Langsmith.trace("operation", tenant_id: "tenant-123") do |run|
  # This trace goes to tenant-123
end

Project-specific tracing

Langsmith.trace("operation", project: "my-special-project") do |run|
  # This trace goes to my-special-project
end

Parameters:

  • name (String)

    Name of the operation being traced

  • run_type (String) (defaults to: "chain")

    Type of run ("chain", "llm", "tool", etc.)

  • inputs (Hash) (defaults to: nil)

    Input data for the trace

  • metadata (Hash) (defaults to: nil)

    Additional metadata

  • tags (Array<String>) (defaults to: nil)

    Tags for filtering

  • extra (Hash) (defaults to: nil)

    Extra data (e.g., token usage)

  • tenant_id (String) (defaults to: nil)

    Tenant ID for multi-tenant scenarios (overrides global config)

  • project (String) (defaults to: nil)

    Project name for this trace (overrides global config)

Yields:

  • (Run)

    the run object for adding metadata, events, etc.

Returns:

  • (Object)

    the return value of the block



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/langsmith.rb', line 100

def trace(name, run_type: "chain", inputs: nil, metadata: nil, tags: nil, extra: nil, tenant_id: nil, project: nil,
          &block)
  run_tree = RunTree.new(
    name: name,
    run_type: run_type,
    inputs: inputs,
    metadata: ,
    tags: tags,
    extra: extra,
    tenant_id: tenant_id,
    project: project
  )

  run_tree.execute(&block)
end

.tracing?Boolean

Check if we're currently inside a trace.

Returns:

  • (Boolean)


137
138
139
# File 'lib/langsmith.rb', line 137

def tracing?
  Context.active?
end

.tracing_enabled?Boolean

Check if tracing is enabled and possible (has API key).

Returns:

  • (Boolean)


47
48
49
# File 'lib/langsmith.rb', line 47

def tracing_enabled?
  configuration.tracing_possible?
end