shipfastlabs/agent-detector
Lightweight Laravel/PHP utility to detect when your code is running inside an AI agent or automated dev environment. Supports multiple known agents (e.g., Cursor, Gemini, Codex, Claude) via environment-variable detection. Requires PHP 8.2+.
Install via Composer:
composer require laravel/agent-detector
First use case: Detect AI agents in your Laravel application's AppServiceProvider boot method or middleware:
use Laravel\AgentDetector\AgentDetector;
public function boot()
{
$result = AgentDetector::detect();
if ($result->isAgent) {
Log::info("Running in AI environment: {$result->name}");
// Customize behavior for AI agents
}
}
Key starting points:
AgentDetector::detect() - Core detection methoddetectAgent() - Standalone function aliasKnownAgent enum - Predefined agent typesAgentResult object - Contains detection results// Basic detection
$result = AgentDetector::detect();
// Check for any agent
if ($result->isAgent) {
// Agent detected
}
// Check specific agent
if ($result->knownAgent() === KnownAgent::Claude) {
// Claude-specific logic
}
public function handle(Request $request, Closure $next)
{
$result = AgentDetector::detect();
if ($result->isAgent && $result->knownAgent() === KnownAgent::Copilot) {
return response()->json(['message' => 'Copilot detected'], 403);
}
return $next($request);
}
public function register()
{
$this->app->singleton('agent-detector', function () {
return AgentDetector::detect();
});
}
// Set environment variable
putenv('AI_AGENT=my-custom-agent');
// Or in .env
// AI_AGENT=my-custom-agent
// Then detect
$result = AgentDetector::detect();
if ($result->name === 'my-custom-agent') {
// Custom logic
}
// In PHPUnit tests
public function testAgentDetection()
{
putenv('AI_AGENT=github-copilot');
$result = AgentDetector::detect();
$this->assertTrue($result->isAgent);
$this->assertEquals(KnownAgent::Copilot, $result->knownAgent());
}
Environment Variables: Detection relies on environment variables. Ensure they're properly set in your deployment environment.
# Example for testing
AI_AGENT=github-copilot php artisan tinker
Namespace Changes: v2.0.0 changed namespace from AgentDetector to Laravel\AgentDetector. Update imports if migrating.
False Positives: Some environments might trigger multiple detection methods. Verify with AgentDetector::AGENT_ENV_VARS:
print_r(AgentDetector::AGENT_ENV_VARS);
Performance: Detection is lightweight but not free. Cache results if checking frequently:
$result = cache()->remember('agent-detection', now()->addHours(1), function() {
return AgentDetector::detect();
});
Inspect Detection Logic:
$result = AgentDetector::detect();
dd($result->name, $result->isAgent, $result->knownAgent());
Check All Environment Variables:
foreach (AgentDetector::AGENT_ENV_VARS as $var) {
echo "$var = " . getenv($var) . "\n";
}
Custom Detection: Extend the detector by creating a custom class:
class CustomAgentDetector extends AgentDetector
{
protected static function detectCustomAgent(): ?string
{
return getenv('MY_CUSTOM_VAR') ? 'my-custom-agent' : null;
}
}
KnownAgent Enum: Use label() method for human-readable names:
echo KnownAgent::Claude->label(); // "Claude"
AgentResult Properties:
isAgent: Boolean indicating if any agent detectedname: String name of detected agentknownAgent(): Returns KnownAgent enum if matches known agentsEnvironment Variable Priority: The detector checks variables in a specific order. Newer agents may override older ones.
Add New Agent Detection:
// In a service provider
AgentDetector::addDetectionMethod(function() {
return getenv('NEW_AGENT_VAR') ? 'new-agent' : null;
});
Custom Agent Detection Class:
class MyAgentDetector extends AgentDetector
{
public static function detect(): AgentResult
{
$result = parent::detect();
if ($result->name === 'custom') {
// Custom logic
}
return $result;
}
}
Override Detection Logic:
// Monkeypatch for testing
AgentDetector::setDetectionMethods([...]);
How can I help you explore Laravel packages today?