
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
llm-cost-tracker
Advanced tools
Professional NPM package for tracking LLM API costs, managing budgets, and monitoring usage across multiple providers
A professional TypeScript library for tracking and managing costs across multiple LLM providers (OpenAI, Anthropic, Google, etc.). Monitor your AI spending, set budgets, and receive alerts when thresholds are reached.
npm install llm-cost-tracker
import {
CostTracker,
InMemoryStorage,
PriceRegistry,
BudgetManager,
} from "llm-cost-tracker";
// Initialize components
const storage = new InMemoryStorage();
const priceRegistry = new PriceRegistry();
const budgetManager = new BudgetManager(storage, {
limitUSD: 100.0,
thresholdPercent: 80,
});
// Create tracker
const tracker = new CostTracker(storage, priceRegistry, budgetManager);
// Track a cost
const event = await tracker.track({
provider: "openai",
model: "gpt-4",
inputTokens: 1000,
outputTokens: 500,
});
console.log(`Cost: $${event.totalCostUSD}`);
// Get summary
const summary = await tracker.getSummary();
console.log(`Total spent: $${summary.totalCostUSD}`);
console.log(`Total events: ${summary.eventCount}`);
import { BudgetManager } from "llm-cost-tracker";
const budgetManager = new BudgetManager(storage, {
limitUSD: 100.0,
thresholdPercent: 80,
});
// Set up alert callbacks
budgetManager.onThreshold((status) => {
console.log(`⚠️ Warning: ${status.percentUsed}% of budget used!`);
console.log(`Spent: $${status.usedUSD} / $${status.limitUSD}`);
});
budgetManager.onExceeded((status) => {
console.log(`🚨 Budget exceeded! Spent: $${status.usedUSD}`);
// Take action: disable API calls, send notifications, etc.
});
import { MCPServer } from "llm-cost-tracker/server";
const server = new MCPServer({
port: 3000,
host: "localhost",
costTracker: tracker,
budgetManager: budgetManager,
});
await server.start();
console.log("MCP Server running on http://localhost:3000");
Track Cost
POST /cost.track
Content-Type: application/json
{
"provider": "openai",
"model": "gpt-4",
"inputTokens": 1000,
"outputTokens": 500
}
Get Summary
GET /cost.summary
Get Budget Status
GET /budget.status
Set Budget
POST /budget.set
Content-Type: application/json
{
"limitUSD": 100.0,
"thresholdPercent": 80
}
Reset Data
POST /cost.reset
gpt-4 - $0.03/1K input, $0.06/1K outputgpt-4-turbo - $0.01/1K input, $0.03/1K outputgpt-3.5-turbo - $0.0005/1K input, $0.0015/1K outputclaude-3-opus - $0.015/1K input, $0.075/1K outputclaude-3-sonnet - $0.003/1K input, $0.015/1K outputclaude-3-haiku - $0.00025/1K input, $0.00125/1K outputgemini-pro - $0.00025/1K input, $0.0005/1K outputgemini-pro-vision - $0.00025/1K input, $0.0005/1K outputimport { PriceRegistry } from "llm-cost-tracker";
const priceRegistry = new PriceRegistry();
// Add custom pricing for a model
priceRegistry.register("openai", "gpt-4-custom", {
inputPricePerThousand: 0.025,
outputPricePerThousand: 0.05,
});
// Use it
const event = await tracker.track({
provider: "openai",
model: "gpt-4-custom",
inputTokens: 1000,
outputTokens: 500,
});
// Track costs for different users/projects
await tracker.track({
provider: "openai",
model: "gpt-4",
inputTokens: 1000,
outputTokens: 500,
namespace: "user-123",
});
// Get summary for specific namespace
const userSummary = await tracker.getSummary("user-123");
// Calculate cost without saving to storage
const simulatedEvent = tracker.simulateCost({
provider: "openai",
model: "gpt-4",
inputTokens: 1000,
outputTokens: 500,
});
console.log(`Estimated cost: $${simulatedEvent.totalCostUSD}`);
import { StorageProvider, CostEvent } from "llm-cost-tracker";
class DatabaseStorage implements StorageProvider {
async save(event: CostEvent): Promise<void> {
// Save to your database
}
async loadAll(namespace?: string): Promise<CostEvent[]> {
// Load from your database
}
async reset(namespace?: string): Promise<void> {
// Clear your database
}
}
const storage = new DatabaseStorage();
const tracker = new CostTracker(storage, priceRegistry);
track(request: TrackingRequest): Promise<CostEvent>Track a new LLM API call and return the cost event.
Parameters:
provider (string) - Provider name (e.g., 'openai', 'anthropic')model (string) - Model name (e.g., 'gpt-4', 'claude-3-opus')inputTokens (number) - Number of input tokensoutputTokens (number) - Number of output tokensnamespace (string, optional) - Namespace for multi-tenant trackingReturns: CostEvent with calculated costs
simulateCost(request: TrackingRequest): CostEventCalculate cost without persisting to storage.
getSummary(namespace?: string): Promise<CostSummary>Get aggregated cost summary with breakdowns by provider and model.
reset(namespace?: string): Promise<void>Clear all tracking data.
constructor(storage: StorageProvider, config: BudgetConfig)Create a new budget manager.
Config Options:
limitUSD (number) - Maximum spending limit in USDthresholdPercent (number) - Percentage threshold for warnings (0-100)resetInterval (string, optional) - 'daily', 'weekly', 'monthly', or 'manual'namespace (string, optional) - Namespace to trackonThreshold(callback: BudgetCallback): voidSet callback for threshold warnings.
onExceeded(callback: BudgetCallback): voidSet callback for limit exceeded.
checkBudget(): Promise<BudgetStatus>Check current spending against budget and trigger callbacks.
getStatus(): Promise<BudgetStatus>Get current budget status without triggering callbacks.
updateConfig(config: Partial<BudgetConfig>): voidUpdate budget configuration.
reset(): Promise<void>Reset budget usage data.
register(provider: string, model: string, pricing: ModelPricing): voidRegister custom pricing for a model.
getPricing(provider: string, model: string): ModelPricingGet pricing for a specific provider and model.
calculateCost(tokens: number, pricePerThousand: number): numberCalculate cost for a given number of tokens.
interface CostEvent {
id: string;
timestamp: Date;
provider: string;
model: string;
inputTokens: number;
outputTokens: number;
inputCostUSD: number;
outputCostUSD: number;
totalCostUSD: number;
namespace?: string;
}
interface CostSummary {
totalCostUSD: number;
totalInputTokens: number;
totalOutputTokens: number;
eventCount: number;
byProvider: Record<string, ProviderSummary>;
byModel: Record<string, ModelSummary>;
}
interface BudgetStatus {
limitUSD: number;
usedUSD: number;
percentUsed: number;
remainingUSD: number;
thresholdReached: boolean;
limitExceeded: boolean;
resetInterval: string;
nextResetDate?: Date;
}
Check out the examples directory for complete working examples:
# Run all tests
npm test
# Run tests with coverage
npm run test:coverage
# Run tests in watch mode
npm run test:watch
npm run build
Contributions are welcome! Please feel free to submit a Pull Request.
MIT © Dominique Houessou
Initial release with:
FAQs
Professional NPM package for tracking LLM API costs, managing budgets, and monitoring usage across multiple providers
We found that llm-cost-tracker demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.