Module: RubyLLM::Agents::DSL::Base
- Included in:
- BaseAgent
- Defined in:
- lib/ruby_llm/agents/dsl/base.rb
Overview
Base DSL available to all agents.
Provides common configuration methods that every agent type needs:
-
model: The LLM model to use
-
system: System instructions
-
user: The user prompt (string with placeholders)
-
assistant: Assistant prefill (string with optional placeholders)
-
description: Human-readable description
-
timeout: Request timeout
-
returns: Structured output schema
Two levels for defining prompts:
-
Class-level string/heredoc for static content
-
Instance method override for dynamic content
Constant Summary collapse
- PLACEHOLDER_PATTERN =
Regex pattern to extract placeholder parameters from prompt strings
/\{(\w+)\}/
Configuration DSL collapse
-
#assistant(text = nil) ⇒ String?
Sets the assistant prefill string.
-
#assistant_config ⇒ String?
Returns the assistant prefill configuration.
-
#cache_prompts(value = nil) ⇒ Boolean
Enables Anthropic prompt caching for this agent.
-
#description(value = nil) ⇒ String?
Sets or returns the description for this agent class.
-
#model(value = nil, overridable: nil) ⇒ String
Sets or returns the LLM model for this agent class.
-
#prompt(template = nil) ⇒ String?
deprecated
Deprecated.
Use ‘user` instead.
-
#returns(&block) ⇒ RubyLLM::Schema?
Alias for schema with a clearer name.
-
#schema(value = nil, &block) ⇒ Hash, ...
Sets or returns the response schema for structured output.
-
#system(text = nil) ⇒ String?
Sets the system prompt/instructions.
-
#system_config ⇒ String?
Returns the system prompt configuration.
-
#timeout(value = nil, overridable: nil) ⇒ Integer
Sets or returns the timeout in seconds for LLM requests.
-
#user(template = nil) ⇒ String?
Sets the user prompt template.
-
#user_config ⇒ String?
Returns the user prompt configuration.
Dashboard Override Support collapse
-
#active_overrides ⇒ Hash{String => Object}
Returns the currently active dashboard overrides for this agent.
-
#clear_override_cache! ⇒ void
Clears the in-memory override cache so the next access reloads from DB.
-
#overridable? ⇒ Boolean
Returns true if any field is overridable from the dashboard.
-
#overridable_fields ⇒ Array<Symbol>
Returns which fields are overridable for this agent.
Instance Method Details
#active_overrides ⇒ Hash{String => Object}
Returns the currently active dashboard overrides for this agent
Only returns overrides for fields that are declared overridable.
331 332 333 334 335 336 |
# File 'lib/ruby_llm/agents/dsl/base.rb', line 331 def active_overrides return {} unless overridable? raw = load_overrides raw.select { |field, _| overridable_fields.include?(field.to_sym) } end |
#assistant(text = nil) ⇒ String?
Sets the assistant prefill string
The assistant prefill is sent as the last message with the “assistant” role, priming the model to continue from that point. Useful for:
-
Forcing output format (e.g., starting with “for JSON)
-
Steering the response style
Supports {placeholder syntax for parameter interpolation.
191 192 193 194 195 196 197 |
# File 'lib/ruby_llm/agents/dsl/base.rb', line 191 def assistant(text = nil) if text @assistant_template = text auto_register_params_from_template(text) end @assistant_template || inherited_or_default(:assistant_config, nil) end |
#assistant_config ⇒ String?
Returns the assistant prefill configuration
202 203 204 |
# File 'lib/ruby_llm/agents/dsl/base.rb', line 202 def assistant_config @assistant_template || inherited_or_default(:assistant_config, nil) end |
#cache_prompts(value = nil) ⇒ Boolean
Enables Anthropic prompt caching for this agent
When enabled, adds cache_control breakpoints to the system prompt and the last tool definition so Anthropic caches them across multi-turn agent loops. This reduces input token costs by ~90% on subsequent calls within the same cache window (~5 minutes).
Only takes effect when the resolved model is served by Anthropic. Non-Anthropic models silently ignore this setting.
256 257 258 259 260 261 |
# File 'lib/ruby_llm/agents/dsl/base.rb', line 256 def cache_prompts(value = nil) @cache_prompts = value unless value.nil? return @cache_prompts if defined?(@cache_prompts) && !@cache_prompts.nil? inherited_or_default(:cache_prompts, false) end |
#clear_override_cache! ⇒ void
This method returns an undefined value.
Clears the in-memory override cache so the next access reloads from DB
Called automatically by AgentOverride after_save/after_destroy callbacks.
343 344 345 |
# File 'lib/ruby_llm/agents/dsl/base.rb', line 343 def clear_override_cache! @_override_cache = nil end |
#description(value = nil) ⇒ String?
Sets or returns the description for this agent class
Useful for documentation and tool registration.
214 215 216 217 |
# File 'lib/ruby_llm/agents/dsl/base.rb', line 214 def description(value = nil) @description = value if value @description || inherited_or_default(:description, nil) end |
#model(value = nil, overridable: nil) ⇒ String
Sets or returns the LLM model for this agent class
79 80 81 82 83 84 85 |
# File 'lib/ruby_llm/agents/dsl/base.rb', line 79 def model(value = nil, overridable: nil) @model = value if value register_overridable(:model) if overridable base = @model || inherited_or_default(:model, default_model) apply_override(:model, base) end |
#overridable? ⇒ Boolean
Returns true if any field is overridable from the dashboard
322 323 324 |
# File 'lib/ruby_llm/agents/dsl/base.rb', line 322 def overridable? overridable_fields.any? end |
#overridable_fields ⇒ Array<Symbol>
Returns which fields are overridable for this agent
313 314 315 316 317 |
# File 'lib/ruby_llm/agents/dsl/base.rb', line 313 def overridable_fields own = @overridable_fields || [] inherited = superclass.respond_to?(:overridable_fields) ? superclass.overridable_fields : [] (own + inherited).uniq end |
#prompt(template = nil) ⇒ String?
Use ‘user` instead.
Backward-compatible alias for ‘user`
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/ruby_llm/agents/dsl/base.rb', line 127 def prompt(template = nil) if template Deprecations.warn( "`prompt` is deprecated. Use `user` instead (e.g., `user \"#{template.truncate(40)}\"`).", caller ) @user_template = template auto_register_params_from_template(template) end @user_template || inherited_or_default(:user_config, nil) end |
#returns(&block) ⇒ RubyLLM::Schema?
Alias for schema with a clearer name
Defines the structured output schema for this agent. This is the preferred method for defining schemas in the simplified DSL.
302 303 304 |
# File 'lib/ruby_llm/agents/dsl/base.rb', line 302 def returns(&block) schema(&block) end |
#schema(value = nil, &block) ⇒ Hash, ...
Sets or returns the response schema for structured output
Accepts a hash (JSON Schema), a block (passed to RubyLLM::Schema.create), or any object that responds to ‘to_json_schema`.
278 279 280 281 282 283 284 285 |
# File 'lib/ruby_llm/agents/dsl/base.rb', line 278 def schema(value = nil, &block) if value @schema = value elsif block @schema = RubyLLM::Schema.create(&block) end @schema || inherited_or_default(:schema, nil) end |
#system(text = nil) ⇒ String?
Sets the system prompt/instructions
When a string is provided, placeholder syntax is supported for parameter interpolation, same as the ‘user` DSL.
158 159 160 161 162 163 164 |
# File 'lib/ruby_llm/agents/dsl/base.rb', line 158 def system(text = nil) if text @system_template = text auto_register_params_from_template(text) end @system_template || inherited_or_default(:system_config, nil) end |
#system_config ⇒ String?
Returns the system prompt configuration
169 170 171 |
# File 'lib/ruby_llm/agents/dsl/base.rb', line 169 def system_config @system_template || inherited_or_default(:system_config, nil) end |
#timeout(value = nil, overridable: nil) ⇒ Integer
Sets or returns the timeout in seconds for LLM requests
228 229 230 231 232 233 234 |
# File 'lib/ruby_llm/agents/dsl/base.rb', line 228 def timeout(value = nil, overridable: nil) @timeout = value if value register_overridable(:timeout) if overridable base = @timeout || inherited_or_default(:timeout, default_timeout) apply_override(:timeout, base) end |
#user(template = nil) ⇒ String?
Sets the user prompt template
When a string is provided, placeholder syntax is used to interpolate parameters. Parameters are automatically registered (as required) unless already defined with ‘param`.
107 108 109 110 111 112 113 |
# File 'lib/ruby_llm/agents/dsl/base.rb', line 107 def user(template = nil) if template @user_template = template auto_register_params_from_template(template) end @user_template || inherited_or_default(:user_config, nil) end |
#user_config ⇒ String?
Returns the user prompt configuration
118 119 120 |
# File 'lib/ruby_llm/agents/dsl/base.rb', line 118 def user_config @user_template || inherited_or_default(:user_config, nil) end |