laravel/agent-detector
Lightweight PHP/Laravel utility to detect when your code runs inside an AI agent or automated dev environment. Detect via AgentDetector::detect() or detectAgent(), identify known agents like Cursor, Gemini, Codex, Claude, or custom via env vars.
Installation:
composer require laravel/agent-detector
First Use Case: Detect if code is running in an AI environment (e.g., GitHub Copilot, Claude, or Replit) in a single line:
use Laravel\AgentDetector\AgentDetector;
$result = AgentDetector::detect();
if ($result->isAgent) {
// Handle AI-generated execution (e.g., log, skip tests, or adjust behavior)
}
Where to Look First:
KnownAgent Enum: Reference for supported agents (e.g., KnownAgent::Claude, KnownAgent::Copilot).AgentDetector::AGENT_ENV_VARS: Public constant listing all environment variables used for detection (useful for debugging or custom extensions).Runtime Detection in Middleware/Service Providers:
// app/Http/Middleware/DetectAgentMiddleware.php
public function handle(Request $request, Closure $next) {
$result = AgentDetector::detect();
if ($result->knownAgent() === KnownAgent::Copilot) {
$request->attributes->add(['is_copilot' => true]);
}
return $next($request);
}
Conditional Logic in Commands/Jobs:
// app/Console/Commands/GenerateReport.php
protected function handle() {
$result = AgentDetector::detect();
if (!$result->isAgent) {
$this->generateReport(); // Only run manually
} else {
$this->warn("Skipping report generation in AI environment.");
}
}
Testing Integration:
// tests/Feature/AgentDetectionTest.php
public function test_agent_detection() {
putenv('AI_AGENT=github-copilot');
$result = AgentDetector::detect();
$this->assertTrue($result->isAgent);
$this->assertEquals(KnownAgent::Copilot, $result->knownAgent());
}
Environment-Specific Config:
Use the detection result to load environment-specific configurations (e.g., .env.copilot):
$result = AgentDetector::detect();
if ($result->knownAgent() === KnownAgent::Copilot) {
$this->loadEnvironmentFrom('.env.copilot');
}
Custom Agents:
Extend detection for proprietary tools by setting AI_AGENT:
AI_AGENT=my-company-ai php artisan my:command
Laravel Service Container: Bind the detector for dependency injection:
// app/Providers/AppServiceProvider.php
public function register() {
$this->app->singleton(AgentDetector::class, fn() => AgentDetector::detect());
}
False Positives:
isAgent if they set AI_AGENT. Verify with:
if ($result->isAgent && !$this->isCiEnvironment()) { ... }
Namespace Changes (v2.0+):
AgentDetector; v2.0+ uses Laravel\AgentDetector. Update imports if migrating.File-Based Detection (e.g., Devin):
/opt/.devin may fail in Docker/containerized environments. Test locally first.dd(AgentDetector::AGENT_ENV_VARS); // List all checked env vars
AgentDetector by overriding detect() or adding new checks to AGENT_ENV_VARS.Performance: Cache the detection result if called repeatedly (e.g., in middleware):
$result = app()->bound('agent_detector') ?? AgentDetector::detect();
app()->singleton('agent_detector', $result);
User-Agent Headers:
For HTTP-based detection (e.g., browser extensions), combine with Request->userAgent():
if (str_contains($request->userAgent(), 'AI-Agent')) { ... }
Testing:
Use putenv() to simulate AI environments in tests:
putenv('CLAUDE_CODE=1');
$this->assertTrue(AgentDetector::detect()->isAgent);
Extending Detection:
Add new agents by extending the KnownAgent enum and updating AgentDetector::detect():
// app/Enums/CustomAgent.php
namespace App\Enums;
use Laravel\AgentDetector\KnownAgent;
class CustomAgent {
public static function detect(): ?KnownAgent {
return env('MY_CUSTOM_AGENT') ? KnownAgent::Custom : null;
}
}
How can I help you explore Laravel packages today?