Table of Contents

Class ChainOfThoughtStrategy<T>

Namespace
AiDotNet.Reasoning.Strategies
Assembly
AiDotNet.dll

Implements Chain-of-Thought (CoT) reasoning that solves problems through explicit step-by-step thinking.

public class ChainOfThoughtStrategy<T> : ReasoningStrategyBase<T>, IReasoningStrategy<T>

Type Parameters

T

The numeric type used for scoring (e.g., double, float).

Inheritance
ChainOfThoughtStrategy<T>
Implements
Inherited Members

Remarks

For Beginners: Chain-of-Thought (CoT) is a reasoning approach where the AI explicitly shows its work, step by step, similar to how you would solve a math problem by writing down each step.

How it works: Given: "What is 15% of 240?"

Step 1: Convert percentage to decimal

  • 15% = 15/100 = 0.15

Step 2: Multiply by the number

  • 0.15 × 240 = 36

Step 3: State the answer

  • The answer is 36

Why it's effective:

  • Makes reasoning transparent and verifiable
  • Catches logical errors early
  • Improves accuracy on complex problems
  • Allows debugging when answers are wrong

Based on research: "Chain-of-Thought Prompting Elicits Reasoning in Large Language Models" (Wei et al., 2022) showed 3-5x improvements on reasoning tasks when models show their work.

Example Usage:

var chatModel = new OpenAIChatModel<double>("gpt-4");
var strategy = new ChainOfThoughtStrategy<double>(chatModel);

var result = await strategy.ReasonAsync(
    "If a train travels 60 mph for 2.5 hours, how far does it go?",
    new ReasoningConfig()
);

Console.WriteLine(result.FinalAnswer); // "150 miles"
Console.WriteLine(result.ReasoningChain); // Shows all steps

Constructors

ChainOfThoughtStrategy(IChatModel<T>, IEnumerable<ITool>?, bool)

Initializes a new instance of the ChainOfThoughtStrategy<T> class.

public ChainOfThoughtStrategy(IChatModel<T> chatModel, IEnumerable<ITool>? tools = null, bool useJsonFormat = true)

Parameters

chatModel IChatModel<T>

The chat model used for reasoning.

tools IEnumerable<ITool>

Optional tools available during reasoning.

useJsonFormat bool

Whether to request JSON-formatted responses (more reliable parsing).

Remarks

For Beginners: Creates a Chain-of-Thought reasoning strategy. The chatModel is the AI that does the thinking, tools are optional helpers (like calculators), and useJsonFormat controls whether responses are structured (recommended for reliability).

Properties

Description

Gets a description of what this reasoning strategy does and when to use it.

public override string Description { get; }

Property Value

string

Remarks

For Beginners: This provides information about what makes this strategy unique and what types of problems it's best suited for. For example, Chain-of-Thought might describe itself as "Best for problems requiring step-by-step logical deduction."

StrategyName

Gets the name of this reasoning strategy.

public override string StrategyName { get; }

Property Value

string

Remarks

For Beginners: This is a human-readable name that identifies the strategy, like "Chain-of-Thought" or "Tree-of-Thoughts". It's useful for logging, debugging, or displaying to users which reasoning approach was used.

Methods

ReasonCoreAsync(string, ReasoningConfig, CancellationToken)

Core reasoning logic to be implemented by derived strategies.

protected override Task<ReasoningResult<T>> ReasonCoreAsync(string query, ReasoningConfig config, CancellationToken cancellationToken)

Parameters

query string

The problem or question to reason about.

config ReasoningConfig

Configuration options for the reasoning process.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<ReasoningResult<T>>

A reasoning result containing the answer and reasoning process.

Remarks

For Beginners: This is where the actual reasoning happens - and it's different for each strategy. Derived classes must implement this method with their specific approach: - ChainOfThoughtStrategy: Linear step-by-step reasoning - TreeOfThoughtsStrategy: Explores multiple paths in a tree - SelfConsistencyStrategy: Multiple attempts with voting

The base class handles setup, timing, and error handling; derived classes focus on implementing their unique reasoning algorithm.