Skip to Content

Getting Usage Statistics

This guide explains how to retrieve usage statistics for a user using Liona’s API. Usage stats are valuable for monitoring spending limits, displaying usage information in your UI, and proactively notifying users who are approaching their limits.

🚫
Important

This endpoint uses the user’s access key for authentication, not the admin API key. This means users can check their own usage stats without needing administrative privileges.

Understanding Usage Statistics

Liona tracks usage across three time periods:

  1. Hourly - Usage within the current hour
  2. Daily - Usage within the current day (24-hour period)
  3. Monthly - Usage within the current month

For each period, the API returns:

  • The limit set in the user’s policy
  • Current usage amount
  • Usage as a percentage of the limit

API Endpoint

FieldValue
URLhttps://liona.ai/v1/client/user-stats
MethodGET
AuthenticationUser Access Key (in Authorization header)

Request Parameters

This endpoint doesn’t require any parameters in the request body.

Response Structure

The API returns a JSON object containing:

Note

All decimal values are returned as strings in the JSON response to preserve precision. You’ll need to parse these strings into decimal/float values in your application.

FieldTypeDescription
hourly_limitdecimal/stringThe hourly spending limit defined in the user’s policy
hourly_limit_useddecimal/stringCurrent usage amount for the hour
hourly_limit_used_pctdecimal/stringPercentage of hourly limit used (0-100)
daily_limitdecimal/stringThe daily spending limit defined in the user’s policy
daily_limit_useddecimal/stringCurrent usage amount for the day
daily_limit_used_pctdecimal/stringPercentage of daily limit used (0-100)
monthly_limitdecimal/stringThe monthly spending limit defined in the user’s policy
monthly_limit_useddecimal/stringCurrent usage amount for the month
monthly_limit_used_pctdecimal/stringPercentage of monthly limit used (0-100)

Examples

curl -X GET https://liona.ai/v1/client/user-stats \ -H "Content-Type: application/json" \ -H "Authorization: Bearer USER_ACCESS_KEY"

How It Works

This endpoint performs the following operations:

  1. Validates the provided user access key
  2. Retrieves the user’s policy and its limits
  3. Calculates the current usage for hourly, daily, and monthly periods
  4. Computes usage percentages for each time period
  5. Returns the compiled statistics

Common Use Cases

Proactively Warning Users About Usage

Alert users when they approach their spending limits:

async function checkUsageLimits() { const stats = await getUserStats(); if (stats) { // Parse string to float const monthlyUsagePct = parseFloat(stats.monthly_limit_used_pct); // Warn at 80% usage if (monthlyUsagePct > 80) { showWarningBanner(`You've used ${monthlyUsagePct.toFixed(1)}% of your monthly limit.`); } // Critical warning at 95% usage if (monthlyUsagePct > 95) { showCriticalAlert("You're about to reach your monthly limit!"); } } }

Implementing Auto-Disable Features

Prevent further API calls when a user is close to their limit:

async function shouldAllowAiRequest() { const stats = await getUserStats(); if (stats) { // Parse string to float const monthlyUsagePct = parseFloat(stats.monthly_limit_used_pct); if (monthlyUsagePct > 98) { // Disable AI features and show upgrade prompt showUpgradeModal("You've reached your monthly limit. Upgrade to continue using AI features."); return false; } } return true; }

Error Handling

The API may return the following errors:

Status CodeDescription
401Unauthorized - Invalid or missing access key
500Internal Server Error - Error retrieving usage statistics

Best Practices

  • Caching: For high-traffic applications, consider caching usage statistics for a short period (1-5 minutes) to avoid excessive API calls
  • Progressive Notifications: Implement tiered notifications at different thresholds (e.g., 50%, 75%, 90%, 95%)
  • Graceful Degradation: When users approach limits, consider offering reduced functionality rather than completely disabling features
  • Security: Keep user access keys secure, even though they have limited privileges compared to admin API keys

Next Steps

After implementing usage statistics in your application, you might want to:

Last updated on