tokenlens  by xn1cklas

AI application toolkit for context, cost, and budget management

Created 10 months ago
254 stars

Top 99.1% on SourcePulse

GitHubView on GitHub
1 Expert Loves This Project
Project Summary

Summary

TokenLens provides typed model metadata and utilities for AI applications to manage context windows, estimate costs, and optimize token usage. It targets developers building AI-powered applications, offering crucial insights for efficient and cost-effective operation, particularly with large language models.

How It Works

The library offers a canonical model registry with alias resolution and trusted metadata. It provides a strong TypeScript interface for model identification and safe helper functions. TokenLens normalizes usage across different providers and SDKs, enabling consistent context budgeting (remaining tokens, percentage used, next-turn budget) and compaction strategies (when to compact, how many tokens to remove). It also includes cost estimation based on source-linked pricing where available.

Quick Start & Requirements

  • Install: npm i tokenlens (or pnpm add tokenlens, yarn add tokenlens).
  • Prerequisites: Node.js (v18+ recommended for fetchModels without custom fetch), TypeScript.
  • Links:

Highlighted Details

  • Canonical model registry with alias resolution and minimal, trusted metadata.
  • Strong TypeScript surface enabling model ID autocomplete and safe helper functions.
  • Usage normalization across providers and SDKs, including Vercel AI SDK fields.
  • Context budgeting utilities: calculates remaining tokens, percentage used, and next-turn budget.
  • Compaction helpers: advises on when to compact and how many tokens to remove.
  • Cost estimation: provides fast, rough USD costs using source-linked pricing from models.dev.

Maintenance & Community

  • Relies on the models.dev dataset for model information (https://github.com/sst/models.dev).
  • No specific community links (Discord/Slack) or contributor information provided in the README.

Licensing & Compatibility

  • License: MIT.
  • Compatibility: Permissive MIT license allows for commercial use and integration into closed-source applications.

Limitations & Caveats

  • Many helper functions are deprecated in favor of a more focused API (getContext, getTokenCosts, getUsage).
  • Cost outputs are estimates based on models.dev pricing; authoritative numbers require runtime API responses.
  • Advanced context budgeting strategies (provider-default, combined, input-only) offer flexibility but require careful selection.
Health Check
Last Commit

1 week ago

Responsiveness

Inactive

Pull Requests (30d)
1
Issues (30d)
0
Star History
5 stars in the last 30 days

Explore Similar Projects

Starred by Shizhe Diao Shizhe Diao(Author of LMFlow; Research Scientist at NVIDIA), Michael Chiang Michael Chiang(Cofounder of Ollama), and
7 more.

openbench by groq

0%
784
Provider-agnostic LLM evaluation infrastructure
Created 11 months ago
Updated 2 weeks ago
Starred by Chip Huyen Chip Huyen(Author of "AI Engineering", "Designing Machine Learning Systems"), Kent Dodds Kent Dodds(Cofounder of Remix), and
10 more.

agentic by transitive-bullshit

0.0%
18k
AI agent stdlib for LLM-based TypeScript tooling
Created 3 years ago
Updated 5 months ago
Feedback? Help us improve.