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
- Next.js
- Vercel
- Cloudflare Workers
- Express
- Netlify
Create or update your Install the package:Then add your API key to The
middleware.ts file at the root of your project:middleware.ts
.env.local: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.The matcher config (Next.js and Vercel)
Thematcher pattern controls which routes the middleware runs on:
/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)
/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.