shipfastlabs/agent-detector
Lightweight PHP/Laravel utility to detect when your code runs inside an AI agent or automated dev environment. Checks common agent-specific environment variables (e.g., Cursor, Gemini, Codex, or custom) and returns agent status and name.
.env files, service providers).AI_AGENT environment variable, allowing teams to adapt the package to future or proprietary AI tools without forking the library.Laravel\AgentDetector) and Laravel-style naming conventions reduce friction.v0 or Cowork are added via PRs).AI_AGENT env var)./admin route?"HandleAgentDetection).app()->make(AgentDetector::class)).php artisan migrate)..env files).composer require laravel/agent-detector
detectAgent() in any PHP file.
$result = detectAgent();
if ($result->isAgent) { ... }
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(AgentDetector::class, function () {
return new AgentDetector();
});
}
// app/Http/Middleware/DetectAgentMiddleware.php
public function handle(Request $request, Closure $next)
{
$result = detectAgent();
if ($result->isAgent) {
// Example: Block access or log the agent
abort(403, 'AI agents not allowed.');
}
return $next($request);
}
Register in app/Http/Kernel.php:
protected $middleware = [
\App\Http\Middleware\DetectAgentMiddleware::class,
];
AI_AGENT=my-custom-agent in your environment to test custom detection.CLAUDE_CODE), so ensure your deployment platform (e.g., Docker, Heroku, AWS) supports passing these variables.detectAgent() in a non-critical endpoint (e.g., a test route).DetectAgentMiddleware to gate sensitive routes.if ($result->isAgent) {
Log::info('AI Agent detected', ['agent' => $result->name]);
}
composer update laravel/agent-detector).CLAUDE_CODE) are set correctly in your deployment environment.AgentDetector::AGENT_ENV_VARS to inspect detected variables.ENV CLAUDE_CODE=your-value # If using Claude
| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Env var not set for known agent | False negative (agent undetected) | Use custom AI_AGENT fallback or log warnings. |
| Custom env var conflicts | False positive (non-agent detected) | Validate env vars against a whitelist (e.g., AgentDetector::AGENT_ENV_VARS). |
| PHP version incompatibility | Package fails to load | Pin to a compatible version in composer.json (e.g., |
How can I help you explore Laravel packages today?