Skip to main content

Overview

Chain-of-Thought (CoT) reasoning forces the model to explicitly articulate its thinking process before generating a final answer. This leads to more accurate and well-reasoned responses, especially for complex problems. This approach is particularly effective for:
  • Mathematical reasoning and calculations
  • Logical deduction and problem-solving
  • Multi-step analysis tasks
  • Scenarios requiring explicit reasoning traces

How It Works

  1. Thinking Phase: Agent generates explicit reasoning steps using thinking tags
  2. Answer Phase: Agent produces the final answer based on its reasoning
  3. Optional Tool Use: Can make tool calls during reasoning if configured

Complete Example

This example shows a Chain-of-Thought agent solving a multi-part math problem:
import "dotenv/config"

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

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

async function main() {
    const agent = createAgent({ name: 'cot-agent' })
        .provider(model)
        .reasoning(new ChainOfThoughtEngine({
            useThinkingTags: true,
            maxToolSteps: 3,
        }))

    agent.on('step:reasoning', (step: ReasoningStep) => {
        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('Final output:', result.output)
}

main().catch(console.error)

Configuration Options

  • useThinkingTags: Enable explicit thinking tags in the model output (default: false)
  • maxToolSteps: Maximum number of tool calls allowed during reasoning (default: 0)

Monitoring Reasoning

Observe the agent’s thinking process:
agent.on('step:reasoning', (step: ReasoningStep) => {
    if (step.type === 'thought') {
        console.log('πŸ’­ Thinking:', step.content)
    }
})

Example Output

For the train speed question, you might see:
πŸ’­ Thinking: First, I need to calculate the train's average speed.
πŸ’­ Thinking: Speed = Distance / Time = 120km / 1.5 hours = 80 km/h
πŸ’­ Thinking: Now comparing: 80 km/h (train) vs 90 km/h (car)
πŸ’­ Thinking: The train is slower than the car.

Final output: The train's average speed is 80 km/h, which is slower than the car traveling at 90 km/h.

When to Use Chain-of-Thought

Use Chain-of-Thought when:
  • You need explicit reasoning for transparency or debugging
  • The task involves complex logical steps
  • Accuracy is more important than speed
  • You want to understand how the agent reached its conclusion
  • The problem requires breaking down into smaller sub-problems

Build docs developers (and LLMs) love