@microfox/usage-tracker
Version:
A TypeScript SDK for Tracking Usage of any LLM and TypeScript AI SDK internally.
147 lines (106 loc) • 2.89 kB
Markdown
A powerful usage tracking system for Microfox AI that enables tracking of LLM and API usage across your applications.
- Track LLM usage (tokens, models)
- Track API usage (request counts, data)
- Daily, monthly, and yearly usage reports
- Redis-based storage with Upstash
- TypeScript support with full type safety
- Environment variable configuration
## Installation
```bash
npm install @microfox/usage-tracker
```
## Configuration
The usage tracker requires Redis configuration. You can provide this either through environment variables or constructor options:
### Environment Variables
```env
MICROFOX_REDIS_URL_TRACKER=your_redis_url
MICROFOX_REDIS_TOKEN_TRACKER=your_redis_token
MICROFOX_CLIENT_REQUEST_ID=your_client_id
```
```typescript
interface UsageTrackerConstructorOptions {
redisOptions?: {
url: string;
token: string;
};
prefix?: string;
}
```
Track language model usage including:
- Model identifier
- Prompt tokens
- Completion tokens
- Optional markup percentage
```typescript
interface LLMUsage {
type: 'llm';
model?: string;
promptTokens: number;
completionTokens: number;
}
```
Track API usage including:
- Request key
- Request count
- Optional request data size
- Optional markup percentage
```typescript
interface API1Usage {
type: 'api_1';
requestKey?: string;
requestCount: number;
requestData?: number;
}
```
```typescript
import { createMicrofoxUsageTracker } from '@microfox/usage-tracker';
const tracker = createMicrofoxUsageTracker({
redisOptions: {
url: 'your_redis_url',
token: 'your_redis_token',
},
});
```
```typescript
await tracker.trackLLMUsage('my-package', 'gpt-4', {
promptTokens: 100,
completionTokens: 50,
});
```
```typescript
await tracker.trackApi1Usage('my-package', 'api-key', {
requestCount: 1,
requestData: 1024,
});
```
```typescript
// Get today's usage
const dailyUsage = await tracker.getDailyUsage();
// Get monthly usage
const monthlyUsage = await tracker.getMonthlyUsage();
// Get yearly usage
const yearlyUsage = await tracker.getYearlyUsage();
// Get usage for specific package
const packageUsage = await tracker.getDailyUsage('my-package');
```
For each package you want to track, you can set these environment variables:
```env
PACKAGE_NAME_SECRET_TRACKING=markup
PACKAGE_NAME_SECRET_MARKUP_PERCENT=10
```
Replace `PACKAGE_NAME` with your actual package name (converted to uppercase with hyphens/spaces replaced by underscores).
## License
MIT
**See the [github repo](https://github.com/microfox-ai/microfox) or [docs](https://microfox.so) for more info.**