Skip to content

Add AI agent detection to user-agent string#1537

Open
simonfaltum wants to merge 6 commits intomainfrom
simonfaltum/agent-detection
Open

Add AI agent detection to user-agent string#1537
simonfaltum wants to merge 6 commits intomainfrom
simonfaltum/agent-detection

Conversation

@simonfaltum
Copy link
Member

@simonfaltum simonfaltum commented Mar 12, 2026

Changes

The SDK now detects known AI coding agents via environment variables and appends agent/<name> to the User-Agent header on every HTTP request. If zero or multiple agents are detected, no agent segment is added (ambiguity guard).

Env Var Agent Name
ANTIGRAVITY_AGENT antigravity
CLAUDECODE claude-code
CLINE_ACTIVE cline
CODEX_CI codex
COPILOT_CLI copilot-cli
CURSOR_AGENT cursor
GEMINI_CLI gemini-cli
OPENCODE opencode
OPENCLAW_SHELL openclaw

The COPILOT_CLI env var was confirmed by direct testing in Copilot CLI.
The OPENCLAW_SHELL env var uses context-qualified values (exec, acp, acp-client); any non-empty value triggers detection.

This is part of a cross-SDK effort to add agent detection to Go, Python, and Java SDKs with identical agent lists.

Tests

  • 13 unit tests in useragent/agent_test.go: each of the 9 agents individually, no agent, multiple agents, empty value, caching behavior.
  • 2 integration tests in config/visitors_test.go: agent detected in UA context, no agent in UA context.
  • All 15 tests pass. go vet clean.

Detect known AI coding agents (Claude Code, Cursor, Cline, Codex,
Gemini CLI, OpenCode, Antigravity) via environment variables and
append agent/<name> to the User-Agent header on HTTP requests.
Returns empty when zero or multiple agents are detected (ambiguity
guard). Part of a cross-SDK effort (Go, Python, Java).

Co-authored-by: Isaac
Signed-off-by: simon <simon.faltum@databricks.com>
The previous tests in config/visitors_test.go manually built the UA
context instead of going through the HTTP client visitor chain. This
meant they would pass even if the visitor wiring in api_client.go
were removed.

New tests in client/client_test.go follow the same pattern as
TestUserAgentForCiCd: create a real HTTP client, make a request,
and capture the actual User-Agent header to verify the agent/ entry
appears end-to-end.

Co-authored-by: Isaac
Signed-off-by: simon <simon.faltum@databricks.com>
Copy link
Member Author

@simonfaltum simonfaltum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review (automated, 2 agents)

Verdict: Approved

0 Critical | 0 Major | 2 Gap | 1 Nit | 1 Suggestion

See inline comments for details.

Add early exit in lookupAgentProvider when count > 1 to
communicate intent more clearly. Add TestUserAgentForMultipleAgents
client-level integration test for the multi-agent ambiguity guard.

Co-authored-by: Isaac
@simonfaltum simonfaltum marked this pull request as ready for review March 12, 2026 13:57
We have data from direct testing in Copilot CLI confirming it sets
COPILOT_CLI=1 in its environment. Add it to the canonical agent list.

Co-authored-by: Isaac
Signed-off-by: simon <simon.faltum@databricks.com>
@github-actions
Copy link

If integration tests don't run automatically, an authorized user can run them manually by following the instructions below:

Trigger:
go/deco-tests-run/sdk-go

Inputs:

  • PR number: 1537
  • Commit SHA: d5bae20bc135363cdf60a7d4f8f61aa1953557bc

Checks will be approved automatically on success.

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.

1 participant