Skip to main content

Overview

The ReAct (Reasoning + Acting) engine combines reasoning traces with tool execution in an interleaved manner. The agent alternates between thinking about what to do next and taking actions using available tools. This approach is particularly effective for:
  • Multi-step tasks requiring both reasoning and external information
  • Problems where actions depend on previous results
  • Scenarios requiring dynamic decision-making

How It Works

  1. Thought: Agent reasons about the current situation and decides what action to take
  2. Action: Agent calls a tool with specific parameters
  3. Observation: Agent receives and processes the tool result
  4. Repeat: Continue the thought-action-observation cycle until the task is complete

Complete Example

This example shows a ReAct agent that can search the web and perform calculations:
import "dotenv/config"

import {  createAgent, AgentInstance, defineTool, ReasoningStep  } from '@agentlib/core'
import { openai } from '@agentlib/openai'
import { ReactEngine } from '@agentlib/reasoning'

const model = openai({ apiKey: process.env['OPENAI_API_KEY']!, model: process.env['OPENAI_MODEL']!, baseURL: process.env['OPENAI_BASE_URL']! })

const searchTool = defineTool({
    schema: {
        name: 'search',
        description: 'Search the web for information',
        parameters: {
            type: 'object',
            properties: { query: { type: 'string' } },
            required: ['query'],
        },
    },
    async execute({ query }) {
        return { results: [`Result for: ${query}`] }
    },
})

const calculatorTool = defineTool({
    schema: {
        name: 'calculator',
        description: 'Evaluate a math expression',
        parameters: {
            type: 'object',
            properties: { expression: { type: 'string' } },
            required: ['expression'],
        },
    },
    async execute({ expression }) {
        return { result: eval(String(expression)) }
    },
})

async function main() {
    const agent = createAgent({ name: 'react-agent' })
        .provider(model)
        .tool(searchTool)
        .tool(calculatorTool)
        .reasoning(new ReactEngine({ maxSteps: 8 }))

    agent.on('step:reasoning', (step: ReasoningStep) => {
        console.log(`[${step.type}]`, step.type === 'thought' ? step.content : '')
    })

    const result = await agent.run('What is the population of Tokyo multiplied by 2?')
    console.log('Final output:', result.output)
}

main().catch(console.error)

Configuration Options

  • maxSteps: Maximum number of reasoning steps before stopping (default: 10)

Monitoring Reasoning Steps

Listen to the step:reasoning event to observe the agent’s thought process:
agent.on('step:reasoning', (step: ReasoningStep) => {
    if (step.type === 'thought') {
        console.log('Thinking:', step.content)
    } else if (step.type === 'tool_call') {
        console.log('Action:', step.toolName)
    }
})

When to Use ReAct

Use the ReAct engine when:
  • Your task requires multiple tool calls in sequence
  • Each action depends on understanding previous results
  • You need transparent reasoning traces for debugging
  • The agent needs to adapt its strategy based on observations

Build docs developers (and LLMs) love