Thanks to visit codestin.com
Credit goes to github.com

Skip to content

trieloff/am-i-ai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

am-i-ai - AI Coding Agent Detection Library

Part of AI Ecoverse

am-i-ai hero

A lightweight, portable shell library for detecting whether code is being executed by an AI coding agent. Used by ai-aligned-git and ai-aligned-gh to provide proper AI attribution.

Why?

AI coding agents like Claude Code, Gemini, Cursor, and others are increasingly common in development workflows. Detecting when code is running under AI control enables:

  • Proper attribution: Ensure AI-generated commits are attributed correctly
  • Safety guardrails: Prevent dangerous operations when AI is in control
  • Transparency: Track AI involvement in your codebase
  • Different behaviors: Apply different policies for human vs AI execution

Quick Start

As a Script

# Download
curl -fsSL https://raw.githubusercontent.com/trieloff/am-i-ai/main/am-i-ai.sh -o am-i-ai.sh
chmod +x am-i-ai.sh

# Detect current AI
./am-i-ai.sh
# Output: "claude" (or "none" if not AI)

# Check if AI (exit code 0 = AI, 1 = not AI)
if ./am-i-ai.sh --check; then
    echo "Running under AI control"
fi

# Debug mode to see detection logic
./am-i-ai.sh --debug

As a Library

# Source the library
source /path/to/am-i-ai.sh

# Simple check
if ami_is_ai; then
    echo "AI detected: $(ami_detect)"
fi

# Get AI tool information
AI_TOOL=$(ami_detect)
if [ "$AI_TOOL" != "none" ]; then
    echo "Name: $(ami_get_name $AI_TOOL)"
    echo "Email: $(ami_get_email $AI_TOOL)"
fi

# See all detected AIs (when multiple tools are nested)
ami_detect_all

Supported AI Tools

Tool Detection Method Environment Variable
Claude Code Process + env CLAUDECODE, CLAUDE_CODE_ENTRYPOINT
Gemini Process + env GEMINI_CLI
Codex CLI Process + env CODEX_CLI, CODEX_SANDBOX
Cursor Process + env CURSOR_AI
Aider Process + env OR_APP_NAME=Aider
Amp Process + env AGENT=amp, AMP_HOME
Qwen Code Process + env QWEN_CODE
Droid Process + env DROID_CLI
OpenCode Process + env OPENCODE_AI
Zed AI Process + env ZED_ENVIRONMENT, ZED_TERM
GitHub Copilot Process + env GITHUB_COPILOT_CLI_MODE
Kimi CLI Process + env KIMI_CLI
OpenHands Process + env OR_APP_NAME=OpenHands, OR_SITE_URL
Crush Process only (detected via process tree)
Goose Process + env GOOSE_TERMINAL
Cline Process + env CLINE_TASK_ID
Roo Code Process + env ROO_CODE_TASK_ID
Windsurf Process + env WINDSURF_SESSION

Detection Methods

Two-Phase Detection

am-i-ai uses a robust two-phase detection approach:

  1. Phase 1: Environment Variables - Fast check for AI-specific environment variables
  2. Phase 2: Process Tree - Walk up the process tree looking for AI tool signatures

This ensures reliable detection even when AI tools don't set environment variables.

Zed AI Special Handling

Zed presents a unique challenge because it sets environment variables for all terminals, not just AI-controlled ones. am-i-ai distinguishes between:

  • Human via git panel: ZED_ENVIRONMENT set, SHLVL=1, no terminal vars
  • Human via terminal: Parent process is an interactive shell (bash, zsh, etc.)
  • Zed AI agent: ZED_ENVIRONMENT set, SHLVL>1, parent is NOT an interactive shell

Priority Order

When multiple AI tools are detected (e.g., Claude running inside Cursor), the more specific tool takes precedence:

Amp > Codex > Aider > Claude > Gemini > Qwen > Droid > OpenCode >
Cursor > Copilot > Kimi > OpenHands > Cline > Roo > Windsurf >
Crush > Goose > Zed

API Reference

Functions

Function Description Returns
ami_detect Detect highest-priority AI tool Tool name or "none"
ami_is_ai Check if any AI is detected Exit code 0/1
ami_detect_all Get all detected AI tools Space-separated list
ami_get_name Get display name for tool Human-readable name
ami_get_email Get email for tool Email address
ami_check_env Run environment detection only Space-separated list
ami_check_ps_tree Run process tree detection only Space-separated list
ami_process_contains Check if process matches pattern Exit code 0/1
ami_version Print version info Version string

Environment Variables

Variable Description Default
AMI_DEBUG Enable debug output false

Installation

Manual Download

curl -fsSL https://raw.githubusercontent.com/trieloff/am-i-ai/main/am-i-ai.sh \
    -o ~/.local/lib/am-i-ai.sh

With install script

curl -fsSL https://raw.githubusercontent.com/trieloff/am-i-ai/main/install.sh | sh

For Projects

Add as a git submodule or copy the file directly:

# Submodule
git submodule add https://github.com/trieloff/am-i-ai.git vendor/am-i-ai

# Or direct download
curl -fsSL https://raw.githubusercontent.com/trieloff/am-i-ai/main/am-i-ai.sh \
    -o lib/am-i-ai.sh

Testing

# Test environment variable detection
CLAUDECODE=1 ./am-i-ai.sh
# Output: claude

# Test with debug output
AMI_DEBUG=true GEMINI_CLI=1 ./am-i-ai.sh
# Shows detailed detection process

# Test all detection
CLAUDECODE=1 CURSOR_AI=1 ./am-i-ai.sh --all
# Output: claude cursor

Adding New AI Tools

To add detection for a new AI tool:

  1. Identify the environment variables and/or process names
  2. Add detection in ami_check_env() for environment variables
  3. Add detection in ami_check_ps_tree() for process tree
  4. Add the tool to priority order in ami_detect()
  5. Add name and email in ami_get_name() and ami_get_email()
  6. Submit a pull request

Related Projects

Part of the AI Ecoverse - tools for AI-assisted development:

License

Apache 2.0 - See LICENSE for details.

About

small ai agent detection library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages