~/sdairs git:(main) $ cat posts/is-ai-agent-rust-crate.md

is-ai-agent for Rust: detect if your CLI is invoked by an agent

I published a new Rust crate is-ai-agent. It lets you detect when your CLI is being called an AI agent, by detecting standard/common environment variables that various agents set.

It’s a very simple API:

use is_ai_agent::{detect, is_ai_agent};

if is_ai_agent() {
    // emit structured/agent-friendly output
}

if let Some(agent) = detect() {
    eprintln!("running under {}", agent.name);
}

It’s not perfect, as there’s no standard signal for agents to identify themselves yet. There is an ongoing discussion for creating a standard AGENT variable, but it seems that only a few of the smaller open source agents have adopted it so far.

For now, this is the environment variable and agent coverage:

VariableAgent
CLAUDECODE, CLAUDE_CODEClaude Code
CURSOR_TRACE_IDCursor (editor)
CURSOR_AGENT, CURSOR_EXTENSION_HOST_ROLE=agent-execCursor CLI
GEMINI_CLIGemini CLI
CODEX_SANDBOX, CODEX_CI, CODEX_THREAD_IDOpenAI Codex
ANTIGRAVITY_AGENTAntigravity
AUGMENT_AGENTAugment
CLINE_ACTIVECline
OPENCODE_CLIENTOpenCode
TRAE_AI_SHELL_IDTRAE AI
GOOSE_TERMINALGoose
REPL_IDReplit
COPILOT_MODEL, COPILOT_ALLOW_ALL, COPILOT_GITHUB_TOKENGitHub Copilot

I needed this for clickhousectl, so I’ll be keeping the rules up to date.

There’s a similar lib for TypeScript with @vercel/detect-agent.