<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/build-a-cost-aware-llm-router-in-node-js-opus-5-5-gpt-6-sol--9qeu178yu" -->

---
title: Build a Cost-Aware LLM Router in Node.js (Opus 5.5 &...
description: A step-by-step guide builds a TypeScript ModelRouter for Node.js 22.6+ that dynamically chooses between two hypothetical LLM tiers (GPT-6 Sol and Claude Opus...
canonical: https://daily.dev/posts/build-a-cost-aware-llm-router-in-node-js-opus-5-5-gpt-6-sol--9qeu178yu
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Build a Cost-Aware LLM Router in Node.js (Opus 5.5 & GPT-6 Sol) | daily.dev
og:description: A step-by-step guide builds a TypeScript ModelRouter for Node.js 22.6+ that dynamically chooses between two hypothetical LLM tiers (GPT-6 Sol and Claude Opus...
og:url: https://daily.dev/posts/build-a-cost-aware-llm-router-in-node-js-opus-5-5-gpt-6-sol--9qeu178yu
og:image: https://api.daily.dev/og/posts/9QeU178YU.png
og:image:alt: Build a Cost-Aware LLM Router in Node.js (Opus 5.5 & GPT-6 Sol)
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Build a Cost-Aware LLM Router in Node.js (Opus 5.5 & GPT-6 Sol)

**[SitePoint](https://daily.dev/sources/sitepoint)** · 23 min read · 0 upvotes · 0 comments

## Summary

A step-by-step guide builds a TypeScript ModelRouter for Node.js 22.6+ that dynamically chooses between two hypothetical LLM tiers (GPT-6 Sol and Claude Opus 5.5) based on cost, complexity, and latency health. It covers pre-inference token-cost estimation with tiktoken, a heuristic 0-100 complexity scorer, a streaming-aware circuit breaker tracking CLOSED/HALF-OPEN/OPEN states, automatic tier fallback logic, and structured JSON telemetry via Pino. Full code is provided for config, environment validation, estimator, scorer, breaker, router, telemetry, and a demo script, plus production hardening notes on persistent budget state, rate-limit coordination, and adding more model tiers.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.sitepoint.com/cost-aware-llm-router-nodejs-claude-opus-5-5-gpt-6-sol>

## Questions this post answers

### How do you build a circuit breaker for streaming LLM responses that can stall mid-stream?

Track three states—CLOSED, HALF-OPEN, and OPEN—based on time-to-first-token and inter-token gap thresholds, not just overall request success. Use an AbortController paired with a resetTimer function that consuming code calls on every token; if the gap between tokens exceeds a configured threshold (e.g. 3000ms), the controller aborts the stream and the breaker records a failure, since a stream that starts successfully but stalls mid-response looks fine to a conventional breaker.

_daily.dev surfaces patterns like this for engineers hardening LLM streaming pipelines against silent stalls._

### How can I estimate the cost of an LLM API call before making it?

Count input tokens ahead of time using tiktoken's encoding for OpenAI-style models, or approximate Anthropic token counts by dividing character count by roughly 3.5 for English text, then multiply by a configurable per-task output multiplier (0.3x for summarization, 1.5x for code generation) to project output tokens before the call completes. Multiply projected input and output tokens by each model's per-1k-token pricing to get an estimated cost.

_Track token-cost estimation techniques on daily.dev as you build budget-aware AI features._

### How do you decide which LLM tier to route a request to based on complexity?

Score the prompt from 0 to 100 using five weighted heuristics: prompt length as a proxy (0-20 points), multi-step instruction markers like 'first' and 'finally' (0-25 points), code-generation keywords like 'implement' or 'function' (0-20 points), structured output requirements like JSON or table mentions (0-15 points), and conversation turn depth (0-20 points). A score of 60 or above routes to the higher-capability tier; below 60 routes to the cheaper tier.

_daily.dev helps developers compare routing heuristics when balancing LLM cost against task complexity._

## Similar posts on daily.dev

- [LLM Context Cost Modeling & Tiered Pricing Over 200k Tokens](https://daily.dev/posts/llm-context-cost-modeling-tiered-pricing-over-200k-tokens-vemedtilo) · SitePoint · 0 upvotes · 0 comments
- [LLM Router Pattern \(2026\): Model Routing, Fallbacks, Cost Control](https://daily.dev/posts/llm-router-pattern-2026-model-routing-fallbacks-cost-control-8txalf5nf) · Alex CloudStar · 1 upvotes · 0 comments
- [Online Learning for Cost-Efficient LLM Routing](https://daily.dev/posts/online-learning-for-cost-efficient-llm-routing-vpgfwfyjn) · Ramp Engineering · 0 upvotes · 0 comments

---

Tags: [#nodejs](https://daily.dev/tags/nodejs), [#typescript](https://daily.dev/tags/typescript), [#finops](https://daily.dev/tags/finops), [#ai-gateway](https://daily.dev/tags/ai-gateway)

[View this post on daily.dev](https://daily.dev/posts/build-a-cost-aware-llm-router-in-node-js-opus-5-5-gpt-6-sol--9qeu178yu)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Build a Cost-Aware LLM Router in Node.js (Opus 5.5 \u0026 GPT-6 Sol)","url":"https://daily.dev/posts/build-a-cost-aware-llm-router-in-node-js-opus-5-5-gpt-6-sol--9qeu178yu","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/build-a-cost-aware-llm-router-in-node-js-opus-5-5-gpt-6-sol--9qeu178yu"},"datePublished":"2026-09-23T18:42:58.183Z","dateModified":"2026-09-28T08:49:21.884Z","description":"A step-by-step guide builds a TypeScript ModelRouter for Node.js 22.6+ that dynamically chooses between two hypothetical LLM tiers (GPT-6 Sol and Claude Opus...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/4c5cb1f6f8cd18c7e2da3669e2b3e69f?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/4c5cb1f6f8cd18c7e2da3669e2b3e69f?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"SitePoint","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"SitePoint","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/sitepoint","url":"https://daily.dev/sources/sitepoint"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/build-a-cost-aware-llm-router-in-node-js-opus-5-5-gpt-6-sol--9qeu178yu","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"nodejs,typescript,finops,ai-gateway","timeRequired":"PT23M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"SitePoint","item":"https://daily.dev/sources/sitepoint"},{"@type":"ListItem","position":3,"name":"Build a Cost-Aware LLM Router in Node.js (Opus 5.5 \u0026 GPT-6 Sol)"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/build-a-cost-aware-llm-router-in-node-js-opus-5-5-gpt-6-sol--9qeu178yu#faq","mainEntity":[{"@type":"Question","name":"How do you build a circuit breaker for streaming LLM responses that can stall mid-stream?","acceptedAnswer":{"@type":"Answer","text":"Track three states—CLOSED, HALF-OPEN, and OPEN—based on time-to-first-token and inter-token gap thresholds, not just overall request success. Use an AbortController paired with a resetTimer function that consuming code calls on every token; if the gap between tokens exceeds a configured threshold (e.g. 3000ms), the controller aborts the stream and the breaker records a failure, since a stream that starts successfully but stalls mid-response looks fine to a conventional breaker. daily.dev surfaces patterns like this for engineers hardening LLM streaming pipelines against silent stalls."}},{"@type":"Question","name":"How can I estimate the cost of an LLM API call before making it?","acceptedAnswer":{"@type":"Answer","text":"Count input tokens ahead of time using tiktoken's encoding for OpenAI-style models, or approximate Anthropic token counts by dividing character count by roughly 3.5 for English text, then multiply by a configurable per-task output multiplier (0.3x for summarization, 1.5x for code generation) to project output tokens before the call completes. Multiply projected input and output tokens by each model's per-1k-token pricing to get an estimated cost. Track token-cost estimation techniques on daily.dev as you build budget-aware AI features."}},{"@type":"Question","name":"How do you decide which LLM tier to route a request to based on complexity?","acceptedAnswer":{"@type":"Answer","text":"Score the prompt from 0 to 100 using five weighted heuristics: prompt length as a proxy (0-20 points), multi-step instruction markers like 'first' and 'finally' (0-25 points), code-generation keywords like 'implement' or 'function' (0-20 points), structured output requirements like JSON or table mentions (0-15 points), and conversation turn depth (0-20 points). A score of 60 or above routes to the higher-capability tier; below 60 routes to the cheaper tier. daily.dev helps developers compare routing heuristics when balancing LLM cost against task complexity."}}]}
```

