Rate limiting and budget controls help manage how much and how often an AI system can be used. This keeps the system fair and prevents overuse.
Rate limiting and budget controls in Agentic AI
Start learning this pattern below
Jump into concepts and practice - no test required
rate_limit = RateLimiter(max_requests=100, time_window=60) # 100 requests per 60 seconds budget_control = BudgetController(max_cost=50.0) # $50 max spending if rate_limit.allow_request(user_id): if budget_control.can_spend(cost): response = ai_model.process(request) budget_control.record_spend(cost) else: response = 'Budget exceeded' else: response = 'Rate limit exceeded'
RateLimiter controls how many requests can happen in a set time.
BudgetController tracks spending and stops requests if the budget is used up.
rate_limit = RateLimiter(max_requests=10, time_window=60) # 10 requests per minute budget_control = BudgetController(max_cost=20.0) # $20 max # Check before processing if rate_limit.allow_request('user123') and budget_control.can_spend(1.5): # Process AI request budget_control.record_spend(1.5)
rate_limit = RateLimiter(max_requests=5, time_window=10) # 5 requests per 10 seconds for i in range(7): if rate_limit.allow_request('user123'): print(f'Request {i+1} allowed') else: print(f'Request {i+1} blocked')
This program simulates 5 requests from one user. It allows up to 3 requests every 5 seconds and a total spending of $5. Each request costs $2. It prints if each request is allowed or blocked.
import time class RateLimiter: def __init__(self, max_requests, time_window): self.max_requests = max_requests self.time_window = time_window self.requests = {} def allow_request(self, user_id): now = time.time() if user_id not in self.requests: self.requests[user_id] = [] # Remove old requests self.requests[user_id] = [t for t in self.requests[user_id] if now - t < self.time_window] if len(self.requests[user_id]) < self.max_requests: self.requests[user_id].append(now) return True return False class BudgetController: def __init__(self, max_cost): self.max_cost = max_cost self.spent = 0.0 def can_spend(self, cost): return (self.spent + cost) <= self.max_cost def record_spend(self, cost): self.spent += cost # Setup rate_limit = RateLimiter(max_requests=3, time_window=5) # 3 requests per 5 seconds budget_control = BudgetController(max_cost=5.0) # $5 max user_id = 'user1' request_cost = 2.0 for i in range(5): if rate_limit.allow_request(user_id): if budget_control.can_spend(request_cost): budget_control.record_spend(request_cost) print(f'Request {i+1}: Allowed, spent ${budget_control.spent}') else: print(f'Request {i+1}: Blocked - Budget exceeded') else: print(f'Request {i+1}: Blocked - Rate limit exceeded') time.sleep(1)
Rate limiting helps protect your AI system from too many requests at once.
Budget controls help you avoid unexpected costs by limiting spending.
Both controls can be combined for better management.
Rate limiting controls how often users can make requests.
Budget controls limit how much money can be spent on AI usage.
Using both keeps your AI system fair, stable, and cost-effective.
Practice
Solution
Step 1: Understand rate limiting concept
Rate limiting is about controlling the number of requests a user can make in a time period.Step 2: Identify the main purpose
This control helps prevent overload and keeps the system fair for all users.Final Answer:
To control how often users can make requests -> Option AQuick Check:
Rate limiting = control request frequency [OK]
- Confusing rate limiting with improving AI accuracy
- Thinking rate limiting increases speed
- Mixing rate limiting with data storage
Solution
Step 1: Identify correct syntax for budget control
Setting a budget limit usually involves assigning a numeric value to a variable representing money allowed.Step 2: Check each option
budget_limit = 1000 # sets max money allowed uses a clear assignment with a comment, which is correct syntax. Others use invalid syntax or unclear expressions.Final Answer:
budget_limit = 1000 # sets max money allowed -> Option DQuick Check:
Assign numeric budget limit = correct [OK]
- Using strings instead of numbers for budget
- Calling undefined functions like setBudget
- Using incorrect syntax like max(1000)
requests = [1,1,1,1,1,1] limit = 5 allowed = sum(requests[:limit]) print(allowed)What will be the printed output?
Solution
Step 1: Understand the code slicing and summing
The code sums the first 5 elements of the list requests, each element is 1.Step 2: Calculate the sum of first 5 elements
Sum = 1+1+1+1+1 = 5Final Answer:
5 -> Option CQuick Check:
Sum first 5 ones = 5 [OK]
- Summing all 6 elements instead of 5
- Confusing slicing syntax
- Expecting an error due to slicing
max_requests = 10
requests_made = 0
if requests_made > max_requests:
print("Limit reached")
else:
requests_made += 1
print("Request allowed")Solution
Step 1: Analyze the condition for rate limiting
The code blocks requests if requests_made is greater than max_requests, but it should block when equal too.Step 2: Correct the condition
Change > to >= to include the max_requests limit properly.Final Answer:
The condition should be >= instead of > -> Option BQuick Check:
Use >= to block at limit [OK]
- Using > misses blocking at exact limit
- Starting requests_made at 1 is unnecessary
- Swapping print messages confuses logic
Solution
Step 1: Understand the need for both controls
Rate limiting controls request count; budget controls money spent. Both must be checked.Step 2: Evaluate options for combining controls
Set daily_limit = 1000 and budget_limit = 500; check both before allowing requests correctly sets both limits and checks them before allowing requests. Others ignore one control or swap values incorrectly.Final Answer:
Set daily_limit = 1000 and budget_limit = 500; check both before allowing requests -> Option AQuick Check:
Use both limits together for control [OK]
- Ignoring budget when rate limiting is set
- Assuming budget controls requests automatically
- Swapping limit values causing confusion
