Skip to main content

Introduction

laravel/agent-detector is a lightweight utility for detecting whether PHP code is running inside an AI agent or automated development environment. It was published as an official Laravel package in April 2026. Now that AI agents are deeply embedded in the development workflow, it has become important to know what environment your code is running in. Real-world cases include adjusting log verbosity or rate limits depending on whether a request comes from an AI agent or a human, or returning agent-specific responses.

Installation

Requires PHP 8.2 or later.

Basic usage

Calling AgentDetector::detect() returns an AgentResult object.
You can also use the standalone function:

AgentResult properties

Supported agents

Detection priority is: AI_AGENT env var → known env vars → filesystem.

Configuring custom agents

Set any value in the AI_AGENT environment variable to have it detected as a custom agent.

Practical use cases

Detect AI agents in middleware

Use Laravel middleware to determine the execution environment and branch to agent-specific behavior.

Emit verbose logs in AI agent environments

Apply rate limits to requests from AI agents

By varying the ThrottleRequests middleware key based on agent detection, you can set different rate limits for agents and humans.

Switch behavior in console commands

An example of emitting detailed progress when an agent runs an Artisan command.

Summary

laravel/agent-detector bundles simple environment variable checks and filesystem inspection into a single package. Just call AgentDetector::detect()—no complex configuration required. As AI agents become an established part of the development flow, understanding “who (or what) is running your code” only grows in importance. This package can be useful in many scenarios such as logging, rate limiting, and customizing responses.

laravel/agent-detector repository

Source code and the latest list of supported agents.
Last modified on July 13, 2026