# Agent usage

**Last updated:** 2026-11-26T04:25:35-03:00 (UTC-3)

The **Agent usage** page shows the agents that call models through the gateway: how much each is used, how fast it answers, what it costs and how often it fails, and how its versions differ. It reads the request spans the [span store](/docs/features/traces/) already keeps; there is no agent SDK to install.

## Turn it on

```yaml
features:
  span_store: true
  trace_explorer: true
  agent_sessions: true     # reads X-Gateway-Agent-Id and X-Gateway-Agent-Version
  session_tracking: true   # agent_sessions needs it
  agent_insights: true     # this page
```

| Flag | Default | Kind | Requires |
|---|---|---|---|
| `agent_insights` | `false` | tenant read | `trace_explorer` |

Readers need `logs:read`; costs need `analytics:read` with `usage_valuation`; tool names need `content_structure:read` of the team and instructions text `content_sent:read`, as everywhere in the [content ladder](/docs/features/access-control/). With `feature_plan_entitlements` the team's plan must include `agent_insights`.

## Which requests count as an agent

A request belongs to an agent when the client names it, or when the gateway recognizes the run:

```http
POST /v1/chat/completions
X-Gateway-Session-Id: run-42
X-Gateway-Agent-Id: planner
X-Gateway-Agent-Version: 2026-11-26.1
```

| Header | Bounds | Meaning |
|---|---|---|
| `X-Gateway-Agent-Id` | 1–128 printable characters; needs `agent_sessions` | The agent. A [recognized coding-agent run](/docs/features/agent-recognition/) names its agent too. |
| `X-Gateway-Agent-Version` | 1–64 characters kept; needs `agent_sessions` | Optional. The version the client gives its agent. |

Without a declared version the gateway gives each version a fingerprint (`v-` and eight hex digits) of what the request was made of: its instructions (chat system and developer messages, the Messages `system`, the Responses `instructions`), the names of the tools it offered and the model alias. A change to any of them is a new version. The request span records the version and keyed digests (HMAC, keyed from the provider encryption key, else the session signing key) of the instructions and of the tool names, never their text, so a reader without the sent view cannot test a guessed prompt against them; without either key each replica keys its own digests and fingerprints differ between replicas; a small catalogue keeps, per version, the models, the hashes, the tool names and a recent request to read the instructions from.

## The page

- **Agent usage**: requests per step of the time range for the eight most used agents, the rest as *other agents*.
- **Recent agents**: one row per team and agent, most recently seen first: last seen, versions, p95 latency of its requests, tokens, cost (`≥` when some request could not be valued, *Unknown* when none could, never zero), spans of its traces and error rate.
- **Most expensive agents** and **Highest latency agents**: the ten first by cost and by p95 latency.
- **The agent panel**: its versions with the same figures, declared or fingerprinted, the models and the tools, and links to the agent's spans (*View spans*) and sessions (*View sessions*).
- **Compare two versions**: whether the model, the tools and the instructions changed; the tools added and removed for readers of the records' structure; the two versions' instructions side by side for readers of the sent view (each read is an audited read of a captured record; a version whose requests were not captured shows *Not captured*).

## API

All routes also answer under `/api/admin`, with a console session and the reader's team scope.

| Route | Purpose | Parameters |
|---|---|---|
| `GET /api/v1/admin/agent-usage` | Agents, series and rankings | `from`, `to` (default the last 24 hours, at most 92 days), `team` (one team) |
| `GET /api/v1/admin/agent-usage/{agent}` | One agent's versions | `team` (one team's agent), `from`, `to` |
| `GET /api/v1/admin/agent-usage/{agent}/compare` | Two versions side by side | `team` (required), `a` and `b` (the two versions), `from`, `to` |

```json
{
  "agents": [
    {"team_id": "support", "agent": "planner", "requests": 312, "errors": 4, "spans": 1288, "versions": 2,
     "p95_duration_us": 2140000, "tokens": 410233, "cost_state": "known", "cost_usd": "0.412",
     "first_at": "2026-11-25T10:00:00Z", "last_at": "2026-11-26T09:58:12Z"}
  ],
  "series": [{"team_id": "support", "agent": "planner", "points": [{"at": "2026-11-26T09:00:00Z", "requests": 14, "errors": 0}]}],
  "highest_latency": [],
  "most_expensive": [],
  "valuation_shown": true,
  "step_seconds": 3600
}
```

The schemas are in the [API reference](/docs/api/).

## Limits and engines

- Figures are read from the spans of the engine in use (`spans.engine`), the catalogue from the database on both engines.
- A version's catalogue keeps at most 8 models and digests and 64 tool names (each cut to 64 bytes on a character boundary); a list holds at most 500 agents.
- p95 latency is the continuous 95th percentile of the requests' durations, as on the Usage page, on both engines.
- An agent is one team's: the same name in two teams is two rows. Agents and versions are what the client declared or the gateway derived; they are not authenticated.

## Upgrading

Migration 283 adds the version catalogue (`gateway_agent_versions`). Requests written before the flag was on have no version: their agent appears with no versions until it sends new traffic.
