Build / AI /

Chain of Thought

Elicit step-by-step reasoning, mathematical problem solving, and complex logic through explicit chain-of-thought prompting.

TL;DR

  1. Instruct complex models to think step-by-step using chain-of-thought prompts.
  2. Isolate intermediate calculation scratchpads inside explicit <thinking> XML blocks.
  3. Sample multiple reasoning paths and select consensus via self-consistency.

Reasoning Elicitation Triggers

    Zero-Shot CoT Trigger

    Classic heuristic phrase activating step-by-step inference.

    Think step-by-step before answering.
    Structured Scratchpad Prompt

    Direct intermediate logic into dedicated reasoning enclosure.

    First, outline steps in <thinking>...</thinking> tags.
    Then provide final answer in <answer>.
    Algorithmic Verification Step

    Instruct model to audit its own calculation before concluding.

    Verify math steps for errors
    before emitting final sum.

Scratchpad Parsing In TypeScript

    Extract Thinking Block

    Isolate reasoning tokens from user-facing answer via regex.

    function extractCoT(raw: string) {
      const p = /<thinking>([\s\S]*?)<\/thinking>/;
      const think = raw.match(p);
      const ans = raw.replace(p, '');
      return {
        reasoning: think?.[1]?.trim(),
        answer: ans.trim(),
      };
    }
    Stream Parsing Scratchpad

    Differentiate live reasoning tokens from final markdown output.

    let inThinking = false;
    if (chunk.includes('<thinking>')) inThinking = true;
    if (chunk.includes('</thinking>')) inThinking = false;
    Collapsible UI Renderer

    Render thinking process in an expandable disclosure component.

    // Collapsible UI pattern
    // Render thought disclosure header
    // Display reasoning scratchpad
    // Render final answer

Self-Consistency Sampling

    Parallel Sample Generation

    Dispatch multiple parallel completion calls at higher temperature.

    const runs = [1, 2, 3, 4, 5];
    const samples = await Promise.all(runs.map(() =>
      client.chat.completions.create({
        model: 'gpt-4o',
        messages: [{ role: 'user', content: mathPrompt }],
        temperature: 0.7,
      })
    ));
    Majority Voting Algorithm

    Select final answer supported by consensus of reasoning paths.

    function majorityVote(answers: string[]): string {
      const counts: Record<string, number> = {};
      for (const a of answers) {
        counts[a] = (counts[a] || 0) + 1;
      }
      return Object.keys(counts).reduce((a, b) =>
        counts[a] > counts[b] ? a : b
      );
    }
    Outlier Rejection

    Filter out reasoning paths that failed basic consistency checks.

    const valid = samples.filter(s => isValidAnswer(s));
    const winner = majorityVote(valid);

Frontier Reasoning Integration

    OpenAI Reasoning Effort

    Tune reasoning token budget on models like o3-mini.

    const res = await openai.chat.completions.create({
      model: 'o3-mini',
      reasoning_effort: 'medium', // low | medium | high
      messages: [{ role: 'user', content: logicPuzzle }],
    });
    Anthropic Extended Thinking

    Allocate explicit thinking budget in tokens for Claude.

    const res = await anthropic.messages.create({
      model: 'claude-3-7-sonnet-20250219',
      max_tokens: 4000,
      thinking: { type: 'enabled', budget_tokens: 2048 },
      messages: [{ role: 'user', content: puzzle }],
    });
    Reasoning Token Metrics

    Monitor billable hidden thinking tokens in response usage.

    const reasoningTokens =
      (res.usage as any)?.completion_tokens_details
        ?.reasoning_tokens ?? 0;
    console.log(`Reasoning tokens: ${reasoningTokens}`);

Tips

  1. Direct the model to articulate its complete logical deduction into explicit intermediate-tokens before emitting final answers.
  2. Parse out and hide internal <thinking> scratchpad tags before displaying final responses in customer-facing user interfaces.

Warnings

  1. Do not demand immediate answers without allowing intermediate reasoning-tokens because autoregressive models cannot perform multi-step math instantly.
  2. Be aware that deep reasoning models like o3-mini generate internal hidden reasoning tokens that bill as standard output tokens.

In Practice

FAQ