laravel/agent-detector
Lightweight PHP utility for detecting whether your Laravel/PHP app is running inside an AI agent or automated dev environment. Identifies known agents (e.g., Cursor, Gemini, Codex, Claude) via env vars, with a simple API and helper function.
Installation: Add the package via Composer:
composer require laravel/agent-detector
Requires PHP 8.2+.
First Check: Import and use the detection in your entry point (e.g., bootstrap/app.php, routes/web.php, or a service provider):
use Laravel\AgentDetector\AgentDetector;
$result = AgentDetector::detect();
Basic Use Case: Conditionally execute logic for AI agents:
if ($result->isAgent) {
// Disable heavy operations, mock data, or log AI activity
info("Running in AI environment: {$result->name}");
}
Check for Specific Agents: Use the KnownAgent enum for precise targeting:
if ($result->knownAgent() === KnownAgent::Claude) {
// Custom logic for Claude
}
Create middleware to detect AI agents across all requests:
// app/Http/Middleware/DetectAgent.php
public function handle(Request $request, Closure $next) {
$result = AgentDetector::detect();
$request->attributes->add(['is_agent' => $result->isAgent, 'agent_name' => $result->name]);
return $next($request);
}
Register in app/Http/Kernel.php:
protected $middleware = [
\App\Http\Middleware\DetectAgent::class,
];
Initialize detection early in a service provider:
// app/Providers/AppServiceProvider.php
public function boot() {
$this->app->singleton('agent-detector', function () {
return AgentDetector::detect();
});
}
Access via dependency injection:
public function __construct(private readonly AgentResult $detector) {}
Skip slow tests or mock responses when running in an AI environment:
// tests/Feature/SomeTest.php
public function testSomething(): void {
$detector = AgentDetector::detect();
if ($detector->isAgent) {
$this->mockHttpResponses();
}
// ...
}
Load AI-specific configs (e.g., .env.agent):
// config/agent.php
return [
'enabled' => env('AGENT_ENABLED', $detector->isAgent),
// ...
];
Extend detection for proprietary tools by setting AI_AGENT:
AI_AGENT=my-tool php artisan tinker
Or programmatically:
putenv('AI_AGENT=my-tool');
Laravel\AgentDetector (previously AgentDetector). Update imports if upgrading./opt/.devin may fail in containerized or cloud environments. Test thoroughly.AgentDetector::AGENT_ENV_VARS to debug environment variables:
dd(AgentDetector::AGENT_ENV_VARS);
$detector = new AgentResult('test-agent');
$this->app->instance('agent-detector', $detector);
$detector = app()->bound('agent-detector') ?? AgentDetector::detect();
app()->singleton('agent-detector', $detector);
AgentDetector:
class CustomAgentDetector extends AgentDetector {
protected static function detectCustomAgent(): ?string {
return getenv('MY_CUSTOM_AGENT') ? 'my-agent' : null;
}
}
KnownAgent::from($result->name)->label() for human-readable names:
echo KnownAgent::from($result->name)->label(); // "GitHub Copilot"
GITHUB_ACTIONS or similar:
if ($detector->isAgent && !env('CI')) {
// AI-only logic
}
KnownAgent enum for breaking changes (e.g., renamed agents).How can I help you explore Laravel packages today?