Quota Enforcement

Example of enforcing quotas before processing requests

Check quotas before processing requests to enforce limits.

import { Client } from '@limitry/sdk';

const client = new Client({
  apiKey: process.env.LIMITRY_API_KEY!,
});

async function processRequest(customerId: string) {
  // Check quota first
  const quotaCheck = await client.quotas.check({
    customerId,
    eventType: 'model_call',
  });

  if (!quotaCheck.allowed) {
    throw new Error('Quota exceeded');
  }

  // Process the request
  const result = await callAI();

  // Track usage
  await client.track.trackUsage({
    customerId,
    eventType: 'model_call',
    inputTokens: result.inputTokens,
    outputTokens: result.outputTokens,
    totalTokens: result.totalTokens,
  });

  return result;
}
import asyncio
from limitry import Client

async def process_request(customer_id: str):
    async with Client(api_key=os.getenv("LIMITRY_API_KEY")) as client:
        # Check quota first
        quota_check = await client.quotas.check(
            customer_id=customer_id,
            event_type="model_call",
        )

        if not quota_check.allowed:
            raise Exception("Quota exceeded")

        # Process the request
        result = await call_ai()

        # Track usage
        await client.track.track_usage(
            customer_id=customer_id,
            event_type="model_call",
            input_tokens=result.input_tokens,
            output_tokens=result.output_tokens,
            total_tokens=result.total_tokens,
        )

        return result