Skip to main content

Overview

The ChainOfThoughtEngine forces the model to reason through problems explicitly before answering. It uses XML-style <thinking> tags to separate the model’s internal reasoning from its final response. This engine is ideal for tasks requiring careful, methodical analysis.

When to Use

Use ChainOfThoughtEngine when:
  • Tasks require explicit step-by-step reasoning
  • You want to observe the model’s thought process
  • Accuracy is more important than speed
  • You need transparent decision-making for debugging or auditing

Constructor

import { ChainOfThoughtEngine } from '@agentlib/reasoning'

const engine = new ChainOfThoughtEngine(config)

Configuration

useThinkingTags
boolean
default:true
Whether to use XML-style <thinking> tags to separate reasoning from response. When true, the engine strips the thinking block before returning the final answer.
maxToolSteps
number
default:5
Maximum tool-using steps after the initial chain-of-thought reasoning pass.
thinkingInstruction
string
Custom instruction appended to the system prompt to elicit step-by-step reasoning. If not provided, uses a default prompt that requests thinking inside <thinking> tags.

Usage Example

import { Agent } from '@agentlib/core'
import { ChainOfThoughtEngine } from '@agentlib/reasoning'

const agent = new Agent({
  name: 'math-tutor',
  model: myModelProvider,
  reasoning: new ChainOfThoughtEngine({
    maxToolSteps: 3,
    thinkingInstruction: 'Solve this step by step, showing all work inside <thinking> tags.'
  })
})

const result = await agent.run({
  input: 'If a train travels 120 km in 2 hours, how far will it go in 5 hours?'
})

How It Works

  1. Inject Instruction: Adds chain-of-thought prompt to system message
  2. Initial Reasoning: Model produces <thinking>...</thinking> + answer (or tool calls)
  3. Extract Thinking: Thinking content is extracted and emitted as a ThoughtStep
  4. Tool Loop: If tools were called, execute them and continue (up to maxToolSteps)
  5. Return Clean Answer: Final response excludes thinking tags

Default Thinking Instruction

Before answering, reason step by step inside <thinking> tags.
Work through the problem carefully, considering all relevant information.
Then provide your final answer outside the tags.

Step Emissions

  • ThoughtStep: Contains the extracted <thinking> content
  • ResponseStep: Contains the final answer (thinking tags removed)

Example Output Flow

Model generates:
<thinking>
1. Train speed: 120 km / 2 hours = 60 km/h
2. Distance in 5 hours: 60 km/h × 5 hours = 300 km
</thinking>

The train will travel 300 km in 5 hours.
ThoughtStep emitted:
{
  "type": "thought",
  "content": "1. Train speed: 120 km / 2 hours = 60 km/h\n2. Distance in 5 hours: 60 km/h × 5 hours = 300 km",
  "engine": "cot"
}
Final return:
The train will travel 300 km in 5 hours.

Error Handling

Throws if maximum tool steps are exceeded:
[ChainOfThoughtEngine] Max tool steps (5) reached.

String Alias

const agent = new Agent({
  name: 'my-agent',
  reasoning: 'cot' // Uses default ChainOfThoughtEngine
})

Implementation Reference

Source: packages/reasoning/src/engines/cot.ts:53

Build docs developers (and LLMs) love