-
Notifications
You must be signed in to change notification settings - Fork 4
API Reference
Complete class and method documentation for RubyLLM::Agents.
The base class for all agents.
Set the LLM model.
model "gpt-4o"Set response randomness (0.0-2.0).
temperature 0.7Set version for cache invalidation.
version "1.0"Set request timeout.
timeout 60Enable response caching.
cache 1.hourEnable/disable streaming.
streaming trueDefine a parameter.
param :query, required: true
param :limit, default: 10Options:
-
required: true- Parameter must be provided -
default: value- Default value if not provided
Configure retry behavior.
retries max: 3, backoff: :exponential, base: 0.5, max_delay: 30.0Options:
-
max:- Maximum retry attempts -
backoff:-:exponentialor:constant -
base:- Initial delay in seconds -
max_delay:- Maximum delay cap -
on:- Array of error classes to retry
Set fallback model chain.
fallback_models "gpt-4o-mini", "claude-3-haiku"Configure circuit breaker.
circuit_breaker errors: 10, within: 60, cooldown: 300Options:
-
errors:- Error count to trip breaker -
within:- Time window in seconds -
cooldown:- Cooldown period in seconds
Set maximum time for all attempts.
total_timeout 30Execute the agent.
result = MyAgent.call(query: "test")
result = MyAgent.call(query: "test", dry_run: true)
result = MyAgent.call(query: "test", skip_cache: true)
result = MyAgent.call(query: "test", with: "image.jpg")
result = MyAgent.call(query: "test") { |chunk| print chunk }Execute the agent (called by .call).
Override to define system prompt.
def system_prompt
"You are a helpful assistant."
endOverride to define user prompt.
def user_prompt
"Process: #{query}"
endOverride to define structured output.
def schema
@schema ||= RubyLLM::Schema.create do
string :result
end
endOverride to post-process response.
def process_response(response)
result = super(response)
result[:processed_at] = Time.current
result
endOverride to add custom metadata.
def execution_metadata
{ user_id: user_id }
endOverride to customize cache key.
def cache_key_data
{ query: query }
endReturned by agent calls.
result.content # Parsed response
result[:key] # Hash-style access
result.dig(:nested, :key)result.input_tokens # Input token count
result.output_tokens # Output token count
result.total_tokens # Total tokens
result.cached_tokens # Cached tokensresult.input_cost # Input cost (USD)
result.output_cost # Output cost (USD)
result.total_cost # Total cost (USD)result.model_id # Requested model
result.chosen_model_id # Actual model used
result.temperature # Temperature settingresult.duration_ms # Execution duration
result.started_at # Start timestamp
result.completed_at # End timestamp
result.time_to_first_token_ms # TTFT (streaming)result.success? # Did it succeed?
result.finish_reason # "stop", "length", etc.
result.streaming? # Was streaming used?
result.truncated? # Was output truncated?result.attempts_count # Number of attempts
result.used_fallback? # Was fallback used?result.tool_calls # Array of tool calls
result.tool_calls_count
result.has_tool_calls?result.to_h # All data as hashActiveRecord model for execution records.
# Time-based
.today
.yesterday
.this_week
.this_month
.last_7_days
.last_30_days
.between(start, finish)
# Status
.successful
.failed
.status_error
.status_timeout
.status_running
# Agent/Model
.by_agent("AgentName")
.by_model("gpt-4o")
.by_version("1.0")
# Performance
.expensive(threshold)
.slow(milliseconds)
.high_token_usage(count)
# Streaming
.streaming
.non_streamingexecution.agent_type # String
execution.model_id # String
execution.status # String: success, error, timeout
execution.input_tokens # Integer
execution.output_tokens # Integer
execution.cached_tokens # Integer
execution.input_cost # Decimal
execution.output_cost # Decimal
execution.total_cost # Decimal
execution.duration_ms # Integer
execution.parameters # Hash (JSONB)
execution.system_prompt # Text
execution.user_prompt # Text
execution.response # Text
execution.error_message # Text
execution.error_class # String
execution.metadata # Hash (JSONB)
execution.streaming # Boolean
execution.time_to_first_token_ms # Integer
execution.attempts # Array (JSONB)
execution.chosen_model_id # String
execution.finish_reason # String
execution.created_at # DateTime# Reports
Execution.daily_report
Execution.cost_by_agent(period: :today)
Execution.cost_by_model(period: :this_week)
Execution.stats_for("AgentName", period: :today)
Execution.compare_versions("Agent", "1.0", "2.0")
Execution.trend_analysis(agent_type: "Agent", days: 7)
# Analytics
Execution.streaming_rate
Execution.avg_time_to_first_tokenWorkflow orchestration.
workflow = RubyLLM::Agents::Workflow.pipeline(
Agent1,
Agent2,
Agent3,
timeout: 60,
max_cost: 1.00,
before_step: { Agent2 => ->(prev, ctx) { ... } },
on_step_failure: :skip # or :abort
)
result = workflow.call(input: data)workflow = RubyLLM::Agents::Workflow.parallel(
branch1: Agent1,
branch2: Agent2,
timeout: 30,
fail_fast: true,
aggregate: ->(results) { ... }
)
result = workflow.call(input: data)workflow = RubyLLM::Agents::Workflow.router(
classifier: ClassifierAgent,
routes: {
"route1" => Agent1,
"route2" => Agent2
},
default: DefaultAgent,
classification_field: :intent,
confidence_threshold: 0.8
)
result = workflow.call(input: data)Budget management.
# Check status
BudgetTracker.status
BudgetTracker.status(agent_type: "MyAgent")
# Check remaining
BudgetTracker.remaining_budget(:global, :daily)
BudgetTracker.remaining_budget(:per_agent, :daily, "MyAgent")
# Check exceeded
BudgetTracker.exceeded?(:global, :daily)Circuit breaker management.
# Check status
CircuitBreaker.status("gpt-4o")
# => { state: :open, errors: 10, closes_at: Time }
# Manual control
CircuitBreaker.open!("gpt-4o")
CircuitBreaker.close!("gpt-4o")
CircuitBreaker.reset_all!Global configuration.
RubyLLM::Agents.configure do |config|
# Defaults
config.default_model = "gpt-4o"
config.default_temperature = 0.0
config.default_timeout = 60
config.default_streaming = false
# Caching
config.cache_store = Rails.cache
# Logging
config.async_logging = true
config.retention_period = 30.days
config.persist_prompts = true
config.persist_responses = true
# Anomaly Detection
config.anomaly_cost_threshold = 5.00
config.anomaly_duration_threshold = 10_000
# Dashboard
config.dashboard_auth = ->(c) { c.current_user&.admin? }
config.dashboard_parent_controller = "ApplicationController"
config.dashboard_per_page = 25
# Budgets
config.budgets = {
global_daily: 100.0,
enforcement: :hard
}
# Alerts
config.alerts = {
on_events: [:budget_hard_cap],
slack_webhook_url: "..."
}
# Redaction
config.redaction = {
fields: %w[password],
patterns: [/.../],
placeholder: "[REDACTED]"
}
endRubyLLM::Agents::BudgetExceededError # Budget limit exceeded
RubyLLM::Agents::CircuitOpenError # Circuit breaker is open- Agent DSL - DSL reference
- Configuration - Configuration guide
- Result Object - Result details