Jido.AI.AdaptiveAgent (Jido AI v2.2.0)

Copy Markdown View Source

Base macro for Adaptive strategy-powered agents.

Wraps use Jido.Agent with Jido.AI.Reasoning.Adaptive.Strategy wired in, plus standard state fields and helper functions.

Usage

defmodule MyApp.SmartAssistant do
  use Jido.AI.AdaptiveAgent,
    name: "smart_assistant",
    description: "Automatically selects the best reasoning approach",
    default_strategy: :react,
    available_strategies: [:cod, :cot, :react, :tot, :got, :trm]
end

Options

  • :name (required) - Agent name
  • :description - Agent description (default: "Adaptive agent #{name}")
  • :model - Model alias or direct model spec (default: :fast, resolved via Jido.AI.resolve_model/1)
  • :tools - Tool modules used when Adaptive selects ReAct
  • :default_strategy - Default strategy if analysis is inconclusive (default: :react)
  • :available_strategies - List of available strategies (default: [:cod, :cot, :react, :tot, :got, :trm])
  • :complexity_thresholds - Map of thresholds for strategy selection
  • :skills - Additional skills to attach to the agent (TaskSupervisorSkill is auto-included)

Generated Functions

  • ask/2,3 - Async: sends query, returns {:ok, %Request{}} for later awaiting
  • await/1,2 - Awaits a specific request's completion
  • ask_sync/2,3 - Sync convenience: sends query and waits for result
  • strategy_opts/0 - Returns the strategy options (for CLI access)
  • on_before_cmd/2 - Captures request in state before processing
  • on_after_cmd/3 - Updates request result when done

Request Tracking

Each ask/2 call returns a Request struct that can be awaited:

{:ok, request} = MyAgent.ask(pid, "Solve this puzzle: ...")
{:ok, result} = MyAgent.await(request, timeout: 30_000)

Or use the synchronous convenience wrapper:

{:ok, result} = MyAgent.ask_sync(pid, "Solve this puzzle: ...")

State Fields

The agent state includes:

  • :model - The LLM model being used
  • :requests - Map of request_id => request state (for concurrent tracking)
  • :last_request_id - ID of the most recent request
  • :last_prompt - The most recent prompt (backward compat)
  • :last_result - The final result from the last completed reasoning (backward compat)
  • :completed - Boolean indicating if the last reasoning is complete (backward compat)
  • :selected_strategy - The strategy type selected for the current task

Task Supervisor

Each agent instance gets its own Task.Supervisor automatically started via the Jido.AI.Plugins.TaskSupervisor. This supervisor is used for:

  • LLM streaming operations
  • Other async operations within the agent's lifecycle

Example

{:ok, pid} = Jido.AgentServer.start(agent: MyApp.SmartAssistant)

# Async pattern (preferred for concurrent requests)
{:ok, request} = MyApp.SmartAssistant.ask(pid, "Solve this puzzle: ...")
{:ok, result} = MyApp.SmartAssistant.await(request)

# Sync pattern (convenience for simple cases)
{:ok, result} = MyApp.SmartAssistant.ask_sync(pid, "Solve this puzzle: ...")

# Check the selected strategy
agent = Jido.AgentServer.get(pid)
agent.state.selected_strategy # => :trm

Strategy Selection

The Adaptive strategy automatically selects the best approach based on task analysis:

  • Algorithmic Exploration (opt-in) → AoT (when :aot is included in available_strategies)
  • Uses single-query algorithmic search formatting with explicit finalization
  • Iterative Reasoning → TRM (puzzles, step-by-step, recursive)
  • Synthesis → Graph-of-Thoughts (combine, merge, perspectives)
  • Tool use → ReAct (search, calculate, execute)
  • Exploration → Tree-of-Thoughts (analyze, compare, alternatives)
  • Simple tasks → Chain-of-Draft (direct questions, factual queries)