Skip to content

feat: add middleware to track AI coding agent traffic#2496

Draft
caffeinum wants to merge 1 commit intolangfuse:mainfrom
caffeinum:feat/ai-agent-tracking
Draft

feat: add middleware to track AI coding agent traffic#2496
caffeinum wants to merge 1 commit intolangfuse:mainfrom
caffeinum:feat/ai-agent-tracking

Conversation

@caffeinum
Copy link

@caffeinum caffeinum commented Feb 5, 2026

Summary

Adds Next.js middleware to detect and track AI coding agents (Claude Code, Cursor, Windsurf, etc.) visiting the docs.

How it works

AI coding agents send Accept: text/markdown header when fetching documentation - browsers never do this. The middleware detects this signal and sends anonymized events to an external analytics API.

Detection signals:

  • Accept: text/markdown (primary - browsers never send this)
  • Accept: text/plain prioritized over text/html

Detected agents:

  • Claude Code (claude, anthropic in UA)
  • Cursor (cursor in UA)
  • Windsurf (windsurf, codeium in UA)
  • OpenCode (opencode in UA)
  • Aider (aider in UA)
  • Continue (continue in UA)
  • GitHub Copilot (copilot, github in UA)
  • Unknown AI (Accept header detected, unknown UA)

Configuration

No configuration needed. The middleware sends events to an external tracking API - no env vars or secrets required in this repo.

Data collected

  • Host (docs.langfuse.com)
  • Path (/docs/tracing)
  • Agent type (claude-code, cursor, etc.)
  • Country/city (from Vercel headers)
  • Timestamp

No PII or sensitive data is collected.

Testing

# AI agent request - should show x-ai-agent header
curl -I -H "Accept: text/markdown" http://localhost:3333/docs

# Human request - no x-ai-agent header  
curl -I -H "Accept: text/html" http://localhost:3333/docs

@vercel
Copy link

vercel bot commented Feb 5, 2026

@caffeinum is attempting to deploy a commit to the langfuse Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@caffeinum caffeinum force-pushed the feat/ai-agent-tracking branch from d0c9172 to 0de00a8 Compare February 5, 2026 01:53
- sends raw headers to API for server-side detection
- uses waitUntil to not block response
- tracks all visits (AI + human) for ratio analytics
@caffeinum caffeinum force-pushed the feat/ai-agent-tracking branch from 0de00a8 to 112a102 Compare February 5, 2026 02:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants