Create a Limit
Limits are linked to meters. First create a meter, then create a limit that references it.Copy
from limitry import Limitry
client = Limitry()
# First, create or get a meter
meter = client.meters.create(
name="Total Tokens",
aggregation="sum",
field="values.tokens",
event_filter={"event_type": "llm.completion"}
)
# Create a limit using that meter
limit = client.limits.create(
name="Daily token limit",
meter_id=meter.id,
limit_value=100000,
period="day",
customer_id="cust_123"
)
print(f"Created limit: {limit.id}")
Limit Options
Periods
Copy
# Hourly (resets every hour on the hour)
limit = client.limits.create(
name="Hourly limit",
meter_id=meter.id,
limit_value=10000,
period="hour",
customer_id="cust_123"
)
# Daily (resets at midnight in customer's timezone)
period="day"
# Weekly (resets Monday midnight in customer's timezone)
period="week"
# Monthly (resets on customer's billing cycle day)
period="month"
# Annual (resets on customer's billing cycle anniversary)
period="annual"
# Lifetime (never resets)
period="all_time"
Dimension Filters
Apply limits to specific segments of events:Copy
# Only limit GPT-4 usage
limit = client.limits.create(
name="GPT-4 daily limit",
meter_id=meter.id,
limit_value=50000,
period="day",
customer_id="cust_123",
dimension_filters={"model": "gpt-4"}
)
# Limit a specific feature
limit = client.limits.create(
name="Premium feature limit",
meter_id=feature_meter.id,
limit_value=100,
period="month",
customer_id="cust_123",
dimension_filters={"feature": "advanced-analysis"}
)
Alert Thresholds
Get notified at specific usage levels:Copy
limit = client.limits.create(
name="Daily limit with alerts",
meter_id=meter.id,
limit_value=100000,
period="day",
customer_id="cust_123",
alert_thresholds=[50, 80, 100] # Alert at 50%, 80%, and 100%
)
Check Limits
Check if a customer is within their limits:Copy
from datetime import datetime
result = client.limits.check(customer_id="cust_123")
print(f"Allowed: {result.allowed}")
for limit in result.limits:
pct = (limit.used / limit.limit) * 100
print(f"{limit.name}: {limit.used:,}/{limit.limit:,} ({pct:.1f}%)")
print(f" Remaining: {limit.remaining:,}")
print(f" Exceeded: {limit.exceeded}")
if limit.reset: # Unix timestamp, null for all_time
print(f" Resets at: {datetime.fromtimestamp(limit.reset)}")
Check with Filters
Check limits that match specific dimensions:Copy
# Check only GPT-4 limits
result = client.limits.check(
customer_id="cust_123",
dimensions={"model": "gpt-4"}
)
List Limits
Copy
# List all limits
limits = client.limits.list()
for limit in limits.data:
print(f"{limit.name} ({limit.id})")
print(f" Meter: {limit.meter_id}")
print(f" Limit: {limit.limit_value} per {limit.period}")
if limit.dimension_filters:
print(f" Filters: {limit.dimension_filters}")
# Filter by customer
limits = client.limits.list(customer_id="cust_123")
# Pagination
limits = client.limits.list(limit=50)
if limits.has_more:
next_page = client.limits.list(cursor=limits.next_cursor)
The
limit parameter in list() controls pagination (max items per page), not the limit value.Get a Limit
Copy
limit = client.limits.retrieve("lmt_abc123")
print(f"Name: {limit.name}")
print(f"Meter ID: {limit.meter_id}")
print(f"Limit Value: {limit.limit_value}")
print(f"Period: {limit.period}")
print(f"Customer: {limit.customer_id}")
print(f"Filters: {limit.dimension_filters}")
Update a Limit
Copy
limit = client.limits.update(
"lmt_abc123",
name="Updated limit name",
limit_value=200000, # Increase limit
alert_thresholds=[75, 90, 100]
)
Delete a Limit
Copy
result = client.limits.delete("lmt_abc123")
if result.success:
print("Limit deleted")