AI News AI资讯 5h ago Updated 2h ago 更新于 2小时前 41

Show HN: Slash-tokens – know LLM cost before the call leaves your machine Show HN:Slash-tokens——在请求发出前预知 LLM 成本

slash-tokens is a lightweight (~4.8 KB WASM) TypeScript SDK that estimates LLM token counts and costs pre-call, intercepting fetch() to major providers (Anthropic, OpenAI, xAI, Google) It provides two key functions: `preflight()` for cross-provider cost analysis and `preflightRoute()` for same-provider cheaper alternatives, enabling go/no-go decisions before API calls leave the machine The token estimation engine is Zig-compiled WASM calibrated against real provider tokenizers, avoiding inaccura slash-tokens 是一个轻量级(4.8KB WASM)的LLM调用前token预估与成本优化工具,支持Anthropic、OpenAI、xAI、Google等主流提供商 核心功能包括:亚毫秒级token估算、跨模型成本对比、同提供商路由降级建议,以及可选的托管仪表板($39/月) 采用Zig编译的WASM引擎,基于真实provider tokenizer校准,非简单的chars/4估算,支持CJK、emoji、二进制等边界情况 提供`preflight`(全提供商分析)和`preflightRoute`(同提供商路由决策)两套API,帮助开发者在调用前做出go/no-go决策 开源协议

55
Hot 热度
65
Quality 质量
58
Impact 影响力

Analysis 深度分析

TL;DR

  • slash-tokens is a lightweight (~4.8 KB WASM) TypeScript SDK that estimates LLM token counts and costs pre-call, intercepting fetch() to major providers (Anthropic, OpenAI, xAI, Google)
  • It provides two key functions: preflight() for cross-provider cost analysis and preflightRoute() for same-provider cheaper alternatives, enabling go/no-go decisions before API calls leave the machine
  • The token estimation engine is Zig-compiled WASM calibrated against real provider tokenizers, avoiding inaccurate flat chars/4 heuristics, with sub-millisecond, non-blocking performance
  • The project maintains a live pricing ladder (as of 2026-08-25) covering models like claude-opus-5 ($5/M input), grok-4.6 ($2/M), gemini-3.5-flash-lite ($0.30/M), and gpt-5.6-luna ($0.20/M)
  • A hosted dashboard is available at $39/mo for tracking savings analytics, with MIT-licensed SDK supporting Node.js, Bun, Deno, Cloudflare Workers, Vercel Edge, and browsers

Why It Matters

As LLM context windows expand and usage scales, token costs compound despite per-token price declines—this tool addresses the growing need for cost visibility and optimization in production AI systems. For AI practitioners building agent workflows, RAG pipelines, or high-volume LLM integrations, pre-call token estimation prevents budget overruns and enables intelligent model routing decisions.

Technical Details

  • Core Engine: 4.8 KB Zig-compiled WebAssembly module calibrated against real provider tokenizers (not heuristic approximations), supporting both string and byte-level token counting via slash() and slashBytes()
  • API Design: preflight(prompt, model) returns estimated tokens, cost, context fit, and cross-provider cheaper alternatives; preflightRoute(prompt, model) returns the cheapest same-provider fallback or null
  • Interception Mechanism: Hooks into fetch() calls targeting Anthropic, OpenAI, xAI, and Google endpoints to estimate tokens before requests leave the client machine—sub-millisecond and non-blocking
  • Provider Coverage: Live pricing ladder includes Claude (opus-5, sonnet-5, haiku-4.5), Grok (4.6, 4.3), Gemini (3.1-pro, 3.5-flash-lite), and GPT (5.6-sol, 5.6-terra, 5.6-luna) models with context windows from 200K to 1.05M tokens
  • Testing & Reliability: TypeScript SDK tests via Bun; Zig coverage includes adversarial cases (CJK characters, emoji, binary data, base64, edge thresholds)

Industry Insight

  • The "cheaper tokens haven't shrunk the bill—usage has" insight reflects a broader industry trend: as model pricing drops, developers compensate by using more tokens, making cost-aware engineering practices essential rather than optional
  • Pre-call token estimation and automatic routing to cheaper alternatives represents a practical pattern for production LLM systems—teams should integrate cost visibility into their CI/CD and monitoring pipelines, not treat it as an afterthought
  • The $39/mo SaaS dashboard model (flat fee, no revenue share) suggests a sustainable monetization path for developer tooling in the AI infrastructure space, distinct from the percentage-based models common in AI cost optimization platforms

TL;DR

  • slash-tokens 是一个轻量级(4.8KB WASM)的LLM调用前token预估与成本优化工具,支持Anthropic、OpenAI、xAI、Google等主流提供商
  • 核心功能包括:亚毫秒级token估算、跨模型成本对比、同提供商路由降级建议,以及可选的托管仪表板($39/月)
  • 采用Zig编译的WASM引擎,基于真实provider tokenizer校准,非简单的chars/4估算,支持CJK、emoji、二进制等边界情况
  • 提供preflight(全提供商分析)和preflightRoute(同提供商路由决策)两套API,帮助开发者在调用前做出go/no-go决策
  • 开源协议为MIT,支持Node.js、Bun、Deno、Cloudflare Workers、Vercel Edge及浏览器环境

为什么值得看

slash-tokens解决了LLM应用开发中"调用前不知成本"的核心痛点,通过本地预检机制帮助开发者控制token消耗,避免意外账单。其轻量级架构和跨环境支持使其成为构建生产级LLM应用时实用的成本管控工具。

技术解析

  • WASM核心引擎:4.8KB的Zig编译WASM模块,针对Anthropic、OpenAI、xAI、Google等provider的真实tokenizer进行校准,支持文本、字节流(slashBytes)等多种输入方式,避免TextEncoder开销
  • 双API设计preflight返回预估token数、成本、上下文窗口适配性及跨提供商 cheaper options;preflightRoute仅返回同提供商的最便宜替代模型,两者职责分离避免误用
  • 实时价格表:内置2026年8月25日的模型定价数据,涵盖claude-opus-5($5/M输入)、gpt-5.6-luna($0.20/M输入)等不同价位模型,支持通用别名(如claude-opus、gpt-5.4)
  • 可选遥测服务:通过init+reportAPI上报token节省数据,支持'skipped'/'reduced'/'routed'三种动作类型,托管仪表板采用固定订阅制而非抽成模式
  • 测试覆盖:TypeScript SDK测试+bun test,WASM层覆盖CJK、emoji、二进制、base64、阈值等对抗性用例

行业启示

  • 成本透明化趋势:随着LLM调用量激增,"便宜token但用量膨胀"成为新痛点,开发者需要工具在调用前获得成本可见性,而非依赖事后账单
  • 边缘计算友好:4.8KB WASM+亚毫秒响应的架构设计,契合Edge/Serverless场景,使token预估可在Cloudflare Workers、Vercel Edge等环境无感集成
  • 开源工具商业化路径:核心SDK MIT开源+可选托管仪表板($39/月)的混合模式,既保持生态采用率,又通过SaaS服务变现,为开发者工具提供可参考的商业模式

Disclaimer: The above content is generated by AI and is for reference only. 免责声明:以上内容由 AI 生成,仅供参考。

LLM 大模型 Open Source 开源 Inference 推理 Deployment 部署