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.
.env files).AI_AGENT env var, allowing future-proofing for new tools.Laravel\AgentDetector), but works in vanilla PHP due to its simplicity.AppServiceProvider for global access via a facade or singleton.AI_AGENT).
Mitigation: Use alongside other heuristics (e.g., request headers, user-agent parsing).AgentDetector → Laravel\AgentDetector). Ensure all references are updated.// app/Providers/AppServiceProvider.php
public function boot(): void {
$this->app->singleton(AgentDetector::class);
}
public function handle(Request $request, Closure $next) {
$request->merge(['is_agent' => detectAgent()->isAgent]);
return $next($request);
}
AgentDetector to an interface for testability.detectAgent() function in CLI scripts or non-Laravel apps.AgentDetector in unit tests using PHP’s getenv() overrides or filesystem stubs.AgentDetector.AI_AGENT for testing (e.g., AI_AGENT=github-copilot).composer require php:^8.2 if needed)..env files are loaded early (Laravel handles this by default).AI_AGENT=my-agent php artisan my:command
/opt/.devin; ensure your deployment has read access.composer require laravel/agent-detector
use AgentDetector\... with use Laravel\AgentDetector\... (if upgrading from v1.x).AppServiceProvider or a dedicated service class.// app/Services/AgentService.php
public function isAgent(): bool {
return detectAgent()->isAgent;
}
public function test_agent_detection() {
putenv('AI_AGENT=github-copilot');
$this->assertTrue(detectAgent()->isAgent);
}
KnownAgent values).AI_AGENT for testing..env.testing for tests).AI_AGENT set accidentally in production).dd(getenv(), detectAgent());
| Scenario | Support Level | Notes |
|---|---|---|
| Basic detection | High | Core functionality. |
| Custom agent support | Medium | Requires env var setup. |
| False positives | Low | May need app-specific heuristics. |
| Performance issues | High | Optimize caching if needed. |
// Cache for 1 minute
$result = Cache::remember('agent_detection', 60, fn() => detectAgent());
| Failure Mode | Impact | Mitigation Strategy |
|---|---|---|
| Env var not set | False negative (agent undetected) | Fallback to isAgent = false or custom logic. |
| Filesystem permission error | Devin/Antigravity undetected | Graceful fallback (e.g., assume not an agent). |
| Package update breaks code | Namespace/interface changes | Pin version in composer.json temporarily. |
| Custom agent misconfigured | False positive | Validate AI_AGENT values against a whitelist. |
detectAgent() in code.AI_AGENT for local testing.## Agent Detection Quick Start
- **Detect**: `detectAgent()->isAgent`
- **Test Locally**: `AI_AG
How can I help you explore Laravel packages today?