Cost-Aware Routing & Budgets
medium10 mincost-routing.ts
Simple questions should not go to the most expensive model, and nobody should blow the daily budget.
What it shows
- Analyze a task and get a model recommendation with
CostAwareRouter - Enforce per-run, hourly and daily limits
- Record costs and read the summary
The code
cost-routing.ts
import { CostAwareRouter } from '@cogitator-ai/core';
const router = new CostAwareRouter({
config: {
enabled: true,
preferLocal: true,
trackCosts: true,
budget: {
maxCostPerRun: 0.05,
maxCostPerHour: 1.0,
maxCostPerDay: 10.0,
warningThreshold: 0.8,
onBudgetWarning: (current, limit) => console.log(`budget warning: $${current.toFixed(4)} / $${limit}`),
onBudgetExceeded: (current, limit) => console.log(`budget exceeded: $${current.toFixed(4)} > $${limit}`),
},
},
});
const task = 'Design a distributed consensus algorithm with a formal proof of correctness';
const requirements = router.analyzeTask(task);
console.log(`complexity=${requirements.complexity} reasoning=${requirements.needsReasoning}`);
const recommendation = await router.recommendModel(task);
console.log(`use ${recommendation.modelId} (${recommendation.provider}), est. $${recommendation.estimatedCost.toFixed(6)}`);
console.log('because:', recommendation.reasons.join('; '));
console.log('fallbacks:', recommendation.fallbacks.slice(0, 2).join(', '));
for (const cost of [0.01, 0.06]) {
const check = router.checkBudget(cost);
console.log(`$${cost}: ${check.allowed ? 'allowed' : `blocked — ${check.reason}`}`);
}
router.recordCost({
model: 'gemini-3.8-flash',
agentId: 'summarizer',
inputTokens: 1200,
outputTokens: 400,
cost: 0.002,
runId: 'run-1',
});
router.recordCost({
model: 'claude-sonnet-5-5',
agentId: 'coder',
inputTokens: 3000,
outputTokens: 1500,
cost: 0.025,
runId: 'run-2',
});
const summary = router.getCostSummary();
console.log(`total $${summary.totalCost.toFixed(4)} over ${summary.runCount} runs`, summary.byAgent);
const status = router.getBudgetStatus();
if (status) console.log(`today: $${status.dailyUsed.toFixed(4)} of $${status.dailyLimit ?? '∞'}`);Run it
install · bash
pnpm add @cogitator-ai/corerun · bash
npx tsx cost-routing.tsOr run the full example from a clone of the repository (keys in .env):
repository · bash
npx tsx examples/core/09-cost-routing.ts