🚀. Socket Launch Week Day 2:Introducing Manifest Alerts.Learn more
Sign In

@tokenlens/helpers

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tokenlens/helpers

Helpers for context windows, usage normalization, compaction, and cost estimation.

latest
Source
npmnpm
Version
1.3.1
Version published
Weekly downloads
410K
-11.03%
Maintainers
1
Weekly downloads
 
Created
Source

@tokenlens/helpers

npm version npm downloads License: MIT TypeScript

TokenLens overview

Utility helpers focused on context caps and rough cost estimation. DI‑first: pass providers from fetchModels() (or getModels() for a static set).

Install

  • npm: npm i @tokenlens/helpers
  • pnpm: pnpm add @tokenlens/helpers
  • yarn: yarn add @tokenlens/helpers

Focused Exports

  • Context: getContext({ modelId, providers })
  • Cost: getTokenCosts({ modelId, usage, providers })
  • Combined: getUsage({ modelId, usage, providers, reserveOutput? })
  • Types: ContextData, TokenCosts, UsageData

Deprecated (use focused exports instead)

  • Context: getContextWindow, remainingContext, percentRemaining, fitsContext
  • Usage: normalizeUsage, breakdownTokens, consumedTokens
  • Cost: estimateCost
  • Compaction: shouldCompact, contextHealth, tokensToCompact
  • Conversation: sumUsage, estimateConversationCost, computeContextRot, nextTurnBudget
  • DI: sourceFromModels, sourceFromCatalog, selectStaticModels

Usage

import { fetchModels } from 'tokenlens';
import { getContext, getTokenCosts, getUsage } from '@tokenlens/helpers';

const openai = await fetchModels('openai');
const modelId = 'openai/gpt-4o-mini';
const usage = { prompt_tokens: 1000, completion_tokens: 500 };

const { maxInput, maxOutput, maxTotal } = getContext(modelId, openai);
const {
  inputUSD,
  outputUSD,
  reasoningUSD,
  cacheReadUSD,
  cacheWriteUSD,
  totalUSD,
} = getTokenCosts(modelId, usage, openai);
const summary = getUsage(modelId, usage, openai);

Notes

  • IDs can be provider/model, provider:id, or providerless model.
  • Version dots normalize to dashes in the model segment.
  • Cost outputs are estimates based on models.dev pricing fields. For authoritative cost numbers, read pricing and usage metrics from your model provider's API responses at runtime.

Back‑compat via tokenlens

import { getContext, getTokenCosts, getUsage } from 'tokenlens';
// These wrappers inject a default providers set if not supplied.

License MIT

Keywords

ai

FAQs

Package last updated on 19 Sep 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts