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

@tokenlay/rules

v0.1.0

Published

Smart rule engine for AI usage routing and cost control logic

Readme

@tokenlay/rules

A smart rule engine for AI usage routing and cost control logic. This TypeScript library allows you to define declarative rules that match request contexts and determine model routing, rate limiting, and cost enforcement policies.

Features

  • Declarative Rules: Define conditions and actions in a simple, readable format
  • Smart Routing: Route requests to different AI models based on user tier, usage, or custom criteria
  • Cost Control: Enforce usage limits by requests, tokens, cost, or concurrent connections
  • Flexible Conditions: Support for comparison operators, logical operators, and nested conditions
  • Window-based Limits: Track usage across different time windows (minute, hour, day, month, year)
  • Graceful Degradation: Downgrade to cheaper models when limits are exceeded
  • Type Safety: Full TypeScript support with Zod schema validation

Installation

npm install @tokenlay/rules

Quick Start

import { evaluateRules, type Rule, type EvaluationContext } from '@tokenlay/rules';

// Define your rules
const rules: Rule[] = [
  {
    id: 'free-tier',
    if: { tier: 'free' },
    then: {
      model: 'gpt-3.5-turbo',
      limits: [{
        type: 'requests',
        max: 100,
        window: 'day',
        per: 'user'
      }]
    },
    priority: 1
  },
  {
    id: 'pro-tier',
    if: { tier: 'pro' },
    then: {
      model: 'gpt-4',
      limits: [{
        type: 'cost',
        max: 50,
        window: 'month',
        per: 'user'
      }],
      on_limit_exceeded: {
        action: 'downgrade',
        downgrade_to: 'gpt-3.5-turbo'
      }
    },
    priority: 2
  }
];

// Evaluate against context
const context: EvaluationContext = {
  tier: 'pro',
  user: 'user123',
  usage: {
    requests: 50,
    tokens: 10000,
    cost: 45,
    concurrent: 2
  },
  windows: {}
};

const result = evaluateRules(rules, context);
console.log(result);
// { matched: true, rule_id: 'pro-tier', action: 'allow', model: 'gpt-4', ... }

Rule Structure

Conditions (if)

Support various matching patterns:

// Simple equality
{ tier: 'free' }

// Comparison operators
{ 'usage.cost': { gte: 50 } }
{ requests: { lt: 100 } }
{ feature: { in: ['chat', 'completion'] } }

// Logical operators
{
  and: [
    { tier: 'enterprise' },
    { 'usage.cost': { lt: 1000 } }
  ]
}

{
  or: [
    { admin: true },
    { tier: { in: ['pro', 'enterprise'] } }
  ]
}

// Negation
{ not: { blocked: true } }

Comparison Operators

  • gt, gte, lt, lte: Numeric comparisons
  • eq, neq: Equality checks
  • in, nin: Array membership
  • contains, startsWith, endsWith: String operations

Actions (then)

Define what happens when a rule matches:

{
  model: 'gpt-4',                    // Model to use
  model_params: { temperature: 0.7 }, // Model parameters
  action: 'allow',                    // allow | block | warn | queue
  limits: [{                          // Usage limits
    type: 'tokens',
    max: 100000,
    window: 'day',
    per: 'user'
  }],
  on_limit_exceeded: {                // What to do when limit hit
    action: 'downgrade',
    downgrade_to: 'gpt-3.5-turbo',
    error_message: 'Daily token limit exceeded'
  }
}

Limit Types

  • requests: Number of API requests
  • tokens: Token consumption
  • cost: Monetary cost
  • concurrent: Concurrent connections

Window Types

  • minute, hour, day, month, year, total

Advanced Usage

Custom Tracking Keys

Group usage by custom fields:

{
  limits: [{
    type: 'cost',
    max: 100,
    window: 'month',
    per: 'project',
    tracking_key: 'project_id'  // Use context.project_id for grouping
  }]
}

Priority Resolution

Rules are evaluated by priority (higher first), then by order:

const rules: Rule[] = [
  { if: { tier: 'free' }, then: { model: 'gpt-3.5' }, priority: 1 },
  { if: { tier: 'free', vip: true }, then: { model: 'gpt-4' }, priority: 10 }
];
// VIP free users get GPT-4

Window-based Usage Tracking

Track usage across time windows:

const context: EvaluationContext = {
  usage: { requests: 50, tokens: 10000, cost: 5, concurrent: 1 },
  windows: {
    'requests_hour_user_123': {
      current: 45,
      limit: 50,
      resets_at: '2025-07-27T15:00:00Z'
    }
  },
  // ... other fields
};

API Reference

evaluateRules(rules: Rule[], context: EvaluationContext): EvaluationResult

Main evaluation function that processes rules against context.

Types

interface Rule {
  id?: string;
  description?: string;
  priority?: number;
  if: Condition;
  then: Action;
}

interface EvaluationContext {
  usage: {
    requests: number;
    tokens: number;
    cost: number;
    concurrent: number;
  };
  windows: Record<string, WindowData>;
  [key: string]: any; // Custom fields
}

interface EvaluationResult {
  matched: boolean;
  rule_id?: string;
  action: 'allow' | 'block' | 'warn' | 'queue';
  model?: string;
  limit_exceeded?: boolean;
  error_code?: string;
  error_message?: string;
  // ... other fields
}

Development

# Install dependencies
npm install

# Run tests
npm test

# Build
npm run build

# Type check
npm run typecheck

License

MIT