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

cognigate

v1.0.0

Published

AI gateway with budget controls, local fallback, and voice mode - never overspend on AI again

Readme

Cognigate

A unified AI gateway with budget controls, local fallback, and voice mode.

npm version Tests License: MIT


Why Cognigate?

Never overspend on AI again. Cognigate gives you hard budget limits, automatic fallback to free local models, and seamless provider switching.

Key Features

  • 💰 Budget Protection - Hard daily spending limits, never exceed your budget
  • 🔄 Smart Fallback - Automatically switches to Ollama/LM Studio/WebLLM when budget runs out
  • 🎤 Voice Mode - Built-in speech recognition and text-to-speech for conversational AI
  • 🚀 Multi-Provider - OpenAI, Anthropic, Google - one API for all
  • 📊 Real-Time Monitoring - Track costs, get webhook alerts (Slack/Discord)
  • ⚡ Performance - Semantic caching and compression reduce costs by 40%+
  • 🌐 Cross-Platform - Works in Node.js, browsers, React, and Next.js

Quick Start

Installation

npm install cognigate

Basic Usage

import { createGateway } from 'cognigate';

const ai = createGateway({
  dailyBudget: 10,        // $10/day limit
  cacheEnabled: true,
  cloudProviders: {
    openai: { apiKey: process.env.OPENAI_API_KEY }
  },
  localFallback: { enabled: true }
});

// Simple completion
const answer = await ai.complete("What is TypeScript?");
console.log(answer);

// Check budget
const status = ai.getBudgetStatus();
console.log(`Used: $${status.used.toFixed(2)} / $${status.dailyLimit}`);

Features in Detail

💰 Budget Protection

Set daily spending limits and never worry about unexpected costs:

const ai = createGateway({
  dailyBudget: 5.00,  // Hard limit: $5/day
  cloudProviders: {
    openai: { apiKey: process.env.OPENAI_API_KEY }
  }
});

// Track budget in real-time
const status = ai.getBudgetStatus();
console.log(`Used: $${status.used} / $${status.dailyLimit}`);
console.log(`Remaining: $${status.remaining}`);
console.log(`Resets at: ${status.resetAt}`);

Budget automatically resets at midnight UTC. When exceeded, requests throw BudgetExceededError or fall back to local models.

🔄 Multi-Provider Support

Use multiple AI providers with automatic fallback:

const ai = createGateway({
  dailyBudget: 10,
  cloudProviders: {
    openai: {
      apiKey: process.env.OPENAI_API_KEY,
      models: ['gpt-4o-mini', 'gpt-4o']  // Tries mini first
    },
    anthropic: {
      apiKey: process.env.ANTHROPIC_API_KEY,
      models: ['claude-3-haiku', 'claude-3-sonnet']
    },
    google: {
      apiKey: process.env.GOOGLE_API_KEY,
      models: ['gemini-1.5-flash']
    }
  }
});

// Automatically tries providers in order until one succeeds
const answer = await ai.complete("Hello!");

🏠 Local Fallback

When budget runs out or cloud providers fail, automatically switch to free local models:

const ai = createGateway({
  dailyBudget: 1.00,  // Small budget
  cloudProviders: {
    openai: { apiKey: process.env.OPENAI_API_KEY }
  },
  localFallback: {
    enabled: true,
    providers: ['ollama', 'lmstudio', 'webllm']  // Tries in order
  }
});

// If budget exceeded, automatically uses Ollama (free!)
const answer = await ai.complete("Hello!");

Supported local providers:

  • Ollama - Most popular, GPU-accelerated
  • LM Studio - Great for Mac with Metal acceleration
  • WebLLM - Runs entirely in browser via WebGPU

⚡ Streaming Responses

Get tokens as they're generated for better UX:

for await (const token of ai.stream("Write a poem")) {
  process.stdout.write(token);  // Print each word immediately
}

🎤 Voice Mode

Built-in speech recognition and text-to-speech:

import { createGateway } from 'cognigate';
import { Conversation } from 'cognigate/voice';

const ai = createGateway({ dailyBudget: 5 });
const conversation = new Conversation(ai, {
  continuous: true,
  autoSpeak: true,
  language: 'en-US'
});

// Listen for events
conversation.on('transcript', (text) => {
  console.log(`You said: ${text}`);
});

conversation.on('response', (text) => {
  console.log(`AI said: ${text}`);
});

// Start listening
await conversation.start();

📊 Webhook Alerts

Get notified when budget thresholds are reached:

const ai = createGateway({
  dailyBudget: 10,
  cloudProviders: {
    openai: { apiKey: process.env.OPENAI_API_KEY }
  },
  webhooks: {
    slack: 'https://hooks.slack.com/services/YOUR/WEBHOOK/URL',
    discord: 'https://discord.com/api/webhooks/YOUR/WEBHOOK/URL',
    custom: 'https://your-server.com/webhook'
  }
});

// Automatically sends alerts at 50%, 80%, and 100% budget usage

Alert types:

  • 50% Warning - Yellow alert
  • 80% Urgent - Orange alert
  • 100% Exceeded - Red alert

🗄️ Smart Caching

Reduce costs with intelligent caching:

const ai = createGateway({
  dailyBudget: 10,
  cacheEnabled: true,
  semanticCaching: true,      // Match similar prompts
  similarityThreshold: 0.9,   // How similar (0-1)
  cloudProviders: {
    openai: { apiKey: process.env.OPENAI_API_KEY }
  }
});

// First call - hits API
await ai.complete("What is TypeScript?");  // Cost: $0.0001

// Second call - cached!
await ai.complete("What is TypeScript?");  // Cost: $0 (cached)

// Third call - semantic match!
await ai.complete("Explain TypeScript");   // Cost: $0 (similar prompt)

🗜️ Prompt Compression

Reduce token usage with automatic compression:

const ai = createGateway({
  dailyBudget: 10,
  compressionLevel: 'high',  // 'low' | 'medium' | 'high'
  cloudProviders: {
    openai: { apiKey: process.env.OPENAI_API_KEY }
  }
});

// Automatically compresses prompts before sending
// High compression: ~40% token reduction

Platform Support

Node.js

npm install cognigate
import { createGateway } from 'cognigate';

const ai = createGateway({
  dailyBudget: 10,
  cloudProviders: {
    openai: { apiKey: process.env.OPENAI_API_KEY }
  }
});

Browser (CDN)

<!DOCTYPE html>
<script type="module">
import { createGateway } from 'https://cdn.jsdelivr.net/npm/[email protected]/dist/index.mjs';

const ai = createGateway({ dailyBudget: 0 }); // unlimited
const answer = await ai.complete("Hello!");
console.log(answer);
</script>

React / Next.js

'use client';
import { createGateway } from 'cognigate';
import { useEffect, useState } from 'react';

export default function ChatPage() {
  const [ai] = useState(() => createGateway({
    dailyBudget: 5,
    cloudProviders: {
      openai: { apiKey: process.env.NEXT_PUBLIC_OPENAI_KEY }
    }
  }));

  const [response, setResponse] = useState('');

  const handleSubmit = async (prompt: string) => {
    const answer = await ai.complete(prompt);
    setResponse(answer);
  };

  return (
    <div>
      <button onClick={() => handleSubmit("Hello!")}>
        Ask AI
      </button>
      <p>{response}</p>
    </div>
  );
}

API Reference

createGateway(config)

Creates a new gateway instance.

interface GatewayConfig {
  dailyBudget?: number;              // Daily spending limit in USD (0 = unlimited)
  cacheEnabled?: boolean;             // Enable response caching
  semanticCaching?: boolean;          // Cache similar prompts
  similarityThreshold?: number;       // Similarity threshold (0-1)
  compressionLevel?: 'low' | 'medium' | 'high';
  localFallback?: LocalFallbackConfig;
  cloudProviders?: CloudProvidersConfig;
  webhooks?: WebhooksConfig;
}

ai.complete(prompt, options?)

Send a prompt and get a text response.

const answer = await ai.complete("What is AI?", {
  model: 'gpt-4o',              // Override default model
  temperature: 0.7,              // Creativity (0-2)
  maxTokens: 500,                // Max response length
  forceProvider: 'cloud' | 'local'  // Force provider type
});

ai.stream(prompt, options?)

Stream response tokens in real-time.

for await (const token of ai.stream("Write a story")) {
  console.log(token);  // Each word as it's generated
}

ai.getBudgetStatus()

Get current budget information.

const status = ai.getBudgetStatus();
// Returns: { dailyLimit, used, remaining, resetAt }

ai.clearCache()

Manually clear the response cache.

ai.clearCache();

Examples

See the examples/ directory for complete working examples:


Configuration Guide

Environment Variables

# Cloud providers
export OPENAI_API_KEY="sk-..."
export ANTHROPIC_API_KEY="sk-ant-..."
export GOOGLE_API_KEY="..."

# Webhooks (optional)
export SLACK_WEBHOOK_URL="https://hooks.slack.com/..."
export DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/..."

Cost Optimization

Reduce costs by 70%+ with these settings:

const ai = createGateway({
  dailyBudget: 5,
  cacheEnabled: true,
  semanticCaching: true,
  similarityThreshold: 0.85,    // Aggressive caching
  compressionLevel: 'high',      // Max compression
  cloudProviders: {
    openai: {
      apiKey: process.env.OPENAI_API_KEY,
      models: ['gpt-4o-mini']    // Use cheapest model
    }
  },
  localFallback: {
    enabled: true                 // Free fallback
  }
});

Troubleshooting

"Budget exceeded" error

Budget resets at midnight UTC. Options:

  1. Increase dailyBudget
  2. Enable localFallback for free models
  3. Wait for reset (check status.resetAt)

Caching not working

Enable caching explicitly:

cacheEnabled: true,
semanticCaching: true

Voice features not working

Voice requires:

  • Browser environment (Chrome, Safari, Edge)
  • HTTPS or localhost
  • Microphone permissions granted

Local models not found

Install Ollama or LM Studio:

# macOS
brew install ollama
ollama serve

# Or download LM Studio
# https://lmstudio.ai/

Development

# Install dependencies
npm install

# Run tests
npm test

# Build
npm run build

# Run examples
npx tsx examples/basic-chat.ts

License

MIT License - see LICENSE for details.


Support


Made with ❤️ by developers who hate surprise AI bills.