Class: RubyLLM::Agents::Result
- Inherits:
-
Object
- Object
- RubyLLM::Agents::Result
- Extended by:
- ActiveSupport::Delegation
- Defined in:
- lib/ruby_llm/agents/results/base.rb
Overview
Wrapper for agent execution results with full metadata
Provides access to the response content along with execution details like token usage, cost, timing, and model information.
Direct Known Subclasses
Token Usage collapse
- #cache_creation_tokens ⇒ Object readonly
-
#cached_tokens ⇒ Integer
readonly
Number of tokens served from cache.
-
#input_tokens ⇒ Integer?
readonly
Number of input tokens consumed.
-
#output_tokens ⇒ Integer?
readonly
Number of output tokens generated.
Cost collapse
-
#input_cost ⇒ Float?
readonly
Cost of input tokens in USD.
-
#output_cost ⇒ Float?
readonly
Cost of output tokens in USD.
- #total_cost ⇒ Object readonly
Model Info collapse
-
#chosen_model_id ⇒ String?
readonly
The model that actually responded (may differ if fallback used).
-
#model_id ⇒ String?
readonly
The model that was requested.
- #temperature ⇒ Object readonly
Timing collapse
-
#completed_at ⇒ Time?
readonly
When execution completed.
-
#duration_ms ⇒ Integer?
readonly
Execution duration in milliseconds.
-
#started_at ⇒ Time?
readonly
When execution started.
- #time_to_first_token_ms ⇒ Object readonly
Status collapse
-
#finish_reason ⇒ String?
readonly
Why generation stopped (stop, length, tool_calls, etc.).
- #streaming ⇒ Object readonly
Error Info collapse
-
#error_class ⇒ String?
readonly
Exception class name if failed.
- #error_message ⇒ Object readonly
Reliability collapse
-
#attempts ⇒ Array<Hash>
readonly
Details of each attempt (for retries/fallbacks).
- #attempts_count ⇒ Object readonly
Tool Calls collapse
-
#tool_calls ⇒ Array<Hash>
readonly
Tool calls made during execution.
- #tool_calls_count ⇒ Object readonly
Thinking collapse
-
#thinking_signature ⇒ String?
readonly
Signature for multi-turn thinking continuity (Claude).
-
#thinking_text ⇒ String?
readonly
The reasoning/thinking content from the model.
- #thinking_tokens ⇒ Object readonly
Execution Record collapse
- #execution_id ⇒ Object readonly
Tracking collapse
- #agent_class_name ⇒ Object readonly
Cancellation collapse
- #cancelled ⇒ Object readonly
Debug collapse
- #trace ⇒ Object readonly
Instance Attribute Summary collapse
- #content ⇒ Object readonly
Debug collapse
-
#cancelled? ⇒ Boolean
Returns whether the execution was cancelled.
-
#error? ⇒ Boolean
Returns whether the execution failed.
-
#execution ⇒ RubyLLM::Agents::Execution?
Loads the associated Execution record from the database.
-
#initialize(content:, **options) ⇒ Result
constructor
Creates a new Result instance.
-
#streaming? ⇒ Boolean
Returns whether streaming was enabled.
-
#success? ⇒ Boolean
Returns whether the execution succeeded.
-
#thinking? ⇒ Boolean
(also: #has_thinking?)
Returns whether thinking data is present in the result.
-
#to_h ⇒ Hash
Converts the result to a hash.
-
#tool_calls? ⇒ Boolean
(also: #has_tool_calls?)
Returns whether tool calls were made during execution.
-
#total_tokens ⇒ Integer
Returns total tokens (input + output).
-
#truncated? ⇒ Boolean
Returns whether the response was truncated due to max tokens.
-
#used_fallback? ⇒ Boolean
Returns whether a fallback model was used.
Deprecated Hash Delegation collapse
-
#[](key) ⇒ Object
deprecated
Deprecated.
Use result.content instead
-
#dig(*keys) ⇒ Object
deprecated
Deprecated.
Use result.content.dig(…) instead
-
#each(&block) ⇒ Object
deprecated
Deprecated.
Use result.content.each instead
-
#keys ⇒ Object
deprecated
Deprecated.
Use result.content.keys instead
-
#map(&block) ⇒ Object
deprecated
Deprecated.
Use result.content.map instead
-
#values ⇒ Object
deprecated
Deprecated.
Use result.content.values instead
Instance Method Summary collapse
-
#to_json(*args) ⇒ String
Custom to_json that returns content as JSON for backward compatibility.
Constructor Details
#initialize(content:, **options) ⇒ Result
Creates a new Result instance
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/ruby_llm/agents/results/base.rb', line 129 def initialize(content:, **) @content = content # Token usage @input_tokens = [:input_tokens] @output_tokens = [:output_tokens] @cached_tokens = [:cached_tokens] || 0 @cache_creation_tokens = [:cache_creation_tokens] || 0 # Cost @input_cost = [:input_cost] @output_cost = [:output_cost] @total_cost = [:total_cost] # Model info @model_id = [:model_id] @chosen_model_id = [:chosen_model_id] || [:model_id] @temperature = [:temperature] # Timing @started_at = [:started_at] @completed_at = [:completed_at] @duration_ms = [:duration_ms] @time_to_first_token_ms = [:time_to_first_token_ms] # Status @finish_reason = [:finish_reason] @streaming = [:streaming] || false # Error @error_class = [:error_class] @error_message = [:error_message] # Reliability @attempts = [:attempts] || [] @attempts_count = [:attempts_count] || 1 # Tool calls @tool_calls = [:tool_calls] || [] @tool_calls_count = [:tool_calls_count] || 0 # Thinking @thinking_text = [:thinking_text] @thinking_signature = [:thinking_signature] @thinking_tokens = [:thinking_tokens] # Execution record @execution_id = [:execution_id] # Tracking @agent_class_name = [:agent_class_name] # Cancellation @cancelled = [:cancelled] || false # Debug trace @trace = [:trace] # Register with active tracker tracker = Thread.current[:ruby_llm_agents_tracker] tracker << self if tracker end |
Instance Attribute Details
#agent_class_name ⇒ Object (readonly)
113 114 115 |
# File 'lib/ruby_llm/agents/results/base.rb', line 113 def agent_class_name @agent_class_name end |
#attempts ⇒ Array<Hash> (readonly)
Returns Details of each attempt (for retries/fallbacks).
87 88 89 |
# File 'lib/ruby_llm/agents/results/base.rb', line 87 def attempts @attempts end |
#attempts_count ⇒ Object (readonly)
87 |
# File 'lib/ruby_llm/agents/results/base.rb', line 87 attr_reader :attempts, :attempts_count |
#cache_creation_tokens ⇒ Object (readonly)
37 |
# File 'lib/ruby_llm/agents/results/base.rb', line 37 attr_reader :input_tokens, :output_tokens, :cached_tokens, :cache_creation_tokens |
#cached_tokens ⇒ Integer (readonly)
Returns Number of tokens served from cache.
37 |
# File 'lib/ruby_llm/agents/results/base.rb', line 37 attr_reader :input_tokens, :output_tokens, :cached_tokens, :cache_creation_tokens |
#cancelled ⇒ Object (readonly)
118 119 120 |
# File 'lib/ruby_llm/agents/results/base.rb', line 118 def cancelled @cancelled end |
#chosen_model_id ⇒ String? (readonly)
Returns The model that actually responded (may differ if fallback used).
55 |
# File 'lib/ruby_llm/agents/results/base.rb', line 55 attr_reader :model_id, :chosen_model_id, :temperature |
#completed_at ⇒ Time? (readonly)
Returns When execution completed.
66 |
# File 'lib/ruby_llm/agents/results/base.rb', line 66 attr_reader :started_at, :completed_at, :duration_ms, :time_to_first_token_ms |
#content ⇒ Object (readonly)
26 27 28 |
# File 'lib/ruby_llm/agents/results/base.rb', line 26 def content @content end |
#duration_ms ⇒ Integer? (readonly)
Returns Execution duration in milliseconds.
66 |
# File 'lib/ruby_llm/agents/results/base.rb', line 66 attr_reader :started_at, :completed_at, :duration_ms, :time_to_first_token_ms |
#error_class ⇒ String? (readonly)
Returns Exception class name if failed.
80 81 82 |
# File 'lib/ruby_llm/agents/results/base.rb', line 80 def error_class @error_class end |
#error_message ⇒ Object (readonly)
80 |
# File 'lib/ruby_llm/agents/results/base.rb', line 80 attr_reader :error_class, :error_message |
#execution_id ⇒ Object (readonly)
108 109 110 |
# File 'lib/ruby_llm/agents/results/base.rb', line 108 def execution_id @execution_id end |
#finish_reason ⇒ String? (readonly)
Returns Why generation stopped (stop, length, tool_calls, etc.).
73 74 75 |
# File 'lib/ruby_llm/agents/results/base.rb', line 73 def finish_reason @finish_reason end |
#input_cost ⇒ Float? (readonly)
Returns Cost of input tokens in USD.
46 47 48 |
# File 'lib/ruby_llm/agents/results/base.rb', line 46 def input_cost @input_cost end |
#input_tokens ⇒ Integer? (readonly)
Returns Number of input tokens consumed.
37 38 39 |
# File 'lib/ruby_llm/agents/results/base.rb', line 37 def input_tokens @input_tokens end |
#model_id ⇒ String? (readonly)
Returns The model that was requested.
55 56 57 |
# File 'lib/ruby_llm/agents/results/base.rb', line 55 def model_id @model_id end |
#output_cost ⇒ Float? (readonly)
Returns Cost of output tokens in USD.
46 |
# File 'lib/ruby_llm/agents/results/base.rb', line 46 attr_reader :input_cost, :output_cost, :total_cost |
#output_tokens ⇒ Integer? (readonly)
Returns Number of output tokens generated.
37 |
# File 'lib/ruby_llm/agents/results/base.rb', line 37 attr_reader :input_tokens, :output_tokens, :cached_tokens, :cache_creation_tokens |
#started_at ⇒ Time? (readonly)
Returns When execution started.
66 67 68 |
# File 'lib/ruby_llm/agents/results/base.rb', line 66 def started_at @started_at end |
#streaming ⇒ Object (readonly)
73 |
# File 'lib/ruby_llm/agents/results/base.rb', line 73 attr_reader :finish_reason, :streaming |
#temperature ⇒ Object (readonly)
55 |
# File 'lib/ruby_llm/agents/results/base.rb', line 55 attr_reader :model_id, :chosen_model_id, :temperature |
#thinking_signature ⇒ String? (readonly)
Returns Signature for multi-turn thinking continuity (Claude).
103 |
# File 'lib/ruby_llm/agents/results/base.rb', line 103 attr_reader :thinking_text, :thinking_signature, :thinking_tokens |
#thinking_text ⇒ String? (readonly)
Returns The reasoning/thinking content from the model.
103 104 105 |
# File 'lib/ruby_llm/agents/results/base.rb', line 103 def thinking_text @thinking_text end |
#thinking_tokens ⇒ Object (readonly)
103 |
# File 'lib/ruby_llm/agents/results/base.rb', line 103 attr_reader :thinking_text, :thinking_signature, :thinking_tokens |
#time_to_first_token_ms ⇒ Object (readonly)
66 |
# File 'lib/ruby_llm/agents/results/base.rb', line 66 attr_reader :started_at, :completed_at, :duration_ms, :time_to_first_token_ms |
#tool_calls ⇒ Array<Hash> (readonly)
Returns Tool calls made during execution.
94 95 96 |
# File 'lib/ruby_llm/agents/results/base.rb', line 94 def tool_calls @tool_calls end |
#tool_calls_count ⇒ Object (readonly)
94 |
# File 'lib/ruby_llm/agents/results/base.rb', line 94 attr_reader :tool_calls, :tool_calls_count |
#total_cost ⇒ Object (readonly)
46 |
# File 'lib/ruby_llm/agents/results/base.rb', line 46 attr_reader :input_cost, :output_cost, :total_cost |
#trace ⇒ Object (readonly)
123 124 125 |
# File 'lib/ruby_llm/agents/results/base.rb', line 123 def trace @trace end |
Instance Method Details
#[](key) ⇒ Object
Use result.content instead
326 327 328 329 330 331 332 |
# File 'lib/ruby_llm/agents/results/base.rb', line 326 def [](key) RubyLLM::Agents::Deprecations.warn( "Result#[] is deprecated. Use result.content[:key] instead.", caller ) content&.[](key) end |
#cancelled? ⇒ Boolean
Returns whether the execution was cancelled
236 237 238 |
# File 'lib/ruby_llm/agents/results/base.rb', line 236 def cancelled? cancelled == true end |
#dig(*keys) ⇒ Object
Use result.content.dig(…) instead
335 336 337 338 339 340 341 |
# File 'lib/ruby_llm/agents/results/base.rb', line 335 def dig(*keys) RubyLLM::Agents::Deprecations.warn( "Result#dig is deprecated. Use result.content.dig(...) instead.", caller ) content&.dig(*keys) end |
#each(&block) ⇒ Object
Use result.content.each instead
362 363 364 365 366 367 368 |
# File 'lib/ruby_llm/agents/results/base.rb', line 362 def each(&block) RubyLLM::Agents::Deprecations.warn( "Result#each is deprecated. Use result.content.each instead.", caller ) content&.each(&block) end |
#error? ⇒ Boolean
Returns whether the execution failed
229 230 231 |
# File 'lib/ruby_llm/agents/results/base.rb', line 229 def error? !success? end |
#execution ⇒ RubyLLM::Agents::Execution?
Loads the associated Execution record from the database
Useful for debugging in the Rails console to inspect the full execution record after an agent call.
201 202 203 |
# File 'lib/ruby_llm/agents/results/base.rb', line 201 def execution @execution ||= Execution.find_by(id: execution_id) if execution_id end |
#keys ⇒ Object
Use result.content.keys instead
344 345 346 347 348 349 350 |
# File 'lib/ruby_llm/agents/results/base.rb', line 344 def keys RubyLLM::Agents::Deprecations.warn( "Result#keys is deprecated. Use result.content.keys instead.", caller ) content&.keys end |
#map(&block) ⇒ Object
Use result.content.map instead
371 372 373 374 375 376 377 |
# File 'lib/ruby_llm/agents/results/base.rb', line 371 def map(&block) RubyLLM::Agents::Deprecations.warn( "Result#map is deprecated. Use result.content.map instead.", caller ) content&.map(&block) end |
#streaming? ⇒ Boolean
Returns whether streaming was enabled
215 216 217 |
# File 'lib/ruby_llm/agents/results/base.rb', line 215 def streaming? streaming == true end |
#success? ⇒ Boolean
Returns whether the execution succeeded
222 223 224 |
# File 'lib/ruby_llm/agents/results/base.rb', line 222 def success? error_class.nil? end |
#thinking? ⇒ Boolean Also known as: has_thinking?
Returns whether thinking data is present in the result
265 266 267 |
# File 'lib/ruby_llm/agents/results/base.rb', line 265 def thinking? thinking_text.present? end |
#to_h ⇒ Hash
Converts the result to a hash
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/ruby_llm/agents/results/base.rb', line 273 def to_h { content: content, input_tokens: input_tokens, output_tokens: output_tokens, total_tokens: total_tokens, cached_tokens: cached_tokens, cache_creation_tokens: cache_creation_tokens, input_cost: input_cost, output_cost: output_cost, total_cost: total_cost, model_id: model_id, chosen_model_id: chosen_model_id, temperature: temperature, started_at: started_at, completed_at: completed_at, duration_ms: duration_ms, time_to_first_token_ms: time_to_first_token_ms, finish_reason: finish_reason, streaming: streaming, error_class: error_class, error_message: , attempts_count: attempts_count, attempts: attempts, tool_calls: tool_calls, tool_calls_count: tool_calls_count, thinking_text: thinking_text, thinking_signature: thinking_signature, thinking_tokens: thinking_tokens, execution_id: execution_id, agent_class_name: agent_class_name, trace: trace }.compact end |
#to_json(*args) ⇒ String
Custom to_json that returns content as JSON for backward compatibility
385 386 387 |
# File 'lib/ruby_llm/agents/results/base.rb', line 385 def to_json(*args) content.to_json(*args) end |
#tool_calls? ⇒ Boolean Also known as: has_tool_calls?
Returns whether tool calls were made during execution
257 258 259 |
# File 'lib/ruby_llm/agents/results/base.rb', line 257 def tool_calls? tool_calls_count.to_i > 0 end |
#total_tokens ⇒ Integer
Returns total tokens (input + output)
208 209 210 |
# File 'lib/ruby_llm/agents/results/base.rb', line 208 def total_tokens (input_tokens || 0) + (output_tokens || 0) end |
#truncated? ⇒ Boolean
Returns whether the response was truncated due to max tokens
250 251 252 |
# File 'lib/ruby_llm/agents/results/base.rb', line 250 def truncated? finish_reason == "length" end |
#used_fallback? ⇒ Boolean
Returns whether a fallback model was used
243 244 245 |
# File 'lib/ruby_llm/agents/results/base.rb', line 243 def used_fallback? chosen_model_id.present? && chosen_model_id != model_id end |
#values ⇒ Object
Use result.content.values instead
353 354 355 356 357 358 359 |
# File 'lib/ruby_llm/agents/results/base.rb', line 353 def values RubyLLM::Agents::Deprecations.warn( "Result#values is deprecated. Use result.content.values instead.", caller ) content&.values end |