Class: SwarmSDK::V3::Loop::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_sdk/v3/loop/result.rb

Overview

Aggregate result of a completed loop execution

Contains all iterations and whether the loop converged. Frozen on creation — both the Result and its iterations array are immutable after construction.

Examples:

Converged loop

result = executor.run(kickoff: "Write a poem", iterate: "Improve it", ...)
result.converged?       #=> true
result.iteration_count  #=> 3
result.final_response   #=> RubyLLM::Message
result.total_tokens     #=> { input: 45, output: 60 }

Non-converged loop (hit max_iterations)

result.converged?       #=> false
result.iteration_count  #=> 10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iterations:, converged:) ⇒ Result

Create a new Result

Parameters:

  • iterations (Array<Iteration>)

    Completed iterations

  • converged (Boolean)

    Whether convergence was detected



34
35
36
37
38
# File 'lib/swarm_sdk/v3/loop/result.rb', line 34

def initialize(iterations:, converged:)
  @iterations = iterations.freeze
  @converged = converged
  freeze
end

Instance Attribute Details

#convergedBoolean (readonly) Also known as: converged?

Returns Whether the loop converged below the threshold.

Returns:

  • (Boolean)

    Whether the loop converged below the threshold



27
28
29
# File 'lib/swarm_sdk/v3/loop/result.rb', line 27

def converged
  @converged
end

#iterationsArray<Iteration> (readonly)

Returns All iterations (frozen).

Returns:

  • (Array<Iteration>)

    All iterations (frozen)



24
25
26
# File 'lib/swarm_sdk/v3/loop/result.rb', line 24

def iterations
  @iterations
end

Instance Method Details

#final_responseRubyLLM::Message?

The last iteration’s LLM response

Examples:

puts result.final_response.content

Returns:

  • (RubyLLM::Message, nil)

    Final response, or nil if no iterations



46
47
48
# File 'lib/swarm_sdk/v3/loop/result.rb', line 46

def final_response
  @iterations.last&.response
end

#iteration_countInteger

Number of iterations executed

Examples:

result.iteration_count  #=> 3

Returns:

  • (Integer)


56
57
58
# File 'lib/swarm_sdk/v3/loop/result.rb', line 56

def iteration_count
  @iterations.size
end

#total_tokensHash{Symbol => Integer}

Aggregate token usage across all iterations

Examples:

result.total_tokens  #=> { input: 45, output: 60 }

Returns:

  • (Hash{Symbol => Integer})

    { input:, output: }



66
67
68
69
70
# File 'lib/swarm_sdk/v3/loop/result.rb', line 66

def total_tokens
  input = @iterations.sum { |i| i.tokens[:input] }
  output = @iterations.sum { |i| i.tokens[:output] }
  { input: input, output: output }
end