AgentX
Menu

Reasoning Framework

The reasoning framework provides structured thinking strategies for complex tasks. An orchestrator classifies tasks and selects the appropriate strategy automatically.

Architecture

graph TD
    T[Task Input] --> O[Orchestrator]
    O --> CL[_classify_task]
    CL --> |math, analytical| CoT[Chain-of-Thought]
    CL --> |planning| ToT[Tree-of-Thought]
    CL --> |research| ReAct[ReAct]
    CL --> |creative, code| Ref[Reflection]

    CoT --> P[Provider]
    ToT --> P
    ReAct --> P
    Ref --> P
    ReAct --> TE[Tool Executor]

    P --> R[ReasoningResult]

    CoT -.-> |fallback| FB[Fallback Strategy]
    ToT -.-> |fallback| FB
    ReAct -.-> |fallback| FB
    Ref -.-> |fallback| FB

Strategies

StrategyClassUse CaseHow It Works
Chain-of-ThoughtChainOfThoughtMath, analytical, simpleStep-by-step reasoning with explicit intermediate steps
Tree-of-ThoughtTreeOfThoughtPlanning, designExplores multiple solution branches (BFS/DFS/beam search)
ReActReActAgentResearch, tool-heavy tasksInterleaves reasoning with tool actions (Reason-Act-Observe loop)
ReflectionReflectiveReasonerCreative, codeGenerates, self-critiques, and revises through multiple rounds

Task Classification

The orchestrator classifies tasks by keyword matching in _classify_task():

Task TypeKeywordsDefault Strategy
mathcalculate, compute, solve, equation, formulaCoT
codecode, program, function, implement, debugReflection
researchsearch, find information, look up, researchReAct
planningplan, design, strategy, organize, roadmapToT
creativewrite, create, compose, draft, story, poemReflection
analyticalanalyze, compare, evaluate, assess, reviewCoT
simplewhat is, who is, when did, define, listCoT
unknown(no match)CoT (fallback)

Strategy Configs

Chain-of-Thought (CoTConfig)

FieldDefaultDescription
model"llama3.2"Model to use
mode"zero_shot""zero_shot" or "few_shot"
examples[]Few-shot examples (if few_shot mode)
max_steps10Maximum reasoning steps

Tree-of-Thought (ToTConfig)

FieldDefaultDescription
model"llama3.2"Model to use
branching_factor3Branches per node
max_depth3Maximum tree depth
search_strategy"bfs""bfs", "dfs", or "beam"
beam_width2Beam width (if beam search)

ReAct (ReActConfig)

FieldDefaultDescription
model"llama3.2"Model to use
tools[]Available Tool objects
max_iterations10Max reason-act-observe cycles

Reflection (ReflectionConfig)

FieldDefaultDescription
model"llama3.2"Model to use
max_revisions3Maximum self-revision rounds
critique_modelnullOptional separate model for critique

Result Structure

ReasoningResult contains:

FieldTypeDescription
answerstringFinal response
strategystringStrategy used
statusReasoningStatus"complete" or "failed"
stepslist[ThoughtStep]Full reasoning trace
total_stepsintStep count
branches_exploredintToT branches explored
actions_takenintReAct actions executed
revisionsintReflection revision count
total_tokensintToken usage
total_time_msfloatElapsed time

Orchestrator Config

from agentx_ai.reasoning.orchestrator import ReasoningOrchestrator, OrchestratorConfig

config = OrchestratorConfig(
    default_model="llama3.2",
    fallback_strategy="cot",    # Fallback if primary fails
    enable_fallback=True,
)

orchestrator = ReasoningOrchestrator(config)
result = await orchestrator.reason("Plan a week-long trip to Japan")
# result.strategy == "tot" (auto-classified as planning)

The strategy map can be overridden in OrchestratorConfig.strategy_map to change which strategy handles each task type.

Integration

The Agent uses reasoning through Agent.run():

  1. Agent receives a task
  2. TaskPlanner decomposes into subtasks
  3. For each subtask, ReasoningOrchestrator.reason() is called
  4. Results are aggregated into the final AgentResult

In chat mode (Agent.chat(simple_mode=True)), reasoning is bypassed — the agent uses direct provider completion instead.