npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@microfox/usage-pricing

v0.3.0

Published

A TypeScript SDK for Tracking Pricing & Usage of any LLM and TypeScript AI SDK internally.

Downloads

28

Readme

@microfox/usage-pricing

A TypeScript SDK for tracking and managing usage-based pricing in Microfox applications. This SDK provides functionality to track, retrieve, and analyze usage data with built-in pricing calculations.

Installation

npm install @microfox/usage-pricing
# or
yarn add @microfox/usage-pricing

Configuration

The SDK requires Redis configuration for storing and retrieving usage data. You can provide the configuration in two ways:

  1. Through environment variables:
MICROFOX_REDIS_URL_TRACKER=your_redis_url
MICROFOX_REDIS_TOKEN_TRACKER=your_redis_token
MICROFOX_CLIENT_REQUEST_ID=your_client_id
  1. Through constructor options:
import { createMicrofoxUsagePricing } from '@microfox/usage-pricing';

const usagePricing = createMicrofoxUsagePricing({
  redisOptions: {
    url: 'your_redis_url',
    token: 'your_redis_token',
  },
  prefix: 'your_prefix',
});

Usage

Basic Usage

import { createDefaultMicrofoxUsagePricing } from '@microfox/usage-pricing';

const usagePricing = createDefaultMicrofoxUsagePricing();

Using Pricing Functions

The SDK provides several functions to calculate pricing for different types of usage:

import {
  attachPricing,
  attachPricingApi1,
  attachPricingLLM,
} from '@microfox/usage-pricing';

// Calculate pricing for any type of usage
const usageWithPricing = attachPricing({
  type: 'llm',
  model: 'gpt-4',
  promptTokens: 100,
  completionTokens: 50,
  package: 'openai',
});

// Calculate pricing specifically for API usage
const apiUsageWithPricing = attachPricingApi1({
  type: 'api_1',
  package: 'aws-ses',
  requestKey: 'send-email',
  requestCount: 100,
  requestData: 5000,
});

// Calculate pricing specifically for LLM usage
const llmUsageWithPricing = attachPricingLLM({
  type: 'llm',
  model: 'gpt-4',
  promptTokens: 100,
  completionTokens: 50,
  package: 'openai',
});

The pricing functions return an object that includes:

  • All original usage data
  • priceUSD: The final price in USD after applying any markup
  • originalPriceUSD: The base price in USD before markup

Example response:

{
  type: 'llm',
  model: 'gpt-4',
  promptTokens: 100,
  completionTokens: 50,
  package: 'openai',
  priceUSD: 0.012, // Price after markup
  originalPriceUSD: 0.015 // Original price before markup
}

API Reference

Get Usage Data

// Get usage with pagination
const usage = await usagePricing.getUsage(packageName, prefixDateKey, {
  limit: 100,
  offset: 0,
});

// Get daily usage
const dailyUsage = await usagePricing.getDailyUsage(packageName, {
  limit: 100,
  offset: 0,
});

// Get monthly usage
const monthlyUsage = await usagePricing.getMonthlyUsage(packageName, {
  limit: 100,
  offset: 0,
});

// Get yearly usage
const yearlyUsage = await usagePricing.getYearlyUsage(packageName, {
  limit: 100,
  offset: 0,
});

Get Full Usage Data

// Get full usage data
const fullUsage = await usagePricing.getFullUsage(packageName, prefixDateKey);

// Get full daily usage
const fullDailyUsage = await usagePricing.getFullDailyUsage(packageName);

// Get full monthly usage
const fullMonthlyUsage = await usagePricing.getFullMonthlyUsage(packageName);

// Get full yearly usage
const fullYearlyUsage = await usagePricing.getFullYearlyUsage(packageName);

Response Format

The usage data is returned in the following format:

{
  data: Usage[], // Array of usage entries with pricing information
  total: number, // Total number of items
  limit: number, // Number of items per page
  offset: number, // Current offset
  hasMore: boolean // Whether there are more items to fetch
}

Each usage entry includes:

  • Usage metrics
  • Package information
  • Timestamp
  • Pricing information (automatically attached)

Types

UsageTrackerConstructorOptions

interface UsageTrackerConstructorOptions {
  redisOptions?: {
    url?: string;
    token?: string;
  };
  prefix?: string;
}

Usage

The Usage type represents a single usage entry with pricing information attached.

Error Handling

The SDK handles various error cases:

  • Invalid Redis configuration
  • Missing environment variables
  • Invalid usage data format

Best Practices

  1. Always provide proper Redis configuration
  2. Use appropriate pagination parameters to manage large datasets
  3. Handle the response data appropriately in your application
  4. Monitor usage patterns using the different time-based methods

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.