1.0.1 • Published 5 months ago

billwise-interceptor v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Billwise Interceptor

Billwise Interceptor is a TypeScript library that provides seamless tracking of OpenAI API usage across both HTTP and WebSocket connections. It automatically intercepts OpenAI API calls and tracks token usage metrics.

Features

  • 🔄 Automatic interception of OpenAI API calls
  • 📊 Token usage tracking for both HTTP and WebSocket connections
  • 🌐 Browser and Node.js support
  • 🔐 Secure API key authentication
  • 🚀 Easy integration with existing applications
  • 📡 Real-time usage monitoring

Installation

npm install billwise-interceptor

Quick Start

import BillwiseInterceptor from 'billwise-interceptor';

// Initialize the interceptor
const interceptor = new BillwiseInterceptor({
  apiKey: 'your-billwise-api-key',
  baseUrl: 'https://your-custom-base-url' // Optional
});

// Start intercepting
interceptor.start();

// Your OpenAI API calls will now be automatically tracked

// Stop intercepting when done
interceptor.stop();

Configuration

The BillwiseInterceptor accepts the following configuration options:

interface BillwiseInterceptorOptions {
  apiKey: string;      // Your Billwise API key
  baseUrl?: string;    // Optional custom base URL (defaults to Billwise API endpoint)
}

Usage Tracking

The interceptor automatically tracks:

  • Prompt tokens
  • Completion tokens
  • Total tokens
  • Model information

This data is automatically sent to your Billwise dashboard for analysis and monitoring.

Browser vs Node.js

The interceptor automatically detects the environment and adjusts its behavior:

  • In browsers: Both HTTP and WebSocket connections are intercepted
  • In Node.js: HTTP connections are intercepted

Error Handling

The interceptor includes comprehensive error handling and logging:

  • Failed tracking attempts are logged to the console
  • Network errors are caught and reported
  • Invalid response formats are handled gracefully

Development

To contribute to the project:

  1. Clone the repository
  2. Install dependencies: npm install
  3. Make your changes
  4. Submit a pull request

License

MIT

Support

For support, please contact the Billwise team or open an issue in the repository.

1.0.1

5 months ago

1.0.0

5 months ago