Module: RubyLLM::Agents::Execution::Replayable

Extended by:
ActiveSupport::Concern
Included in:
RubyLLM::Agents::Execution
Defined in:
app/models/ruby_llm/agents/execution/replayable.rb

Overview

Adds replay capability to execution records.

Allows re-executing a previous run with the same inputs, or with tweaked parameters for A/B testing and debugging.

Examples:

Replay with same settings

run = SupportAgent.last_run
new_run = run.replay

Replay with different model

run.replay(model: "claude-sonnet-4-6")

Compare two models

run1 = SupportAgent.last_run
run2 = run1.replay(model: "gpt-4o-mini")
puts "Original: #{run1.total_cost} | Replay: #{run2.total_cost}"

Instance Method Summary collapse

Instance Method Details

#replay(model: nil, temperature: nil, **overrides) ⇒ Object

Re-executes this agent run with the same (or overridden) inputs.

Loads the original agent class, reconstructs its parameters from the execution detail record, and executes through the full pipeline. The new execution is tracked separately and linked via metadata.

Parameters:

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

    Override the model

  • temperature (Float, nil) (defaults to: nil)

    Override the temperature

  • overrides (Hash)

    Additional parameter overrides

Returns:

  • (Object)

    The result from the new execution

Raises:

  • (ReplayError)

    If the agent class cannot be resolved or detail is missing



39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/ruby_llm/agents/execution/replayable.rb', line 39

def replay(model: nil, temperature: nil, **overrides)
  validate_replayable!
  agent_klass = resolve_agent_class
  params = build_replay_params(overrides)

  opts = params.merge(_replay_source_id: id)
  opts[:model] = model if model
  opts[:temperature] = temperature if temperature

  agent_klass.call(**opts)
end

#replay?Boolean

Returns whether this execution is a replay of another.

Returns:

  • (Boolean)


88
89
90
# File 'app/models/ruby_llm/agents/execution/replayable.rb', line 88

def replay?
  &.dig("replay_source_id").present?
end

#replay_sourceRubyLLM::Agents::Execution?

Returns the original execution this was replayed from.

Returns:



77
78
79
80
81
82
# File 'app/models/ruby_llm/agents/execution/replayable.rb', line 77

def replay_source
  source_id = &.dig("replay_source_id")
  return nil unless source_id

  self.class.find_by(id: source_id)
end

#replayable?Boolean

Returns whether this execution can be replayed.

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
# File 'app/models/ruby_llm/agents/execution/replayable.rb', line 55

def replayable?
  return false if agent_type.blank?
  return false if detail.nil?

  resolve_agent_class
  true
rescue
  false
end

#replaysActiveRecord::Relation

Returns all executions that are replays of this one.

Returns:

  • (ActiveRecord::Relation)


69
70
71
# File 'app/models/ruby_llm/agents/execution/replayable.rb', line 69

def replays
  self.class.replays_of(id)
end