llm-cost-monitor-sdk
v1.1.0
Published
TypeScript SDK for LLM Cost Monitor - Track, aggregate, and analyze LLM usage costs
Maintainers
Readme
LLM Cost Monitor - TypeScript SDK
Official TypeScript/JavaScript SDK for LLM Cost Monitor.
Installation
npm install llm-cost-monitor-sdk
# or
pnpm add llm-cost-monitor-sdk
# or
bun add llm-cost-monitor-sdkQuick Start
import { Configuration, EventsApi } from "llm-cost-monitor-sdk";
const config = new Configuration({
basePath: "http://localhost:8080",
headers: {
Authorization: "Bearer YOUR_API_KEY",
},
});
const events = new EventsApi(config);
// Track an LLM usage event
const response = await events.createEvent({
createEventRequest: {
traceId: "unique-request-id",
provider: "openai",
model: "gpt-4o",
inputTokens: 1200,
outputTokens: 350,
feature: "chat",
userId: "user_123",
},
});
console.log(`Cost: $${response.costUsd}`);Authentication
All API requests require an API key passed in the Authorization header:
const config = new Configuration({
basePath: process.env.LLM_COST_MONITOR_URL,
headers: {
Authorization: `Bearer ${process.env.LLM_COST_MONITOR_API_KEY}`,
},
});API Reference
Events API
import { EventsApi } from "llm-cost-monitor-sdk";
const events = new EventsApi(config);
// Create event
const result = await events.createEvent({
createEventRequest: {
traceId: "req_abc123",
provider: "openai",
model: "gpt-4o",
inputTokens: 1200,
outputTokens: 350,
},
});
// List events
const eventList = await events.listEvents({
limit: 50,
from: new Date("2026-01-01"),
});Metrics API
import { MetricsApi } from "llm-cost-monitor-sdk";
const metrics = new MetricsApi(config);
// Get aggregated metrics
const data = await metrics.getMetrics({
period: "hour",
from: new Date("2026-01-01"),
});
// Get usage summary
const usage = await metrics.getUsage({
period: "month",
});Alerts API
import { AlertsApi } from "llm-cost-monitor-sdk";
const alerts = new AlertsApi(config);
// Create alert
await alerts.createAlert({
createAlertRequest: {
threshold: 100.0,
windowInterval: "24h",
},
});
// List alerts
const alertList = await alerts.listAlerts();
// Delete alert
await alerts.deleteAlert({ id: "alert-uuid" });Pricing API
import { PricingApi } from "llm-cost-monitor-sdk";
const pricing = new PricingApi(config);
// List all pricing
const prices = await pricing.listPricing({});
// Filter by provider
const openaiPrices = await pricing.listPricing({
provider: "openai",
});Error Handling
import { ResponseError } from "llm-cost-monitor-sdk";
try {
await events.createEvent({ ... });
} catch (error) {
if (error instanceof ResponseError) {
const body = await error.response.json();
console.error(`Error ${body.error.code}: ${body.error.message}`);
// Handle specific errors
if (error.response.status === 402) {
console.error("Budget limit exceeded!");
}
}
}Environment Variables
LLM_COST_MONITOR_URL=http://localhost:8080
LLM_COST_MONITOR_API_KEY=your-api-keyLicense
MIT
