detect_agent
This is a Python port of Vercel's
detect-agentpackage.
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"]:
agent = result["agent"]
print(f"Running in {agent['name']} environment")
# Adapt behavior for AI agent context
Agent Definitions
Detection rules are vendored from upstream agents.json (validated against agents.schema.json) and evaluated in array order; the first match wins.
Supported Agents
This package can detect the following AI agents and development environments:
- Cursor (Anysphere)
- Claude Code (Anthropic)
- Claude Cowork (Anthropic)
- Devin (Cognition Labs)
- Gemini CLI (Google)
- Codex (OpenAI)
- Antigravity (Google DeepMind)
- Augment
- Cline
- OpenCode
- OpenClaw
- Goose (Block)
- Junie (JetBrains)
- Kiro (AWS)
- Pi
- GitHub Copilot
- Replit
- Custom agents (via the
AI_AGENTenvironment variable)
See agents.json for the exact environment variables and conditions used to detect each one.
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
import os
from detect_agent import determine_agent
def setup_environment():
result = determine_agent()
if result["is_agent"]:
agent = result["agent"]
# Running in AI environment - adjust behavior
os.environ["LOG_LEVEL"] = "verbose"
print(f"Detected AI agent: {agent['name']}")
Telemetry and Analytics
import time
from detect_agent import determine_agent
def track_usage(event: str):
result = determine_agent()
analytics.track(
event,
{
"agent": result["agent"]["name"] if result["is_agent"] else "human",
"timestamp": time.time(),
},
)
Feature Toggles
from detect_agent import determine_agent
def should_enable_feature(feature: str) -> bool:
result = determine_agent()
# Enable experimental features for AI agents
if result["is_agent"] and feature == "experimental-ai-mode":
return True
return False
Contributing
Adding New Agent Support
To add support for a new AI agent:
- Sync detection rules from upstream
agents.jsonintodetect_agent/agents.json. Agents are evaluated in array order — the first match wins. - Sync or extend test cases in
tests/testcases.json. - Update this README with the new agent information.
Links
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file detect_agent-0.5.0.tar.gz.
File metadata
- Download URL: detect_agent-0.5.0.tar.gz
- Upload date:
- Size: 45.4 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2344df2b332acaf2f220d119bba26f6f2aeb4f3f3e317ad5718d4dd57df9c5de
|
|
| MD5 |
517e901f3829ba372325cf20f13028e2
|
|
| BLAKE2b-256 |
9bea92fea3787c033488e4aeb56d07abfecb10428b4b2ebd5c7f0376c343741b
|
File details
Details for the file detect_agent-0.5.0-py3-none-any.whl.
File metadata
- Download URL: detect_agent-0.5.0-py3-none-any.whl
- Upload date:
- Size: 11.2 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
58dd50744a4e87ea7e658442675f72b7a998cdc3d8b1e0f7130e253d0f892d65
|
|
| MD5 |
47a69a1494d766060a10c582e1ddc9f4
|
|
| BLAKE2b-256 |
21411b201289f128a2c1d247e6777eb6804bff852643ada516e092a63020dc71
|