Skip to main content

Overview

The ReactEngine implements the canonical Reason + Act (ReAct) pattern for agent reasoning. It executes a simple loop where the model thinks, optionally calls tools, processes results, and repeats until a final answer is reached. This is the default reasoning engine when no strategy is configured. Paper: “ReAct: Synergizing Reasoning and Acting in Language Models” (Yao et al., 2022)

When to Use

Use ReactEngine when:
  • You need a straightforward, reliable reasoning loop
  • Your task involves iterative tool use (search, calculate, API calls)
  • You want the model to decide when it has gathered enough information
  • You don’t need explicit planning or reflection

Constructor

import { ReactEngine } from '@agentlib/reasoning'

const engine = new ReactEngine(config)

Configuration

maxSteps
number
default:10
Maximum number of reasoning steps before forcing a final answer. Each step includes a model call and optional tool execution.

Usage Example

import { Agent } from '@agentlib/core'
import { ReactEngine } from '@agentlib/reasoning'
import { searchTool, calculatorTool } from './tools'

const agent = new Agent({
  name: 'research-agent',
  model: myModelProvider,
  tools: [searchTool, calculatorTool],
  reasoning: new ReactEngine({ maxSteps: 8 })
})

const result = await agent.run({
  input: 'Find the current price of Bitcoin and calculate 5% of it'
})

How It Works

  1. Think: Model receives messages and generates a response (with optional tool calls)
  2. Act: If tool calls are present, execute them and append results to conversation
  3. Repeat: Loop back to step 1 with updated context
  4. Terminate: When no tool calls are made, return the model’s final response

Step Emissions

  • ThoughtStep: Emitted when the model generates content alongside tool calls
  • ResponseStep: Emitted when the final answer is produced

Error Handling

Throws an error if maxSteps is reached without the model producing a final answer:
[ReactEngine] Max steps (10) reached without a final answer.

String Alias

You can use the string alias 'react' instead of instantiating the class:
const agent = new Agent({
  name: 'my-agent',
  reasoning: 'react' // Uses default ReactEngine
})

Implementation Reference

Source: packages/reasoning/src/engines/react.ts:31

Build docs developers (and LLMs) love