Repository navigation
feat: detect AI coding agents in the user agent - #419
Merged
Merged
Conversation
NickJosevski
force-pushed
the
robe/claude-ua
branch
from
August 14, 2026 04:37
365a62e to
25dc418
Compare
NickJosevski
approved these changes
Aug 14, 2026
NickJosevski
left a comment
Contributor
There was a problem hiding this comment.
you still want this in?
Report the AI agent driving a process as its own user agent token, separate from the automation (CI) environment, so an agent running in CI or on an Octopus worker reports both rather than one at random. - Honour the emerging AI_AGENT convention, mapping known values to a canonical name and passing the sanitised raw value through as a comment - Fall back to vendor variables for Claude Code, Cursor, Codex, Gemini CLI, GitHub Copilot, OpenCode, Antigravity, Goose, Amp, Cline, Roo Code, Augment, Qwen Code, Factory Droid, Kimi CLI, Trae, Aider and OpenHands, checked in a fixed order - Exclude variables that are set without an agent driving the process (e.g. CURSOR_TRACE_ID, COPILOT_GITHUB_TOKEN, REPL_ID) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T4ijpGHFpAqBBMhn5WKUq2
Map AI_AGENT to one of the known agent names instead of passing the raw value through as a user agent comment. Unrecognised values report as AIAgent/Other. The raw value is free-form and user-settable, so it is now only used for matching, which also removes the need to sanitise it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T4ijpGHFpAqBBMhn5WKUq2
zentron
force-pushed
the
robe/claude-ua
branch
from
November 12, 2026 22:57
0d73c0d to
3422651
Compare
Collapse runs of underscores and whitespace into a single hyphen before matching a self-reported agent identifier, so a name such as "Claude Code" maps the same way in all three clients. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T4ijpGHFpAqBBMhn5WKUq2
Merge the AI_AGENT prefix table into the agent list so each agent is one entry, keeping two lookups (prefix, then vendor variables) so a self-reported identity still wins. Rename the AI_AGENT local and trim the tests to edge cases plus a happy path, matching review on the .NET port. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T4ijpGHFpAqBBMhn5WKUq2
Match the .NET and TypeScript clients, which keep the full set. Drops the 200-character AI_AGENT case, which dated from when the raw value was sent and length-capped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T4ijpGHFpAqBBMhn5WKUq2
Keep edge cases that guard real behaviour plus a happy path, and drop the per-agent table that restated the agent list. The same cases are used in the Go, .NET and TypeScript clients. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T4ijpGHFpAqBBMhn5WKUq2
egorpavlikhin
approved these changes
Nov 19, 2026
egorpavlikhin
left a comment
There was a problem hiding this comment.
Looks to be doing the same thing as the ts client 👍
A self-reported identifier now only matches an agent's prefix when the prefix is followed by the end of the identifier or a separator (-, @, .) or digit. Previously a short prefix such as "amp" matched an unrelated identifier such as "amplitude-mcp", and because self-reported identifiers take precedence, that also hid a correct match from vendor variables. Applied the same way in the Go, .NET and TypeScript clients. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T4ijpGHFpAqBBMhn5WKUq2
Report the agent as an RFC 9110 comment, (ai-agent=Name), appended at the very end of the User-Agent. Everything before it is byte-for-byte what the client sent before, so servers and tools that match on the start of the header keep working (Octopus Server identifies the local MCP server by the "ts-octopusdeploy octopus-mcp-server" prefix). A comment also avoids presenting the agent as a product/version pair. Applied the same way in the Go, .NET and TypeScript clients. Also normalise AI_AGENT the same way as the .NET and TypeScript clients, and group imports per golangci-lint's gci sections now that main is merged in. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T4ijpGHFpAqBBMhn5WKUq2
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T4ijpGHFpAqBBMhn5WKUq2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
AI coding agents increasingly drive the Octopus CLI, the Terraform provider and other tools built on this SDK, but none of that traffic is identifiable today. Agents don't change the headers of the tools they run; they only export environment variables to the processes they launch. Reading those variables here gives the server a signal for agent-driven API usage.
Results
When an agent is detected, an
(ai-agent=<Name>)comment is appended to the very end of the user agent:AI_AGENTfirst. This is the emerging cross-vendor convention (@vercel/detect-agent). Its value is mapped to a known name by prefix, e.g.claude-code_2-1-322_agent→ClaudeCode,github_copilot_vscode_agent→GitHubCopilot. A prefix only matches when followed by the end of the value, a separator (-,@,.) or a digit, soampdoesn't matchamplitude-mcp. The raw value is never sent, since it is free-form and user-settable. An unrecognised value reports as(ai-agent=Other).AI_AGENTprefix and its vendor variables.CURSOR_TRACE_ID,COPILOT_GITHUB_TOKEN,GOOSE_PROVIDER,REPL_IDandZED_TERMare set in ordinary human sessions, so they would label people as agents.Claude Codeandclaude_codemap the same way.Testing
ai_agent_provider_test.gocovers the edge cases: value-matched rules, excluded variables, nested agents,AI_AGENTformats and precedence, the prefix boundary, and that the rawAI_AGENTvalue never appears.ai_agent_provider_internal_test.gochecks that appending the agent leaves the existing user agent as an exact prefix. The existing CI detection tests still pass.(ai-agent=ClaudeCode)in a real Claude Code session.mainis merged in andgolangci-lintpasses.How to review this PR
The logic is all in
pkg/services/api/ai_agent_provider.go.api.gobuilds the user agent exactly as before and then callsappendAIAgent. The agent list is the part most worth a second opinion: the variable names come from other detectors (.NET Aspire'sCodingAgentDetector,@vercel/detect-agent,is-ai-agent) and are mostly undocumented vendor internals. This file is the reference copy of the list: the TypeScript port (api-client.ts#294) and the .NET port point back to it in a comment.Caveats
Other(if it setsAI_AGENT) or not at all until it is added and an SDK release ships.AGENT=ampcomes from a secondary source. If Amp doesn't set it, that rule never fires.Generated by Claude Code