Skip to main content

Limiter for AI Agents

What Is a Limiter?

A limiter is a mechanism that restricts the number of AI agent jobs a user can run concurrently. Its primary purpose is to prevent resource abuse, especially excessive AI token consumption and queue overload.

The core implementation is located in: App\Services\AiChat\UserJobLimiter.php

Configuration

The limiter is configured via the following environment variable:

AI_MAX_CONCURRENT_JOBS=5

This setting is applied per user.

How It Works

  • Each user can have only N concurrent AI jobs running in the queue at any given time.

  • For example, a user may submit AI requests from multiple chats in parallel:

    • Chat A → AI request
    • Chat B → AI request
  • Once the number of active jobs reaches the configured limit (N), any additional requests are blocked or deferred until an existing job completes.

By default, the limit is set to 5 concurrent jobs per user.

Testing

Automated tests for this functionality are located at:

tests/Feature/Services/AiChat/UserJobLimiterTest.php

These tests ensure the limiter behaves correctly under various concurrency scenarios.