Use different reasoning strategies to optimize agent behavior
Reasoning engines control how agents process inputs, use tools, and generate outputs. AgentLIB provides five built-in engines, each optimized for different task types.
Every agent uses a reasoning engine to orchestrate its behavior. If you don’t specify one, agents use a simple single-shot completion. For multi-step tasks with tools, you’ll want to choose an appropriate engine.
import { createAgent } from '@agentlib/core'import { ReactEngine } from '@agentlib/reasoning'const agent = createAgent({ name: 'agent' }) .provider(model) .reasoning(new ReactEngine({ maxSteps: 8 })) // or shorthand: .reasoning('react')
Best for: Tool-using agents, most general-purpose tasksReAct (Reasoning + Acting) interleaves thinking and tool calls. The agent reasons about what to do, calls a tool, observes the result, and repeats until it has enough information to answer.
1
Import and Configure
import { createAgent, defineTool } from '@agentlib/core'import { openai } from '@agentlib/openai'import { ReactEngine } from '@agentlib/reasoning'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}`] } },})
agent.on('step:reasoning', (step) => { console.log(`[${step.type}]`, step.type === 'thought' ? step.content : '')})const result = await agent.run('What is the population of Tokyo?')console.log(result.output)
Best for: Complex reasoning problems, math, logic puzzlesChain-of-Thought (CoT) forces the model to think explicitly before answering. The thinking is captured for observability but stripped from the final output.
import { ChainOfThoughtEngine } from '@agentlib/reasoning'const agent = createAgent({ name: 'cot-agent' }) .provider(model) .reasoning(new ChainOfThoughtEngine({ useThinkingTags: true, // Use <thinking> tags for clarity maxToolSteps: 3, // Allow limited tool use }))agent.on('step:reasoning', (step) => { if (step.type === 'thought') { console.log('💭 Thinking:', step.content) }})const result = await agent.run( 'If a train travels 120km in 1.5 hours, what is its average speed? ' + 'Is that faster or slower than a car driving at 90km/h?')console.log(result.output)
Best for: Multi-step tasks, decomposable workflows, research tasksThe Planner engine first creates a structured plan (JSON list of subtasks), executes each subtask with tool access, then synthesizes all results into a final answer.
agent.on('step:reasoning', (step) => { if (step.type === 'plan') { console.log('📋 Plan:') step.tasks.forEach((t) => console.log(` [${t.id}] ${t.description}`)) } if (step.type === 'thought') { console.log('▶', step.content) }})const result = await agent.run( 'Research the top 3 JavaScript frameworks in 2025 and write a comparison report to /tmp/frameworks.md')console.log(result.output)
Best for: High-stakes tasks, accuracy-critical outputs, writingThe Reflect engine generates an answer, self-critiques it (score 0-10), and revises if the score falls below the threshold. Slower but produces more reliable output.
import { ReflectEngine } from '@agentlib/reasoning'const agent = createAgent({ name: 'reflect-agent' }) .provider(model) .reasoning(new ReflectEngine({ maxReflections: 2, // Maximum revision cycles acceptanceThreshold: 8, // Minimum score to accept (0-10) }))agent.on('step:reasoning', (step) => { if (step.type === 'reflection') { console.log(`🔍 Reflection: ${step.assessment}`) console.log(` Needs revision: ${step.needsRevision}`) }})const result = await agent.run( 'Explain the CAP theorem and its implications for distributed systems design.')console.log(result.output)console.log('\nSteps taken:', result.state.steps.length)
Best for: Long-horizon tasks, open-ended exploration, research agentsThe Autonomous engine runs indefinitely until it explicitly calls a built-in finish tool. Suitable for agents that need to explore before they know when they’re done.
import { AutonomousEngine } from '@agentlib/reasoning'const agent = createAgent({ name: 'autonomous-agent', systemPrompt: 'You are an autonomous research assistant. Work step by step. When you have a complete answer, call the finish tool.',}) .provider(model) .tool(searchTool) .tool(readFileTool) .reasoning(new AutonomousEngine({ maxSteps: 25 })) .policy({ maxSteps: 25, tokenBudget: 50_000 })agent.on('step:reasoning', (step) => { if (step.type === 'thought') console.log('🤖', step.content.slice(0, 120)) if (step.type === 'tool_call') console.log(`🔧 ${step.toolName}(${JSON.stringify(step.args).slice(0, 60)})`)})const result = await agent.run( 'Find and summarize recent developments in quantum computing from the last 6 months.')console.log('\n✅ Final answer:')console.log(result.output)