Class: SwarmSDK::V3::Loop::Iteration

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

Overview

Immutable record of a single loop iteration

Captures the prompt sent, the LLM response received, token usage, and the embedding-similarity delta from the previous iteration. Frozen on creation to prevent accidental mutation.

Examples:

First iteration (no delta)

iteration = Iteration.new(
  number: 1,
  response: response,
  prompt: "Write a poem",
  tokens: { input: 10, output: 20 },
  delta_score: nil,
)

Subsequent iteration with convergence score

iteration = Iteration.new(
  number: 2,
  response: response,
  prompt: "Improve the poem",
  tokens: { input: 15, output: 25 },
  delta_score: 0.87,
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number:, response:, prompt:, tokens:, delta_score:) ⇒ Iteration

Create a new Iteration record

Parameters:

  • number (Integer)

    1-indexed iteration number

  • response (RubyLLM::Message)

    LLM response

  • prompt (String)

    Prompt used

  • tokens (Hash{Symbol => Integer})

    Token usage { input:, output: }

  • delta_score (Float, nil)

    Similarity to previous iteration



52
53
54
55
56
57
58
59
# File 'lib/swarm_sdk/v3/loop/iteration.rb', line 52

def initialize(number:, response:, prompt:, tokens:, delta_score:)
  @number = number
  @response = response
  @prompt = prompt
  @tokens = tokens.freeze
  @delta_score = delta_score
  freeze
end

Instance Attribute Details

#delta_scoreFloat? (readonly)

Returns Cosine similarity to previous iteration (nil for iteration 1).

Returns:

  • (Float, nil)

    Cosine similarity to previous iteration (nil for iteration 1)



43
44
45
# File 'lib/swarm_sdk/v3/loop/iteration.rb', line 43

def delta_score
  @delta_score
end

#numberInteger (readonly)

Returns 1-indexed iteration number.

Returns:

  • (Integer)

    1-indexed iteration number



31
32
33
# File 'lib/swarm_sdk/v3/loop/iteration.rb', line 31

def number
  @number
end

#promptString (readonly)

Returns Prompt used for this iteration (kickoff or iterate).

Returns:

  • (String)

    Prompt used for this iteration (kickoff or iterate)



37
38
39
# File 'lib/swarm_sdk/v3/loop/iteration.rb', line 37

def prompt
  @prompt
end

#responseRubyLLM::Message (readonly)

Returns LLM response from ask().

Returns:

  • (RubyLLM::Message)

    LLM response from ask()



34
35
36
# File 'lib/swarm_sdk/v3/loop/iteration.rb', line 34

def response
  @response
end

#tokensHash{Symbol => Integer} (readonly)

Returns Token usage { input:, output: }.

Returns:

  • (Hash{Symbol => Integer})

    Token usage { input:, output: }



40
41
42
# File 'lib/swarm_sdk/v3/loop/iteration.rb', line 40

def tokens
  @tokens
end