Skip to main content

A library to detect if Python code is run inside an agent

Project description

AI Agent Detector

Tests Python License: MIT

Detect whether your Python code is running inside an AI agent (Codex, Claude, Cursor, Devin, and more).

A Python port of pushpak1300's PHP library agent-detector.

Installation

pip install ai-agent-detector

Usage

from ai_agent_detector import detect_agent

result = detect_agent()

result.is_agent       # True / False
result.name           # "codex", "claude", "cursor", … or None
result.is_known_agent # True if the agent is in the built-in list

How detection works

Detection is attempted in this order:

1. Custom agent — AI_AGENT env var

Any process can set the AI_AGENT environment variable to identify itself:

AI_AGENT=my-custom-agent python script.py

result.is_known_agent will be False for custom agents not in the built-in list.

2. Known agents — environment variables

Each known agent is detected by the presence of one or more environment variables it sets automatically:

Agent Env vars
Amp AMP_CURRENT_THREAD_ID
Antigravity ANTIGRAVITY_AGENT
Augment CLI AUGMENT_AGENT
Claude Code CLAUDECODE, CLAUDE_CODE
Codex CODEX_SANDBOX, CODEX_THREAD_ID
Copilot COPILOT_CLI
Cursor CURSOR_AGENT
Gemini CLI GEMINI_CLI
Opencode OPENCODE_CLIENT, OPENCODE
Replit REPL_ID

The variable just needs to be present — its value is not checked.

3. Devin — filesystem marker

Devin does not set a dedicated env var. Instead, its presence is detected by checking for the file /opt/.devin.

Development

uv sync
uv run mypy src
uv run pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ai_agent_detector-0.2.0.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ai_agent_detector-0.2.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file ai_agent_detector-0.2.0.tar.gz.

File metadata

  • Download URL: ai_agent_detector-0.2.0.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for ai_agent_detector-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4c962dd1622ba1cf42208d4c47b8bb98b1ab9759b905cd0c8b3f6c211fb17128
MD5 be7ff042ccbbacc91c48788644c880ad
BLAKE2b-256 163db241352dab2cead35c94293e1635cf0404dc9330a1dfa2dd1b295f2d1422

See more details on using hashes here.

File details

Details for the file ai_agent_detector-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ai_agent_detector-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f1aa5e2171d5971f6c7843891f8e2c6cc79be99d90990e7afcc89ea28270277
MD5 78d74db46611f08e40ec750012aa8a8b
BLAKE2b-256 ef9d8aae95b2c30224f9242fd2111d4b8cbe5071e36222186b6f45506b1fa985

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page