Detect if code is running in an AI agent or automated development environment using @vercel/detect-agent package. Adapts application behavior, telemetry, and features based on the execution context.
Detect if your code is being executed by an AI agent or automated development environment using the `@vercel/detect-agent` package. This enables adaptive behavior, improved telemetry, and conditional features based on execution context.
This skill helps you integrate AI agent detection into Node.js applications, allowing your code to:
Follow these steps to implement AI agent detection in the user's project:
Install `@vercel/detect-agent` in the project:
```bash
npm install @vercel/detect-agent
```
Create or update the relevant file where agent detection is needed. Import and use the `determineAgent` function:
```typescript
import { determineAgent } from '@vercel/detect-agent';
const { isAgent, agent } = await determineAgent();
if (isAgent) {
console.log(`🤖 Running in ${agent.name} environment`);
// Adapt behavior for AI agent context
} else {
console.log('Running in standard environment');
}
```
Based on the user's requirements, implement one or more of these patterns:
#### Adaptive Logging Behavior
```typescript
import { determineAgent } from '@vercel/detect-agent';
async function setupEnvironment() {
const { isAgent, agent } = await determineAgent();
if (isAgent) {
// Increase verbosity for AI agents
process.env.LOG_LEVEL = 'verbose';
process.env.OUTPUT_FORMAT = 'structured';
console.log(`🤖 Detected AI agent: ${agent.name}`);
} else {
process.env.LOG_LEVEL = 'info';
}
}
// Call during application initialization
await setupEnvironment();
```
#### Telemetry and Analytics
```typescript
import { determineAgent } from '@vercel/detect-agent';
async function trackUsage(event: string, data?: Record<string, any>) {
const result = await determineAgent();
await analytics.track(event, {
...data,
agent: result.isAgent ? result.agent.name : 'human',
isAutomated: result.isAgent,
timestamp: Date.now(),
});
}
// Usage
await trackUsage('feature_used', { feature: 'deployment' });
```
#### Feature Flags for AI Agents
```typescript
import { determineAgent } from '@vercel/detect-agent';
async function shouldEnableFeature(feature: string): Promise<boolean> {
const result = await determineAgent();
// Enable specific features for AI agents
const aiOnlyFeatures = [
'experimental-ai-mode',
'verbose-error-traces',
'auto-suggestions',
];
if (result.isAgent && aiOnlyFeatures.includes(feature)) {
return true;
}
// Regular feature flag logic
return checkFeatureFlag(feature);
}
// Usage
if (await shouldEnableFeature('experimental-ai-mode')) {
enableExperimentalMode();
}
```
#### Conditional CLI Output
```typescript
import { determineAgent } from '@vercel/detect-agent';
async function displayOutput(message: string, data?: any) {
const { isAgent } = await determineAgent();
if (isAgent) {
// Structured output for AI parsing
console.log(JSON.stringify({ message, data, timestamp: new Date().toISOString() }));
} else {
// Human-friendly output
console.log(message);
if (data) console.log(data);
}
}
```
If the user is building an AI tool, advise them to set the `AI_AGENT` environment variable:
```bash
export AI_AGENT="tool-name"
AI_AGENT="[email protected]" node script.js
```
**Naming convention:**
Create a test script to verify detection works:
```typescript
import { determineAgent } from '@vercel/detect-agent';
async function testDetection() {
const result = await determineAgent();
console.log('Detection Result:', {
isAgent: result.isAgent,
agentName: result.isAgent ? result.agent.name : 'none',
});
}
testDetection();
```
Run the test:
The package detects these environments:
1. **CLI Tools**: Adjust output verbosity and format when run by AI agents
2. **Build Systems**: Enable additional logging for debugging AI-driven builds
3. **Development Servers**: Provide structured responses for AI consumption
4. **Testing Frameworks**: Tag test runs with agent context for analytics
5. **Deployment Scripts**: Adapt confirmation prompts when automated
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/detect-ai-agent-environment/raw