Skip to main content

Detect if code is running in an AI agent or automated development environment

Project description

detect_agent

This is a Python Port of Vercels NPM package

A lightweight utility for detecting if code is being executed by an AI agent or automated development environment.

Installation

uv add detect_agent

Usage

from detect_agent import determine_agent

result = determine_agent()

if result["is_agent"]:
  print(f"Running in {result["agent"]["name"]} environment");

Supported Agents

This package can detect the following AI agents and development environments:

  • Custom agents via AI_AGENT environment variable
  • Cursor (cursor editor and cursor-cli)
  • Claude Code (Anthropic's Claude)
  • Devin (Cognition Labs)
  • Gemini CLI (Google)
  • Codex (OpenAI)
  • Antigravity (Google DeepMind)
  • GitHub Copilot (via AI_AGENT=github-copilot|github-copilot-cli, COPILOT_MODEL, COPILOT_ALLOW_ALL, or COPILOT_GITHUB_TOKEN)
  • Replit (online IDE)
  • v0 (Vercel's AI assistant, via AI_AGENT=v0)

The AI_AGENT Standard

We're promoting AI_AGENT as a universal environment variable standard for AI development tools. This allows any tool or library to easily detect when it's running in an AI-driven environment.

For AI Tool Developers

Set the AI_AGENT environment variable to identify your tool:

export AI_AGENT="your-tool-name"
# or
AI_AGENT="your-tool-name" your-command

Recommended Naming Convention

  • Use lowercase with hyphens for multi-word names
  • Include version information if needed, separated by an @ symbol
  • Examples: claude-code, cursor-cli, devin@1, custom-agent@2.0

Development

uv sync --extra dev
uv run pytest
# Lint and format check (CI)
uv run ruff check . && uv run ruff format --check .
# Fix and format
uv run ruff check . --fix && uv run ruff format .

Use Cases

Adaptive Behavior

from detect_agent import determine_agent
import os

def setup_environment():
  result = determine_agent()

  if (result["is_agent"]) {
    # Running in AI environment - adjust behavior
    os.environ.setdefault("TOGETHER_LOG", "debug")
    print(f"🤖 Detected AI agent: {result["agent"]["name"]}");

Telemetry and Analytics

from detect_agent import determine_agent

def track_usage(event: string):
  result = determine_agent();

  analytics.track(event, {
    "agent": result["agent"]["name"] if result["is_agent"] else "human",
  })

Adding New Agent Support

To add support for a new AI agent:

  1. Add detection logic to main.py
  2. Add comprehensive test cases in test.py
  3. Update this README with the new agent information
  4. Follow the existing priority order pattern

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

detect_agent-0.3.0.tar.gz (42.0 kB view details)

Uploaded Source

Built Distribution

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

detect_agent-0.3.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file detect_agent-0.3.0.tar.gz.

File metadata

  • Download URL: detect_agent-0.3.0.tar.gz
  • Upload date:
  • Size: 42.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for detect_agent-0.3.0.tar.gz
Algorithm Hash digest
SHA256 186e8a80638985574417ba752e74ca1b80341280dc6d4308b0a25b4e7bac50f3
MD5 9afff4fa6be9a377eec801f8c9c9a685
BLAKE2b-256 6b4090675e9934cac99acb4d5df050dbed9418587775ed916f12844c71e6a53f

See more details on using hashes here.

File details

Details for the file detect_agent-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: detect_agent-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for detect_agent-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 14c0ab0b90fca111eafea51d22f24fb187591e2a16eda8cbbe856a204ffdd7aa
MD5 f832800b355333a08077dde6d7e8af1b
BLAKE2b-256 1667746fe6402fbe769431d09965f7808c586dcbb0764b4694d62785a88d8736

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