Skip to main content
Agent Tracking is a lightweight middleware layer that detects when AI coding agents visit your website or documentation. When Claude Code, OpenCode, ChatGPT, or any of the other supported agents reads your docs pages, Lightsage records the visit — giving you real data on which agents are actively researching your API.

Why this matters

You can optimize your documentation for AI agents without knowing which ones are actually visiting. But with Agent Tracking, you see the real picture: Claude Code visits your quick start 40 times a day, while Codex primarily reads your API reference. That’s not a guess — it’s a signal you can act on. Understanding agent visit patterns helps you:
  • Prioritize which agents to run API Performance evaluations against
  • Identify which documentation pages agents read most (and which they skip)
  • Detect when a new agent starts using your docs after you publish or update content
  • Correlate documentation changes with changes in agent visit frequency

How it works

The middleware inspects incoming HTTP requests for agent-specific user agent strings. When it detects a known AI coding agent or answer engine, it logs the visit — including which page was accessed and a timestamp — and forwards the data to Lightsage. The middleware adds no detectable latency to normal user traffic.

Installation

Create or update your middleware.ts file at the root of your project:
middleware.ts
Install the package:
Then add your API key to .env.local:
The matcher config applies the middleware to all routes except api, _next, and admin. This ensures you’re tracking documentation and marketing page visits without adding overhead to your own API routes or Next.js internals.

The LIGHTSAGE_API_KEY

Your API key connects the middleware to your Lightsage account. Find it in your Lightsage dashboard under Settings → API Keys.
Never commit your LIGHTSAGE_API_KEY to version control. Use environment variables in your deployment platform — .env.local for local development (already excluded by .gitignore in most frameworks), and the platform’s secret management for production.

The matcher config (Next.js and Vercel)

The matcher pattern controls which routes the middleware runs on:
This regex matches every path except:
  • /api/* — your own API routes (no need to track these)
  • /_next/* — Next.js internal assets and static files
  • /admin/* — your admin panel (if one exists)
If your documentation lives on a subdirectory like /docs, you can narrow the matcher to only track those pages:

What you see in the dashboard

After installing the middleware, the Agent Analytics section of your dashboard shows: You can filter by agent, date range, and page path to drill into specific patterns.

Agents detected

Lightsage’s middleware detects all 17 tracked AI platforms:

Coding agents (10)

Claude Code, OpenAI Codex, Cursor, GitHub Copilot, Gemini CLI, OpenClaw, OpenCode, Hermes, Pi, Kilo

Answer engines (7)

ChatGPT, Google AI Overviews, Google AI Mode, Gemini Search, Perplexity, Claude, Microsoft Copilot
The middleware tracks visits from AI agent user agents, not human developer visits. Your existing analytics tools continue to capture human traffic normally. Agent Tracking adds a parallel view of your AI audience without replacing or interfering with any existing analytics setup.

Performance impact

The middleware operates asynchronously — it sends tracking data to Lightsage without blocking the request. For your human users, response times are unaffected. For AI agent visits, tracking adds no perceptible delay because agents are making HTTP requests regardless and aren’t sensitive to single-digit millisecond differences. Lightsage does not proxy your traffic. Requests go directly from the agent to your server; the middleware reads the request headers and fires a non-blocking event to Lightsage in the background.