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 🙏

© 2026 – Pkg Stats / Ryan Hefner

tokvigil

v0.1.0

Published

TypeScript SDK for TokVigil - AI usage control platform

Readme

TokVigil Node.js SDK

Official TypeScript/JavaScript SDK for TokVigil - AI usage control platform.

Manage rate limits, budgets, and policies for your AI-powered applications.

Installation

npm install tokvigil
# or
yarn add tokvigil
# or
pnpm add tokvigil

Quick Start

import { TokVigil } from "tokvigil";

// Initialize client
const tv = new TokVigil({ apiKey: "tv_live_xxx" });

// Check if request is allowed
const result = await tv.evaluate({
  userId: "user_123",
  plan: "free",
  feature: "chat",
  model: "gpt-4o-mini",
  inputTokens: 100,
});

if (result.allowed) {
  // Make your AI call
  const response = await openai.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [{ role: "user", content: "Hello!" }],
  });

  // Log the usage
  await tv.logUsage({
    requestId: "req_123",
    userId: "user_123",
    model: "gpt-4o-mini",
    inputTokens: response.usage.prompt_tokens,
    outputTokens: response.usage.completion_tokens,
    status: "allowed",
  });
} else {
  console.log(`Blocked: ${result.message}`);
}

Features

  • ✅ Check requests against policies
  • ✅ Log AI usage (tokens, cost, latency)
  • ✅ Get usage analytics
  • ✅ Automatic retry with backoff
  • ✅ Full TypeScript support

Usage

Evaluate Request

const result = await tv.evaluate({
  userId: "user_123",
  model: "gpt-4o-mini",
  plan: "free", // optional
  feature: "chat", // optional
  inputTokens: 100, // optional
  inputText: "Hello", // optional (estimates tokens)
});

console.log(result.allowed); // true or false
console.log(result.reasonCode); // "ALLOWED" or "DAILY_REQUEST_LIMIT_EXCEEDED"
console.log(result.message); // Human readable message
console.log(result.estimatedCostUsd); // 0.0001
console.log(result.limitState?.requestsToday); // 45
console.log(result.limitState?.requestsLimitDaily); // 50

Log Usage

await tv.logUsage({
  requestId: "req_123",
  userId: "user_123",
  model: "gpt-4o-mini",
  inputTokens: 100,
  outputTokens: 50,
  status: "allowed",
  plan: "free",
  feature: "chat",
  latencyMs: 350,
});

Check and Call (Helper)

Automatically evaluate, call AI, and log usage:

const { result, response } = await tv.checkAndCall(
  {
    userId: "user_123",
    model: "gpt-4o-mini",
    plan: "free",
    feature: "chat",
  },
  async () => {
    return await openai.chat.completions.create({
      model: "gpt-4o-mini",
      messages: [{ role: "user", content: "Hello" }],
    });
  },
  (response) => ({
    inputTokens: response.usage.prompt_tokens,
    outputTokens: response.usage.completion_tokens,
  }),
);

if (result.allowed && response) {
  console.log(response.choices[0].message.content);
} else {
  console.log(`Blocked: ${result.message}`);
}

Get Usage Analytics

// Summary
const summary = await tv.getUsageSummary();
console.log(summary.totalRequests);
console.log(summary.totalTokens);
console.log(summary.totalCostUsd);

// Recent usage
const recent = await tv.getRecentUsage({ page: 1, pageSize: 20 });
for (const record of recent.items) {
  console.log(`${record.userId}: ${record.totalTokens} tokens`);
}

// Usage by user
const byUser = await tv.getUsageByUser();
for (const group of byUser.items) {
  console.log(`${group.group}: ${group.requests} requests, $${group.costUsd}`);
}

// Usage by feature
const byFeature = await tv.getUsageByFeature();
for (const group of byFeature.items) {
  console.log(`${group.group}: ${group.requests} requests`);
}

Error Handling

import { TokVigil, RateLimitError, AuthenticationError } from "tokvigil";

const tv = new TokVigil({ apiKey: "tv_live_xxx" });

try {
  const result = await tv.evaluate({
    userId: "user_123",
    model: "gpt-4o-mini",
  });
} catch (error) {
  if (error instanceof AuthenticationError) {
    console.log(`Invalid API key: ${error.message}`);
  } else if (error instanceof RateLimitError) {
    console.log(`Rate limited. Retry after ${error.retryAfter} seconds`);
  } else if (error instanceof TokVigilError) {
    console.log(`Error: ${error.message}`);
  }
}

Configuration

const tv = new TokVigil({
  apiKey: "tv_live_xxx",
  baseUrl: "https://api.tokvigil.com", // Custom API URL
  timeout: 30000, // Request timeout in milliseconds
  retryCount: 3, // Number of retries
  retryDelay: 1000, // Delay between retries in milliseconds
});

License

MIT