Skip to content

[2.x] Detect Claude via AI_AGENT prefix#17

Merged
pushpak1300 merged 1 commit into
laravel:mainfrom
jackbayliss:detect-claude
Apr 29, 2026
Merged

[2.x] Detect Claude via AI_AGENT prefix#17
pushpak1300 merged 1 commit into
laravel:mainfrom
jackbayliss:detect-claude

Conversation

@jackbayliss

@jackbayliss jackbayliss commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

I was playing around with this locally, just to see what I could do with it.

Used the code snippet in the readme, but noticed it wasn't detecting Claude as a known agent.

Code snippet
<?php

namespace App\Console\Commands;

use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
use Laravel\AgentDetector\AgentDetector;
use Laravel\AgentDetector\KnownAgent;

#[Signature('app:example')]
#[Description('Command description')]
class ExampleCommand extends Command
{
    /**
     * Execute the console command.
     */
    public function handle()
    {
        $this->info('Hello World!');
        $result = AgentDetector::detect();

        if ($result->isAgent) {
            $this->info("Running inside: {$result->name}");
        }

        if ($result->knownAgent() === KnownAgent::Claude) {
            $this->info("Hello from Claude!");
        }
    }
}

I was expecting to see 'Hello from Claude':
image

With the change, its now detected properly:

image

I've put the logic in the match as it felt nicer than having a bunch of return points around name matching.

AI_AGENT is checked first afaik, which is why CLAUDECODE by itself doesnt trigger the right name, this could happen for other agents too.

Maybe self::fromKnownEnvVars() should be checked first? But I'm not sure, so went with this surgical approach for now :)

@github-actions

Copy link
Copy Markdown

Thanks for submitting a PR!

Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@pushpak1300

Copy link
Copy Markdown
Member

I think we can move AI_AGENT check at the end.

@jackbayliss

jackbayliss commented Apr 29, 2026

Copy link
Copy Markdown
Contributor Author

@pushpak1300 I did think the same, but prioritizes CURSOR_AGENT over CLAUDECODE fails is we do that. Perhaps you can see a better way though? Thanks for the reply 👍🏻

@jackbayliss jackbayliss marked this pull request as ready for review April 29, 2026 10:48
@pushpak1300 pushpak1300 self-requested a review April 29, 2026 18:31
@pushpak1300 pushpak1300 merged commit 90694b9 into laravel:main Apr 29, 2026
21 checks passed
@pushpak1300

Copy link
Copy Markdown
Member

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants