Skip to main content

Crate detect_coding_agent

Crate detect_coding_agent 

Source
Expand description

§detect-coding-agent

Detect whether your application is being invoked by an AI coding agent, such as Claude Code, GitHub Copilot Cloud Agent, OpenAI Codex, Cursor, Aider, Gemini CLI, and many more.

§Why?

When your application knows it is running inside an automated coding agent it can:

  • Suggest machine-friendly interfaces (e.g. MCP) instead of interactive UIs.
  • Emit structured output that the agent can parse more reliably.
  • Skip interactive prompts and use sensible defaults instead.
  • Provide agent-specific hints or documentation snippets.

§Quick start

use detect_coding_agent::{detect, is_agent};

if is_agent() {
    println!("Running inside a coding agent — switching to machine-friendly output.");
}

if let Some(agent) = detect() {
    println!("Detected: {} ({})", agent.name, agent.kind);
}

§Detection strategy

Detection is based on environment variables only (no subprocesses are spawned, no filesystem access is performed). The first provider whose signals match the current environment is returned.

Each provider has a documented source for its detection heuristic — see providers for the full list.

§Testing

Because detection reads the process environment, tests should supply an explicit environment snapshot via detect_with_env. Use std::collections::HashMap to build the environment:

use std::collections::HashMap;
use detect_coding_agent::detect_with_env;

let mut env = HashMap::new();
env.insert("CLAUDECODE".to_string(), "1".to_string());

let agent = detect_with_env(env).unwrap();
assert_eq!(agent.id, "claude-code");

Modules§

providers

Structs§

DetectedAgent
Information about a detected AI coding agent environment.

Enums§

AgentKind
The kind of AI coding environment detected.

Functions§

detect
Detect the AI coding agent present in the current process environment.
detect_with_env
Detect the AI coding agent using a caller-supplied environment map.
is_agent
Returns true when the current process is running inside an autonomous coding agent or a AgentKind::Hybrid environment.
is_hybrid
Returns true when the current process is running inside a AgentKind::Hybrid environment (supports both agent and interactive use).
is_interactive
Returns true when the current process is running inside an interactive AI-assisted coding tool or a AgentKind::Hybrid environment.