Class: LLM::Context
- Inherits:
-
Object
- Object
- LLM::Context
- Includes:
- Deserializer, Serializer
- Defined in:
- lib/llm/context.rb,
lib/llm/context/serializer.rb,
lib/llm/context/deserializer.rb
Overview
LLM::Context is the stateful execution boundary in llm.rb.
It holds the evolving runtime state for an LLM workflow: conversation history, tool calls and returns, schema and streaming configuration, accumulated usage, and request ownership for interruption.
This is broader than prompt context alone. A context is the object that lets one-off prompts, streaming turns, tool execution, persistence, retries, and serialized long-lived workflows all run through the same model.
A context can drive the chat completions API that all providers support or the Responses API on providers that expose it.
Defined Under Namespace
Modules: Deserializer, Serializer
Instance Attribute Summary collapse
-
#compacted ⇒ Boolean
(also: #compacted?)
private
Returns whether the context has been compacted and no later model response has cleared that state.
-
#llm ⇒ LLM::Provider
readonly
Returns a provider.
-
#messages ⇒ LLM::Buffer<LLM::Message>
readonly
Returns the accumulated message history for this context.
-
#mode ⇒ Symbol
readonly
Returns the context mode.
Instance Method Summary collapse
-
#ask(prompt, options = {}) {|String| ... } ⇒ LLM::Response
Ask a question and return the content string directly.
-
#compactor ⇒ LLM::Compactor
Returns a context compactor This feature is inspired by the compaction approach developed by General Intelligence Systems.
-
#compactor=(compactor) ⇒ LLM::Compactor, ...
Sets a context compactor or compactor config.
-
#context_window ⇒ Integer
Returns the model's context window.
-
#cost ⇒ LLM::Cost
Returns an approximate cost for a given context based on both the provider, and model.
-
#functions ⇒ Array<LLM::Function>
Returns an array of functions that can be called.
-
#functions? ⇒ Boolean
Returns whether there is pending tool work in this context.
-
#guard ⇒ #call?
Returns a guard, if configured.
-
#guard=(guard) ⇒ #call, ...
Sets a guard or guard config.
-
#image_url(url) ⇒ LLM::Object
Recongize an object as a URL to an image.
-
#initialize(llm, params = {}) ⇒ Context
constructor
A new instance of Context.
- #inspect ⇒ String
-
#interrupt! ⇒ nil
(also: #cancel!)
Interrupt the active request, if any.
-
#local_file(path) ⇒ LLM::Object
Recongize an object as a local file.
-
#model ⇒ String
Returns the model a Context is actively using.
-
#params ⇒ Hash
Returns the default params for this context.
-
#prompt(&b) ⇒ LLM::Prompt
(also: #build_prompt)
Build a role-aware prompt for a single request.
-
#remote_file(res) ⇒ LLM::Object
Reconginize an object as a remote file.
-
#returns ⇒ Array<LLM::Function::Return>
Returns tool returns accumulated in this context.
-
#serialize(path:) ⇒ void
(also: #save)
Save the current context state.
-
#spawn(function, strategy) ⇒ LLM::Function::Return, LLM::Function::Task
Spawns a function through the context.
-
#stream ⇒ LLM::Stream, ...
Returns a stream object, or nil.
-
#talk(prompt, params = {}) ⇒ LLM::Response
Interact with the context via the chat completions API.
- #to_h ⇒ Hash
- #to_json ⇒ String
-
#tracer ⇒ LLM::Tracer
Returns an LLM tracer.
- #tracer=(other) ⇒ void
-
#transformer ⇒ #call?
Returns a transformer, if configured.
-
#transformer=(transformer) ⇒ #call?
Sets a transformer.
-
#usage ⇒ LLM::Object
Returns token usage accumulated in this context.
-
#wait(strategy, except: []) ⇒ Array<LLM::Function::Return>
Waits for queued tool work to finish.
Methods included from Deserializer
#deserialize, #deserialize_message
Constructor Details
#initialize(llm, params = {}) ⇒ Context
Returns a new instance of Context.
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/llm/context.rb', line 84 def initialize(llm, params = {}) @llm = llm @mode = params.delete(:mode) || (llm.name == :openai ? :responses : :completions) @compactor = params.delete(:compactor) @guard = params.delete(:guard) @transformer = params.delete(:transformer) tools = [*params.delete(:tools), *load_skills(params.delete(:skills))] @params = {model: llm.default_model, schema: nil}.compact.merge!(params) @params[:tools] = tools unless tools.empty? @params[:store] ||= false if @mode == :responses @messages = LLM::Buffer.new(llm) end |
Instance Attribute Details
#compacted ⇒ Boolean Also known as: compacted?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns whether the context has been compacted and no later model response has cleared that state.
127 128 129 |
# File 'lib/llm/context.rb', line 127 def compacted @compacted end |
#llm ⇒ LLM::Provider (readonly)
Returns a provider
64 65 66 |
# File 'lib/llm/context.rb', line 64 def llm @llm end |
#messages ⇒ LLM::Buffer<LLM::Message> (readonly)
Returns the accumulated message history for this context
59 60 61 |
# File 'lib/llm/context.rb', line 59 def @messages end |
#mode ⇒ Symbol (readonly)
Returns the context mode
69 70 71 |
# File 'lib/llm/context.rb', line 69 def mode @mode end |
Instance Method Details
#ask(prompt, options = {}) {|String| ... } ⇒ LLM::Response
Ask a question and return the content string directly.
Accepts with: for file attachments and a block for streaming.
This interface is compatible with RubyLLM's ask method.
221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/llm/context.rb', line 221 def ask(prompt, = {}, &block) = {with: nil, stream: nil}.merge!( || {}) with, stream = .values_at(:with, :stream) prompt = with ? [prompt, [*with].map { local_file(_1) }] : prompt target = if block blk = block.dup blk.singleton_class.alias_method(:<<, :call) blk else stream end target ? talk(prompt, stream: target) : talk(prompt) end |
#compactor ⇒ LLM::Compactor
Returns a context compactor This feature is inspired by the compaction approach developed by General Intelligence Systems.
109 110 111 112 |
# File 'lib/llm/context.rb', line 109 def compactor @compactor = LLM::Compactor.new(self, @compactor || {}) unless LLM::Compactor === @compactor @compactor end |
#compactor=(compactor) ⇒ LLM::Compactor, ...
Sets a context compactor or compactor config
118 119 120 |
# File 'lib/llm/context.rb', line 118 def compactor=(compactor) @compactor = compactor end |
#context_window ⇒ Integer
This method returns 0 when the provider or model can't be found within Registry.
Returns the model's context window. The context window is the maximum amount of input and output tokens a model can consider in a single request.
494 495 496 497 498 499 500 501 |
# File 'lib/llm/context.rb', line 494 def context_window LLM .registry_for(llm) .limit(model:) .context rescue LLM::NoSuchModelError, LLM::NoSuchRegistryError 0 end |
#cost ⇒ LLM::Cost
Returns an approximate cost for a given context based on both the provider, and model
482 483 484 |
# File 'lib/llm/context.rb', line 482 def cost LLM::Cost.from(self) end |
#functions ⇒ Array<LLM::Function>
Returns an array of functions that can be called
246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/llm/context.rb', line 246 def functions return_ids = returns.map(&:id) @messages .select(&:assistant?) .flat_map do |msg| fns = msg.functions.select { _1.pending? && !return_ids.include?(_1.id) } fns.each do |fn| fn.tracer = tracer fn.model = msg.model end end.extend(LLM::Function::Array) end |
#functions? ⇒ Boolean
Returns whether there is pending tool work in this context. This prefers queued streamed tool work when present, and otherwise falls back to unresolved functions derived from the message history.
264 265 266 267 |
# File 'lib/llm/context.rb', line 264 def functions? pending = queue (pending && !pending.empty?) || functions.any? end |
#guard ⇒ #call?
Returns a guard, if configured.
Guards are context-level supervisors for agentic execution. A guard can inspect the runtime state and decide whether pending tool work should be blocked before the context keeps looping.
The built-in implementation is LLM::LoopGuard, which detects repeated tool-call patterns and turns them into in-band LLM::GuardError tool returns.
142 143 144 145 146 147 |
# File 'lib/llm/context.rb', line 142 def guard return if @guard.nil? || @guard == false @guard = LLM::LoopGuard.new if @guard == true @guard = LLM::LoopGuard.new(@guard) if Hash === @guard @guard end |
#guard=(guard) ⇒ #call, ...
Sets a guard or guard config.
Guards must implement call(ctx) and return either nil or a warning
string. Returning a warning tells the context to block pending tool work
with guarded tool errors instead of continuing the loop.
158 159 160 |
# File 'lib/llm/context.rb', line 158 def guard=(guard) @guard = guard end |
#image_url(url) ⇒ LLM::Object
Recongize an object as a URL to an image
393 394 395 |
# File 'lib/llm/context.rb', line 393 def image_url(url) LLM::Object.from(value: url, kind: :image_url) end |
#inspect ⇒ String
237 238 239 240 241 |
# File 'lib/llm/context.rb', line 237 def inspect "#<#{LLM::Utils.object_id(self)} " \ "@llm=#{@llm.class}, @mode=#{@mode.inspect}, @params=#{@params.inspect}, " \ "@messages=#{@messages.inspect}>" end |
#interrupt! ⇒ nil Also known as: cancel!
Interrupt the active request, if any. This is inspired by Go's context cancellation model.
334 335 336 337 338 339 340 341 342 343 |
# File 'lib/llm/context.rb', line 334 def interrupt! pending = functions.to_a llm.interrupt!(@owner) queue&.interrupt! return if pending.empty? pending.each(&:interrupt!) returns = pending.map { _1.cancel(reason: "function call cancelled") } @messages << LLM::Message.new(@llm.tool_role, returns) nil end |
#local_file(path) ⇒ LLM::Object
Recongize an object as a local file
403 404 405 |
# File 'lib/llm/context.rb', line 403 def local_file(path) LLM::Object.from(value: LLM.File(path), kind: :local_file) end |
#model ⇒ String
Returns the model a Context is actively using
442 443 444 |
# File 'lib/llm/context.rb', line 442 def model .find(&:assistant?)&.model || @params[:model] end |
#params ⇒ Hash
Returns the default params for this context
100 101 102 |
# File 'lib/llm/context.rb', line 100 def params @params.dup end |
#prompt(&b) ⇒ LLM::Prompt Also known as: build_prompt
Build a role-aware prompt for a single request.
Prefer this method over #build_prompt. The older method name is kept for backward compatibility.
382 383 384 |
# File 'lib/llm/context.rb', line 382 def prompt(&b) LLM::Prompt.new(@llm, &b) end |
#remote_file(res) ⇒ LLM::Object
Reconginize an object as a remote file
413 414 415 |
# File 'lib/llm/context.rb', line 413 def remote_file(res) LLM::Object.from(value: res, kind: :remote_file) end |
#returns ⇒ Array<LLM::Function::Return>
Returns tool returns accumulated in this context
287 288 289 290 291 292 293 294 295 |
# File 'lib/llm/context.rb', line 287 def returns @messages .select(&:tool_return?) .flat_map do |msg| LLM::Function::Return === msg.content ? [msg.content] : [*msg.content].grep(LLM::Function::Return) end end |
#serialize(path:) ⇒ void Also known as: save
This method returns an undefined value.
Save the current context state
473 474 475 |
# File 'lib/llm/context.rb', line 473 def serialize(path:) ::File.binwrite path, LLM.json.dump(to_h) end |
#spawn(function, strategy) ⇒ LLM::Function::Return, LLM::Function::Task
Spawns a function through the context.
When a guard is configured, this method can return an in-band guarded tool error instead of spawning work.
278 279 280 281 282 |
# File 'lib/llm/context.rb', line 278 def spawn(function, strategy) warning = guard&.call(self) return guarded_return_for(function, warning) if warning function.spawn(strategy) end |
#stream ⇒ LLM::Stream, ...
Returns a stream object, or nil
435 436 437 |
# File 'lib/llm/context.rb', line 435 def stream @stream || @params[:stream] end |
#talk(prompt, params = {}) ⇒ LLM::Response
Interact with the context via the chat completions API. This method immediately sends a request to the LLM and returns the response.
196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/llm/context.rb', line 196 def talk(prompt, params = {}) @owner = @llm.request_owner compactor.compact!(prompt) if compactor.compact?(prompt) repair!(@messages, prompt) prompt, params, res = mode == :responses ? respond(prompt, params) : complete(prompt, params) self.compacted = false role = params[:role] || @llm.user_role role = @llm.tool_role if params[:role].nil? && [*prompt].grep(LLM::Function::Return).any? @messages.concat LLM::Prompt === prompt ? prompt.to_a : [LLM::Message.new(role, prompt)] @messages.concat [res.choices[-1]].compact res end |
#to_h ⇒ Hash
448 449 450 451 452 453 454 455 |
# File 'lib/llm/context.rb', line 448 def to_h { schema_version: 1, model:, compacted:, messages: @messages.map { (_1) } } end |
#to_json ⇒ String
459 460 461 |
# File 'lib/llm/context.rb', line 459 def to_json(...) LLM.json.dump(to_h, ...) end |
#tracer ⇒ LLM::Tracer
Returns an LLM tracer
420 421 422 |
# File 'lib/llm/context.rb', line 420 def tracer @llm.tracer end |
#tracer=(other) ⇒ void
This method returns an undefined value.
428 429 430 |
# File 'lib/llm/context.rb', line 428 def tracer=(other) @llm.tracer = nil end |
#transformer ⇒ #call?
Returns a transformer, if configured.
Transformers can rewrite outgoing prompts and params before a request is sent to the provider.
169 170 171 |
# File 'lib/llm/context.rb', line 169 def transformer @transformer end |
#transformer=(transformer) ⇒ #call?
Sets a transformer.
Transformers must implement call(ctx, prompt, params) and return a
two-element array of [prompt, params].
181 182 183 |
# File 'lib/llm/context.rb', line 181 def transformer=(transformer) @transformer = transformer end |
#usage ⇒ LLM::Object
Returns token usage accumulated in this context
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/llm/context.rb', line 349 def usage if usage = @messages.find(&:assistant?)&.usage LLM::Object.from( input_tokens: usage.input_tokens || 0, output_tokens: usage.output_tokens || 0, reasoning_tokens: usage.reasoning_tokens || 0, input_audio_tokens: usage.input_audio_tokens || 0, output_audio_tokens: usage.output_audio_tokens || 0, input_image_tokens: usage.input_image_tokens || 0, cache_read_tokens: usage.cache_read_tokens || 0, cache_write_tokens: usage.cache_write_tokens || 0, total_tokens: usage.total_tokens || 0 ) else ZERO_USAGE end end |
#wait(strategy, except: []) ⇒ Array<LLM::Function::Return>
Waits for queued tool work to finish.
This prefers queued streamed tool work when the configured stream exposes a non-empty queue. Otherwise it falls back to waiting on the context's pending functions directly.
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/llm/context.rb', line 312 def wait(strategy, except: []) if LLM::Stream === stream && !stream.queue.empty? @queue = stream.queue @queue.wait else tools = except.empty? ? functions : functions - except guards = guarded_returns(tools:) return guards if guards @queue = tools.spawn(strategy) returns = @queue.wait emit_tool_returns(tools, returns) returns end ensure @queue = nil @stream = nil end |