/* What does ONE supervisor request actually cost in input tokens? */ import dotenv from 'dotenv'; dotenv.config(); import { supervisorPrompt } from '../src/orchestration/supervisor.js'; import { delegationToolsFor, agentMenuFor, writeToolsFor } from '../src/agents/factory.js'; import { artifactTools } from '../src/tools/artifacts/index.js'; import { connectMongo } from '../src/data/mongo.js'; import { toJsonSchema } from '@langchain/core/utils/json_schema'; await connectMongo(); const user = { id: '6a00cefe524bebd27037a968', name: 'Thulasiraman S', role: 'admin', permissions: [] }; const system = supervisorPrompt({ user, agentMenu: agentMenuFor(user), now: new Date(), channel: 'crm_chat', writeTools: writeToolsFor(user), }); const tools = [...delegationToolsFor(user), ...artifactTools, ...writeToolsFor(user)]; const apiTools = tools.map((t) => ({ name: t.name, description: t.description, input_schema: toJsonSchema(t.schema), })); // count_tokens needs credit, so estimate locally. // ~3.6 chars/token is a good approximation for English prose + JSON schemas. const CHARS_PER_TOKEN = 3.6; const est = (str) => Math.round(str.length / CHARS_PER_TOKEN); const toolsJson = JSON.stringify(apiTools); const perCall = est(system) + est(toolsJson) + 20; console.log(`system prompt : ${system.length.toLocaleString()} chars → ~${est(system).toLocaleString()} tokens`); console.log(`tool schemas : ${toolsJson.length.toLocaleString()} chars → ~${est(toolsJson).toLocaleString()} tokens`); console.log(`tool count : ${tools.length}`); console.log(`\nINPUT TOKENS PER SUPERVISOR CALL: ${perCall.toLocaleString()}`); // Opus 5: $5 / MTok input, $25 / MTok output const IN = 5 / 1e6; const OUT = 25 / 1e6; console.log(`\nOpus 5 input cost, one call: $${(perCall * IN).toFixed(4)}`); console.log('\n─ A ReAct loop re-sends the whole prompt every iteration ─'); for (const iters of [3, 6, 10, 20]) { // Input grows each iteration as tool calls + results accumulate; ~1.5k/iter is conservative. let total = 0; for (let i = 0; i < iters; i++) total += perCall + i * 1500; console.log(` ${String(iters).padStart(2)} iterations → ${total.toLocaleString().padStart(9)} input tokens = $${(total * IN).toFixed(3)}`); } console.log('\n─ Plus sub-agents: each runs its OWN loop with its own prompt+tools ─'); console.log(' (measured separately below)'); process.exit(0);