← Back to docs

SDK Reference

Full API for agensor npm package.

createMeter(config)

Returns a Meter instance. Call once at startup.

PropTypeDescription
apiKeystringRequired. Your secret key (sk_…) from app.agensor.dev.
baseUrlstring?Defaults to https://api.agensor.dev. Override for self-hosted.
syncIntervalMsnumber?Interval for background balance sync. Default: 30 000.

meter.wrapAnthropic(client, options)

Returns a drop-in replacement for the Anthropic SDK client.

PropTypeDescription
getUserId(ctx?) => stringRequired. Returns a stable user identifier for the caller.
runRunHandle?Optional run budget. Throw if total run cost exceeds run.maxCredits.

meter.wrapOpenAI(client, options)

Same interface as wrapAnthropic but for the OpenAI SDK.

PropTypeDescription
getUserId(ctx?) => stringRequired.
runRunHandle?Optional run budget.

meter.startRun(options)

Creates a scoped run budget across multiple LLM calls.

PropTypeDescription
userIdstringRequired.
maxCreditsnumberMaximum 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
}