class RubyLLM::Task

A tool call that finishes later. Return one from Tool#execute and a chat pauses the call instead of waiting for it, the way it pauses for an approval. Chat#pending_tasks returns the tasks it waits on, and each Chat#complete hands every one back to its tool’s Tool#resume, which returns the call’s result once it has one:

class RunSubAgent < RubyLLM::Tool
  description "Hands a job to a sub-agent"

  def execute(job:)
    run = SubAgentRun.start(job)
    run.finished? ? run.answer : RubyLLM::Task.new(status: :input_required, data: { "run_id" => run.id })
  end

  def resume(task, **)
    run = SubAgentRun.find(task.data["run_id"]).continue
    run.finished? ? run.answer : task
  end
end

A task is saved with its tool call as to_h, so a Rails chat can pause in one process and resume in another. MCP::Task is the task of an MCP tool call that a server runs in the background.