User agent analytics
Every request that reaches the gateway carries a User-Agent header that names the program that sent it, such as a coding agent, an SDK or a command line tool. The gateway records that name as a tag on the request's daily spend rollup, so you can answer questions such as "how many engineers used the coding agent this week", "which tool drives most of our tokens" and "who are the heaviest users of it" without any change to the tools themselves. The User Agent Activity panels of the Usage page read these routes.
The tags are written by default. Set disable_add_user_agent_to_request_tags: true in spanway_settings to stop recording them, as the settings reference explains. The routes need a database and no license feature, and the DAU, WAU and MAU routes count users per tag over fixed windows ending tomorrow in UTC, the last 7 days, 7 weeks and 7 months.
Scenario
An engineering manager rolled out a coding agent to three teams and wants to know whether people actually use it before buying more seats. The Usage page shows that twenty eight engineers used it on a typical day last week, that it accounts for most of the gateway's tokens, and the per-user table shows that five people generate half the traffic. The manager gets the seat decision and the names of the power users to ask for feedback, all from the gateway's own records.
Steps
-
Make sure the tools send through the gateway with a key per person, so a user can be counted. Keys shared by a whole team count as one user.
-
In the Admin UI open Usage, stay on the global view and read the Top agents panel. Click View activity on a row to switch to User Agent Activity with the filter set to that agent's tags.
-
Read DAU, WAU & MAU per Agent and switch Active users period between DAU, WAU and MAU. From the API the daily numbers for one tool look like this.
curl "http://localhost:4000/tag/dau?tag_filters=User-Agent:%20claude-cli" -H "Authorization: Bearer $SPANWAY_API_KEY" -
Switch Activity view to Per user to read Per User Usage (Last 30 Days), or call the per-user route.
curl "http://localhost:4000/tag/user-agent/per-user-analytics?tag_filters=User-Agent:%20claude-cli&page=1&page_size=50" -H "Authorization: Bearer $SPANWAY_API_KEY"The tag is
User-Agent:followed by the part of the header before the first/. Claude Code sends its model calls asclaude-cli/<version>, so its traffic is underUser-Agent: claude-cli. Only its own side fetches, such as model discovery, useclaude-code/<version>, so a filter onclaude-codemisses almost all of its usage. curl appears asUser-Agent: curl, andGET /tag/distinctshows the exact tag every other tool produces. -
For totals over a date range of your choosing, call the summary route with
start_dateandend_date, and list every tag the gateway has seen withGET /tag/distinctto find the exact tag spelling.curl "http://localhost:4000/tag/summary?start_date=2026-11-01&end_date=2026-11-21" -H "Authorization: Bearer $SPANWAY_API_KEY"
Expected result
The DAU call answers 200 with one row per tag and day for the last seven days.
{"results": [{"tag": "User-Agent: claude-cli", "active_users": 28, "date": "2026-11-20"}, {"tag": "User-Agent: claude-cli", "active_users": 31, "date": "2026-11-19"}]}
WAU and MAU rows add period_start and period_end. The summary answers with one row per tag.
{"results": [{"tag": "User-Agent: claude-cli", "unique_users": 64, "total_requests": 48210, "successful_requests": 47990, "failed_requests": 220, "total_tokens": 912000000, "total_spend": 4180.42}]}
The per-user call answers with a page of users, each with user_id, user_email, user_agent, successful_requests, failed_requests, total_tokens and spend, plus total_count, page, page_size and total_pages, the same numbers the Per User Usage (Last 30 Days) table shows.
Routes
| Method | Path | Summary |
|---|---|---|
| GET | /tag/distinct | List the user agent tags the gateway has seen |
| GET | /tag/dau | Daily active users per user agent, last 7 days |
| GET | /tag/wau | Weekly active users per user agent, last 7 weeks |
| GET | /tag/mau | Monthly active users per user agent, last 7 months |
| GET | /tag/summary | Users, requests, tokens and spend per tag in a date range |
| GET | /tag/user-agent/per-user-analytics | Per user requests, tokens and spend by user agent, last 30 days |
tag_filters may be repeated to pick several tags and takes precedence over the older single tag_filter. GET /tag/distinct returns at most 250 tags, most used first, and includes tags that are not user agents, such as plain request tags without a colon.
Who can use it
The routes are for proxy admins and admin viewers. Any other key is refused with a 401 that says only a proxy admin may use the route, unless an admin grants the routes to it through the key's allowed_routes or a custom role. The numbers are gateway wide, because a tag rollup has no owner, so grant the routes only to people who may see every team's usage. The Usage page shows Top agents and User Agent Activity to proxy admins and admin viewers only.
Errors
| Status | What it means | Fix |
|---|---|---|
| 401 | The call carried no key or an invalid one. | Send Authorization: Bearer <virtual key>. |
401 Authentication Error, Only proxy admin can be used to generate, delete, update info for new keys/users/teams. Route=/tag/dau... | The key is not a proxy admin or admin viewer, and the route is not granted to it. | Use an admin key, or add the routes to the key's allowed_routes or custom role. |
| 422 | start_date or end_date is missing on the summary route, or page_size is above 1000. | Send both dates as YYYY-MM-DD and a page size of 1000 or less. |
| 500 database not connected | The gateway has no database, so there are no rollups to read. | Set DATABASE_URL and restart the gateway. |
| 500 "Failed to fetch ... The gateway log has the cause." | The rollup query failed in the database. | Read the User agent analytics query failed line in the gateway log for the database error. |
Empty results | No request carried a User-Agent in the window, or disable_add_user_agent_to_request_tags is on. | Check a recent request on the Logs page for its tags and the setting in spanway_settings. |
Limits
The per-user route reads every daily tag row of the last 30 days for the tags you ask for, adds them up per user in the gateway and only then cuts out the page you asked for. On a gateway with many users and tools that read is large, so the answer can be slow and, on a small gateway host, can fail with the 500 above. Filter with tag_filters to the tools you care about to keep it small. The DAU, WAU, MAU and summary routes count in the database and do not have this limit.
Logs
| Level | Line | Meaning | Action |
|---|---|---|---|
| ERROR | User agent analytics query failed: ... | One of the routes failed in the database and the caller got a 500. | Read the database error after the colon. Retry with tag_filters when it is the per-user route. |
Related
- Usage page describes the Top agents and User Agent Activity panels.
- Tag management and tag routing cover request tags, which user agent tags are a kind of.
- MCP usage analytics splits active users and agents by MCP server, tool and client label.
- Agent activity reports report spend per registered A2A agent rather than per tool.
- Coding agent setup points coding agents at the gateway.
- Cost exports download the same rollups as files.