Class: LLM::Agent
- Inherits:
-
Object
- Object
- LLM::Agent
- Defined in:
- lib/llm/agent.rb
Overview
LLM::Agent provides a class-level DSL for defining reusable, preconfigured assistants with defaults for model, tools, schema, and instructions.
It wraps the same stateful runtime surface as
LLM::Context: message history, usage, persistence,
streaming parameters, and provider-backed requests still flow through
an underlying context. The defining behavior of an agent is that it
automatically resolves pending tool calls for you during talk and
respond, instead of leaving tool loops to the caller.
Notes:
- Instructions are injected once unless a system message is already present.
- An agent automatically executes tool loops (unlike LLM::Context).
- The automatic tool loop enables the wrapped context's
guardby default. The built-in LLM::LoopGuard detects repeated tool-call patterns and blocks stuck execution before more tool work is queued. - The default tool attempt budget is
25. After that, the agent sends advisory tool errors back through the model and keeps the loop in-band. Settool_attempts: nilto disable that advisory behavior. - Tool loop execution can be configured with
concurrency :call,:thread,:task,:fiber, or:ractor.
Instance Attribute Summary collapse
-
#llm ⇒ LLM::Provider
readonly
Returns a provider.
Class Method Summary collapse
-
.tools(*tools, &block) ⇒ Array<LLM::Function>
Set or get the default tools.
-
.skills(*skills, &block) ⇒ Array<String>?
Set or get the default skills.
-
.schema(schema = nil, &block) ⇒ #to_json?
Set or get the default schema.
-
.instructions(instructions = nil) ⇒ String?
Set or get the default instructions.
-
.concurrency(concurrency = nil) ⇒ Symbol, ...
Set or get the tool execution concurrency.
-
.tracer(tracer = nil, &block) ⇒ LLM::Tracer, ...
Set or get the default tracer.
-
.stream(stream = nil, &block) ⇒ Object, ...
Set or get the default stream.
-
.confirm(*tool_names, &block) ⇒ Array<String>, ...
Set or get the tool names that require confirmation before they can run.
-
.model(model = nil, &block) ⇒ String?
Set or get the default model.
Instance Method Summary collapse
-
#on_tool_confirmation(fn, strategy) ⇒ LLM::Function::Return
This method is called when confirmation is required before a tool can run.
-
#initialize(llm, params = {}) ⇒ Agent
constructor
A new instance of Agent.
-
#talk(prompt, params = {}) ⇒ LLM::Response
Maintain a conversation via the chat completions API.
- #ask(prompt, params = {}) ⇒ Object
- #messages ⇒ LLM::Buffer<LLM::Message>
- #functions ⇒ Array<LLM::Function>
- #returns ⇒ Array<LLM::Function::Return>
- #wait ⇒ Array<LLM::Function::Return>
- #usage ⇒ LLM::Object
-
#interrupt! ⇒ nil
(also: #cancel!)
Interrupt the active request, if any.
- #prompt(&b) ⇒ LLM::Prompt (also: #build_prompt)
-
#image_url(url) ⇒ LLM::Object
Returns a tagged object.
-
#local_file(path) ⇒ LLM::Object
Returns a tagged object.
-
#remote_file(res) ⇒ LLM::Object
Returns a tagged object.
-
#tracer ⇒ LLM::Tracer
Returns an LLM tracer.
-
#stream ⇒ LLM::Stream, ...
Returns a stream object, or nil.
-
#model ⇒ String
Returns the model an Agent is actively using.
- #mode ⇒ Symbol
-
#concurrency ⇒ Symbol, ...
Returns the configured tool execution concurrency.
- #cost ⇒ LLM::Cost
- #context_window ⇒ Integer
- #params ⇒ Hash
- #to_h ⇒ Hash
- #to_json ⇒ String
- #inspect ⇒ String
- #serialize(**kw) ⇒ void (also: #save)
- #deserialize(**kw) ⇒ Object (also: #restore)
Constructor Details
#initialize(llm, params = {}) ⇒ Agent
Returns a new instance of Agent.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/llm/agent.rb', line 187 def initialize(llm, params = {}) @llm = llm fields = %i[model skills schema tracer stream tools concurrency instructions confirm] fields_ivar = %i[tracer concurrency instructions confirm] fields.each do |field| resolvable = params.key?(field) ? params.delete(field) : self.class.public_send(field) resolve_symbol = !%i[concurrency confirm].include?(field) resolved = resolvable != nil ? resolve_option(self, resolvable, resolve_symbol:) : resolvable resolved = [*resolved].map(&:to_s) if field == :confirm && resolved if field == :model params[field] = resolved unless resolved.nil? || params.key?(field) elsif resolved && !fields_ivar.include?(field) params[field] ||= resolved elsif fields_ivar.include?(field) instance_variable_set(:"@#{field}", resolved) end end @ctx = LLM::Context.new(llm, {guard: true}.merge(params)) end |
Instance Attribute Details
#llm ⇒ LLM::Provider (readonly)
Returns a provider
43 44 45 |
# File 'lib/llm/agent.rb', line 43 def llm @llm end |
Class Method Details
.tools(*tools, &block) ⇒ Array<LLM::Function>
Set or get the default tools
62 63 64 65 |
# File 'lib/llm/agent.rb', line 62 def self.tools(*tools, &block) return @tools || [] if tools.empty? && !block @tools = block || tools.flatten end |
.skills(*skills, &block) ⇒ Array<String>?
Set or get the default skills
73 74 75 76 |
# File 'lib/llm/agent.rb', line 73 def self.skills(*skills, &block) return @skills if skills.empty? && !block @skills = block || skills.flatten end |
.schema(schema = nil, &block) ⇒ #to_json?
Set or get the default schema
84 85 86 87 |
# File 'lib/llm/agent.rb', line 84 def self.schema(schema = nil, &block) return @schema if schema.nil? && !block @schema = block || schema end |
.instructions(instructions = nil) ⇒ String?
Set or get the default instructions
95 96 97 98 |
# File 'lib/llm/agent.rb', line 95 def self.instructions(instructions = nil) return @instructions if instructions.nil? @instructions = instructions end |
.concurrency(concurrency = nil) ⇒ Symbol, ...
Set or get the tool execution concurrency.
115 116 117 118 |
# File 'lib/llm/agent.rb', line 115 def self.concurrency(concurrency = nil) return @concurrency if concurrency.nil? @concurrency = concurrency end |
.tracer(tracer = nil, &block) ⇒ LLM::Tracer, ...
Set or get the default tracer.
When a block is provided, it is stored and evaluated lazily against the agent instance during initialization so it can build a tracer from the resolved provider.
135 136 137 138 |
# File 'lib/llm/agent.rb', line 135 def self.tracer(tracer = nil, &block) return @tracer if tracer.nil? && !block @tracer = block || tracer end |
.stream(stream = nil, &block) ⇒ Object, ...
Set or get the default stream.
When a block is provided, it is stored and evaluated lazily against the agent instance during initialization so it can build a fresh stream for each agent.
155 156 157 158 |
# File 'lib/llm/agent.rb', line 155 def self.stream(stream = nil, &block) return @stream if stream.nil? && !block @stream = block || stream end |
.confirm(*tool_names, &block) ⇒ Array<String>, ...
Set or get the tool names that require confirmation before they can run.
168 169 170 171 |
# File 'lib/llm/agent.rb', line 168 def self.confirm(*tool_names, &block) return @confirm if tool_names.empty? && !block @confirm = block || tool_names.flatten.map(&:to_s) end |
.model(model = nil, &block) ⇒ String?
Set or get the default model
51 52 53 54 |
# File 'lib/llm/agent.rb', line 51 def self.model(model = nil, &block) return @model if model.nil? && !block @model = block || model end |
Instance Method Details
#on_tool_confirmation(fn, strategy) ⇒ LLM::Function::Return
This method is called when confirmation is required before a tool can run.
411 412 413 |
# File 'lib/llm/agent.rb', line 411 def on_tool_confirmation(fn, strategy) fn.cancel end |
#talk(prompt, params = {}) ⇒ LLM::Response
Maintain a conversation via the chat completions API. This method immediately sends a request to the LLM and returns the response.
223 224 225 |
# File 'lib/llm/agent.rb', line 223 def talk(prompt, params = {}) run_loop(prompt, params, :talk) end |
#ask(prompt, params = {}) ⇒ Object
229 230 231 |
# File 'lib/llm/agent.rb', line 229 def ask(prompt, params = {}) run_loop(prompt, params, :ask) end |
#functions ⇒ Array<LLM::Function>
241 242 243 |
# File 'lib/llm/agent.rb', line 241 def functions @tracer ? @llm.with_tracer(@tracer) { @ctx.functions } : @ctx.functions end |
#returns ⇒ Array<LLM::Function::Return>
248 249 250 |
# File 'lib/llm/agent.rb', line 248 def returns @ctx.returns end |
#wait ⇒ Array<LLM::Function::Return>
255 256 257 |
# File 'lib/llm/agent.rb', line 255 def wait(...) @tracer ? @llm.with_tracer(@tracer) { @ctx.wait(...) } : @ctx.wait(...) end |
#interrupt! ⇒ nil Also known as: cancel!
Interrupt the active request, if any.
268 269 270 |
# File 'lib/llm/agent.rb', line 268 def interrupt! @ctx.interrupt! end |
#prompt(&b) ⇒ LLM::Prompt Also known as: build_prompt
277 278 279 |
# File 'lib/llm/agent.rb', line 277 def prompt(&b) @ctx.prompt(&b) end |
#image_url(url) ⇒ LLM::Object
Returns a tagged object
287 288 289 |
# File 'lib/llm/agent.rb', line 287 def image_url(url) @ctx.image_url(url) end |
#local_file(path) ⇒ LLM::Object
Returns a tagged object
296 297 298 |
# File 'lib/llm/agent.rb', line 296 def local_file(path) @ctx.local_file(path) end |
#remote_file(res) ⇒ LLM::Object
Returns a tagged object
305 306 307 |
# File 'lib/llm/agent.rb', line 305 def remote_file(res) @ctx.remote_file(res) end |
#tracer ⇒ LLM::Tracer
Returns an LLM tracer
312 313 314 |
# File 'lib/llm/agent.rb', line 312 def tracer @tracer || @ctx.tracer end |
#stream ⇒ LLM::Stream, ...
Returns a stream object, or nil
319 320 321 |
# File 'lib/llm/agent.rb', line 319 def stream @ctx.stream end |
#model ⇒ String
Returns the model an Agent is actively using
326 327 328 |
# File 'lib/llm/agent.rb', line 326 def model @ctx.model end |
#mode ⇒ Symbol
332 333 334 |
# File 'lib/llm/agent.rb', line 332 def mode @ctx.mode end |
#concurrency ⇒ Symbol, ...
Returns the configured tool execution concurrency.
339 340 341 |
# File 'lib/llm/agent.rb', line 339 def concurrency @concurrency end |
#context_window ⇒ Integer
353 354 355 |
# File 'lib/llm/agent.rb', line 353 def context_window @ctx.context_window end |
#params ⇒ Hash
360 361 362 |
# File 'lib/llm/agent.rb', line 360 def params @ctx.params end |
#to_h ⇒ Hash
367 368 369 |
# File 'lib/llm/agent.rb', line 367 def to_h @ctx.to_h end |
#to_json ⇒ String
373 374 375 |
# File 'lib/llm/agent.rb', line 373 def to_json(...) LLM.json.dump(to_h, ...) end |
#inspect ⇒ String
379 380 381 382 |
# File 'lib/llm/agent.rb', line 379 def inspect "#<#{LLM::Utils.object_id(self)} " \ "@llm=#{@llm.class}, @mode=#{mode.inspect}, @messages=#{.inspect}>" end |
#serialize(**kw) ⇒ void Also known as: save
This method returns an undefined value.
387 388 389 |
# File 'lib/llm/agent.rb', line 387 def serialize(**kw) @ctx.serialize(**kw) end |
#deserialize(**kw) ⇒ Object Also known as: restore
395 396 397 |
# File 'lib/llm/agent.rb', line 395 def deserialize(**kw) @ctx.deserialize(**kw) end |