npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

ai-agent-detector

v1.0.1

Published

A TypeScript library that recognizes visits from AI agents and identifies which AI client visited

Readme

AI Agent Detector Library - Documentation

Overview

The AI Agent Detector is a TypeScript library that recognizes visits from AI agents and identifies which AI client visited your website. It's designed to be a Google Analytics-like tool specifically for tracking AI agent traffic.

Features

  • AI Agent Detection: Identifies when a website visitor is an AI agent

  • Client Identification: Determines which specific AI client is visiting (ChatGPT, Perplexity, Meta AI, etc.)

  • Multi-factor Detection: Uses multiple techniques for more accurate detection

  • Confidence Scoring: Provides confidence levels for detection results

  • Analytics: Tracks AI agent visits, pages visited, and more

  • Extensible: Easy to add new detection techniques and AI agent types

Installation

npm install ai-agent-detector

Basic Usage

import { AIAgentDetector } from 'ai-agent-detector';

// Create detector instance
const detector = new AIAgentDetector();

// Express middleware example
app.use((req, res, next) => {
  const result = detector.detect(req);
  
  if (result.isAIAgent) {
    // Add AI agent info to request
    req.aiAgent = result;
    
    // Log AI agent visit
    console.log(`AI agent detected: ${result.client?.name || 'Unknown'}`);
  }
  
  next();
});

Advanced Configuration

import { 
  AIAgentDetector, 
  DetectionTechniqueType, 
  AIClientType 
} from 'ai-agent-detector';

// Create detector with custom configuration
const detector = new AIAgentDetector({
  confidenceThreshold: 75,
  enabledTechniques: [
    DetectionTechniqueType.USER_AGENT,
    DetectionTechniqueType.IP_RANGE,
    DetectionTechniqueType.BEHAVIORAL
  ],
  enabledAgents: [
    AIClientType.CHATGPT,
    AIClientType.PERPLEXITY,
    AIClientType.GOOGLE_AI
  ],
  collectAnalytics: true
});

// Register custom detection technique
detector.registerTechnique({
  id: 'custom-header-check',
  type: DetectionTechniqueType.CUSTOM,
  weight: 40,
  execute: (request) => {
    const hasSpecificHeader = !!request.headers['x-specific-header'];
    return {
      techniqueId: 'custom-header-check',
      techniqueType: DetectionTechniqueType.CUSTOM,
      detected: hasSpecificHeader,
      confidence: hasSpecificHeader ? 40 : 0
    };
  }
});

Detection Techniques

The library uses multiple techniques to detect AI agents:

  1. User-Agent Analysis: Identifies AI agents by their user-agent strings

  2. IP Range Verification: Checks if requests come from known AI provider IP ranges

  3. Behavioral Analysis: Detects AI agent behavior patterns (JavaScript execution, header patterns, etc.)

Supported AI Clients

  • ChatGPT (OpenAI)

  • Perplexity

  • Meta AI

  • Google AI (including Gemini)

  • Claude (Anthropic)

  • Common Crawl

Analytics

// Get analytics data
const analytics = detector.getAnalytics();

console.log(`Total AI Agent Visits: ${analytics.totalVisits}`);
console.log('Visits by Client:', analytics.visitsByClient);
console.log('Visits by Page:', analytics.visitsByPage);

API Reference

AIAgentDetector

The main class for detecting AI agents.

Methods

  • detect(request): Analyzes a request to determine if it's from an AI agent

  • configure(options): Updates detector configuration

  • registerTechnique(technique): Adds a custom detection technique

  • registerAgent(agent): Adds a custom AI agent definition

  • startTracking(): Begins collecting analytics data

  • stopTracking(): Stops collecting analytics data

  • getAnalytics(): Returns collected analytics data

Configuration Options

  • confidenceThreshold: Minimum confidence level to consider as an AI agent (0-100)

  • enabledTechniques: Array of detection techniques to use

  • enabledAgents: Array of AI client types to detect

  • collectAnalytics: Whether to collect analytics data

  • analyticsStorage: Storage options for analytics data

  • customRules: Custom detection rules

Best Practices

  1. Use Multiple Detection Techniques: Relying on user-agent alone is not reliable

  2. Adjust Confidence Threshold: Set based on your tolerance for false positives/negatives

  3. Regular Updates: Keep the library updated as AI agent patterns evolve

  4. Custom Techniques: Add your own detection techniques for your specific use case

License

MIT