gram-tenant
v0.1.3
Published
Client package for gram-tenants: LLM billing and multitenancy
Maintainers
Readme
gram-tenant
Track LLM costs per customer. Budget enforcement, usage dashboards, billing exports.
import OpenAI from 'openai';
import { createTenantClient } from 'gram-tenant';
const client = createTenantClient(new OpenAI(), {
apiKey: process.env.GRAM_API_KEY,
tenantId: 'my-app',
userId: currentUser.id,
});
// Use like normal — costs are tracked automatically
await client.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Hello' }]
});Get Started
- Sign up (coming soon)
- Get your API key from the dashboard
- Install the SDK:
npm install gram-tenantFeatures
- Budget enforcement — Block requests when users exceed limits
- Usage tracking — Every request logged with cost, tokens, model
- Per-user dashboards — See who's spending what
Configuration
const client = createTenantClient(openai, {
apiKey: process.env.GRAM_API_KEY, // Required
tenantId: 'my-app', // Required — your app identifier
userId: currentUser.id, // Required — the end user
// Optional callbacks
onWarning: (budget) => console.log('Approaching limit:', budget),
onBlocked: (budget) => console.log('Budget exceeded:', budget),
onError: (err) => console.error('API error:', err),
});Error Handling
import { GramBudgetExceededError } from 'gram-tenant';
try {
await client.chat.completions.create({...});
} catch (error) {
if (error instanceof GramBudgetExceededError) {
// User exceeded their budget
console.log(error.budget); // { limit, spent, remaining, percent }
}
}How It Works
gram-tenant wraps your AI client:
- Before request — Checks user's budget (cached, fail-open)
- After request — Logs usage to your dashboard (async, non-blocking)
Built on gram-middleware for cost estimation across 69+ models.
License
MIT
