← Back to docs
SDK Reference
Full API for agensor npm package.
createMeter(config)
Returns a Meter instance. Call once at startup.
| Prop | Type | Description |
|---|---|---|
| apiKey | string | Required. Your secret key (sk_…) from app.agensor.dev. |
| baseUrl | string? | Defaults to https://api.agensor.dev. Override for self-hosted. |
| syncIntervalMs | number? | Interval for background balance sync. Default: 30 000. |
meter.wrapAnthropic(client, options)
Returns a drop-in replacement for the Anthropic SDK client.
| Prop | Type | Description |
|---|---|---|
| getUserId | (ctx?) => string | Required. Returns a stable user identifier for the caller. |
| run | RunHandle? | Optional run budget. Throw if total run cost exceeds run.maxCredits. |
meter.wrapOpenAI(client, options)
Same interface as wrapAnthropic but for the OpenAI SDK.
| Prop | Type | Description |
|---|---|---|
| getUserId | (ctx?) => string | Required. |
| run | RunHandle? | Optional run budget. |
meter.startRun(options)
Creates a scoped run budget across multiple LLM calls.
| Prop | Type | Description |
|---|---|---|
| userId | string | Required. |
| maxCredits | number | Maximum credits the run may consume before throwing BudgetExhaustedError. |
BudgetExhaustedError
Thrown when a user has 0 credits or a run has exceeded its budget. Catch this to return a 402 or a friendly message.
try {
const msg = await client.messages.create(...)
} catch (err) {
if (err instanceof BudgetExhaustedError) {
return res.status(402).json({ error: 'Out of credits' })
}
throw err
}