Skip to content

API Reference

LLMGuard

MethodReturnsDescription
new LLMGuard(config)LLMGuardCreate a guard instance
wrapOpenAI(client)OpenAIProviderWrap an OpenAI SDK client
wrapAnthropic(client)AnthropicProviderWrap an Anthropic SDK client
wrapGemini(client)GeminiProviderWrap a Google Generative AI client
guard.openaiOpenAIProviderAccess the wrapped OpenAI provider
guard.anthropicAnthropicProviderAccess the wrapped Anthropic provider
guard.geminiGeminiProviderAccess the wrapped Gemini provider
getStats(ctx?)Promise<BudgetStats[]>Get usage stats for all applicable scopes
getRemainingBudget(ctx?)Promise<number>Get minimum remaining tokens across scopes
reset(ctx?)Promise<void>Reset usage counters
getBudgetManager()BudgetManagerAccess the underlying budget manager

Provider .chat() Method

All providers (OpenAI, Anthropic, Gemini) share the same interface:

typescript
await guard.openai.chat(params, context?)
ParameterTypeDescription
params.modelstringModel name (e.g. 'gpt-4o', 'claude-sonnet-4-20250514')
params.messagesChatMessage[]Array of { role, content } messages
params.max_tokensnumberMax output tokens (default: 4096)
context.userIdstring?User identifier for per-user budgets
context.sessionIdstring?Session identifier for per-session budgets
context.routestring?Route/endpoint for per-route budgets

BudgetStats Object

typescript
{
  scope: 'global' | 'user' | 'session' | 'route',
  scopeKey: string,
  used: number,
  limit: number,
  remaining: number,
  percentage: number
}

BudgetExceededError

typescript
err.message   // Human-readable error string
err.stats     // BudgetStats object with full details
err.name      // 'BudgetExceededError'

Exports

typescript
// Core
import { LLMGuard, BudgetManager, BudgetExceededError } from 'llm-spend-guard';

// Providers
import { OpenAIProvider, AnthropicProvider, GeminiProvider } from 'llm-spend-guard';

// Storage
import { MemoryStorage, RedisStorage } from 'llm-spend-guard';

// Middleware
import { expressMiddleware, budgetErrorHandler, withBudgetGuard } from 'llm-spend-guard';

// Utilities
import { estimateTokens, estimateMessagesTokens, truncateMessages } from 'llm-spend-guard';

// Types
import type {
  GuardConfig, BudgetConfig, BudgetStats, BudgetScope,
  AlertLevel, StorageAdapter, ScopeUsage, RequestContext,
  ChatMessage, TokenEstimatorFn,
} from 'llm-spend-guard';

Released under the MIT License.