Skip to content

Support block/proc for dynamic model selection in Agent #676

@kryzhovnik

Description

@kryzhovnik

Feature Request

Allow the model class method in RubyLLM::Agent to accept a block for dynamic model selection at runtime, consistent with how tools, params, headers, and schema already support blocks.

Use case

Selecting a model based on runtime context — e.g., routing simple tasks to a cheaper model:

class CardAgent < RubyLLM::Agent
  inputs :card
  model { card.special_type? ? 'gpt-4.1-mini' : 'gpt-4.1-nano' }
  instructions 'You are helpful.'
end

CardAgent.chat(card: @card)

Current behavior

model only accepts a string:

model 'gpt-4.1-nano'

Proposed behavior

model also accepts a block, evaluated in the same runtime context as other block-based DSLs (tools, params, etc.), with access to declared inputs:

inputs :quality
model { quality == :high ? 'gpt-4.1-mini' : 'gpt-4.1-nano' }

Implementation

I have a working implementation ready (~13 lines of production code) that follows the existing apply_* pattern. Happy to open a PR if this aligns with the project's direction.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions