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
Requires PHP 8.2+.
First Detection Check:
use Laravel\AgentDetector\AgentDetector;
$result = AgentDetector::detect();
if ($result->isAgent) {
echo "Detected agent: " . $result->name;
}
Quick Standalone Usage:
use function Laravel\AgentDetector\detectAgent;
$agent = detectAgent();
Conditional Logic for AI Agents:
if ($agent->knownAgent() === KnownAgent::Claude) {
// Provide Claude-specific guidance or hints
return "Claude-specific response";
}
// Disable heavy computations for AI agents
if ($agent->isAgent) {
return response()->json(['warning' => 'AI agents may not support this feature']);
}
switch ($agent->knownAgent()) {
case KnownAgent::Copilot:
return "Copilot: Use `php artisan make:model` for scaffolding";
case KnownAgent::Replit:
return "Replit: Run `composer install` first";
default:
return "General instructions...";
}
// Block AI agents from sensitive operations
if ($agent->isAgent && $request->is('admin/*')) {
abort(403, 'AI agents cannot access admin routes');
}
// app/Providers/AppServiceProvider.php
public function boot()
{
$this->app->singleton('agent', function () {
return AgentDetector::detect();
});
}
Usage in Controllers:
use Illuminate\Support\Facades\App;
if (App::make('agent')->isAgent) {
// Agent-specific logic
}
// app/Http/Middleware/DetectAgent.php
public function handle(Request $request, Closure $next)
{
$request->merge(['is_agent' => AgentDetector::detect()->isAgent]);
return $next($request);
}
Route Middleware:
Route::middleware(['detect.agent'])->group(function () {
// Agent-aware routes
});
// app/Providers/BladeServiceProvider.php
Blade::directive('agentCheck', function ($expression) {
return "<?php if (Laravel\\AgentDetector\\AgentDetector::detect()->{$expression}): ?>";
});
Usage in Views:
@agentCheck('isAgent')
<p>This content is for AI agents only.</p>
@endagentCheck
False Positives:
GITHUB_ACTIONS=true, which isn’t directly detected but could conflict with agent logic.Environment Variable Overrides:
COPILOT_MODEL and AI_AGENT). Prioritize checks based on your use case:
if ($agent->knownAgent() === KnownAgent::Copilot) {
// Handle Copilot specifically
} elseif ($agent->isAgent) {
// Fallback for other agents
}
Custom Agent Naming:
AI_AGENT env var supports any value, but ensure consistency in your team’s naming conventions (e.g., my-team-agent).Inspect All Detected Agents:
$result = AgentDetector::detect();
dd($result->name, $result->isAgent, $result->envVars);
Check Environment Variables:
env | grep -i "AI_AGENT\|COPILOT\|CLAUDE"
Update Detection Logic:
Performance:
$agent = Cache::remember('agent_detection', now()->addHours(1), fn() => AgentDetector::detect());
Extending Detection:
KnownAgent enum or modifying AgentDetector::AGENT_ENV_VARS:
AgentDetector::AGENT_ENV_VARS['MY_CUSTOM_AGENT'] = ['MY_CUSTOM_VAR'];
Testing:
$this->app->instance('agent', (new AgentResult('test-agent', true)));
Human vs. Agent Logic:
Security:
Pro Tip: Combine with laravel/ai packages for richer AI integration (e.g., laravel/ai + laravel/agent-detector for agent-aware AI responses).
How can I help you explore Laravel packages today?