Tree of Thoughts

advanced15 mintree-of-thought.ts

A design question has many plausible answers. You want several branches explored and scored before committing to one.

What it shows

  • Configure depth, branching and beam width on ThoughtTreeExecutor
  • Read the best path and stats

The code

tree-of-thought.ts
import { Agent, Cogitator, ThoughtTreeExecutor } from '@cogitator-ai/core';

const apiKey = process.env.GOOGLE_API_KEY;
if (!apiKey) throw new Error('Set GOOGLE_API_KEY');

const cog = new Cogitator({ llm: { providers: { google: { apiKey } } } });

const architect = new Agent({
  name: 'architect',
  model: 'google/gemini-3.5-flash-lite',
  instructions: `You are a senior software architect specializing in distributed systems.
Think through trade-offs: scalability, latency, consistency and operational complexity.
Be concrete about technologies, data structures and protocols.`,
  temperature: 0.7,
  maxIterations: 5,
});

const tot = new ThoughtTreeExecutor(cog, {
  maxDepth: 2,
  branchFactor: 2,
  beamWidth: 2,
  confidenceThreshold: 0.2,
  terminationConfidence: 0.85,
  maxTotalNodes: 12,
  timeout: 120_000,
  onBranchEvaluated: (branch, score) =>
    console.log(`[eval] ${branch.thought.slice(0, 60)}... -> ${score.composite.toFixed(2)}`),
});

const result = await tot.explore(
  architect,
  'Design a caching strategy for a social media feed with 10M daily active users. ' +
    'Consider cache invalidation, personalization and cold start for new users.'
);

console.log(`Explored ${result.stats.exploredNodes} nodes, pruned ${result.stats.prunedNodes}, ${result.stats.llmCalls} LLM calls`);
for (const node of result.bestPath) {
  console.log(`depth ${node.depth}: ${node.branch.thought.slice(0, 100)}`);
}
console.log(result.output);

await cog.close();

Run it

install · bash
pnpm add @cogitator-ai/core
EnvironmentGOOGLE_API_KEY
run · bash
GOOGLE_API_KEY=your-key npx tsx tree-of-thought.ts

Or run the full example from a clone of the repository (keys in .env):

repository · bash
npx tsx examples/core/05-tree-of-thought.ts

Learn more