# External usage connectors

**Last updated:** 2026-11-22T07:30:00-03:00 (UTC-3)

An external usage connector reads a provider's own usage report, which includes traffic that did not pass through this gateway: a coding tool billed to an API organization, a script with a direct key, a provider console. The gateway keeps the latest report of every record, with corrections and withdrawals, and reports it next to its own traffic. Flag: `external_usage_connectors` (default `false`).

**Reporting only.** The data arrives after the requests ran, so it can never admit, refuse or reserve anything: a hard budget cannot stop traffic that bypassed the gateway. Route the tool through the gateway when it must be governed.

| Source | Report | Credential |
| --- | --- | --- |
| `anthropic_claude_code` | Claude Code usage report (Anthropic Admin API), `GET https://api.anthropic.com/v1/organizations/usage_report/claude_code`, one UTC day per call, paged | An Anthropic Admin API key, `x-api-key` with `anthropic-version: 2023-06-01` |
| `openai_usage_completions` | OpenAI organization usage, completions (Admin API), `GET https://api.openai.com/v1/organization/usage/completions`, daily buckets grouped by model and API key | An OpenAI Admin key with usage read access, `Authorization: Bearer` |

Origins are fixed, redirects are not followed and the credential comes from its environment variable at each synchronization: never stored, logged or shown. Other coding assistants (for example GitHub Copilot and Cursor usage feeds) are not implemented yet; the API lists them in `deferred_sources`.

The Claude Code report covers Claude Code used with the API organization of the key; whether individual subscription seats appear depends on the provider's plan.

## Enable it

```yaml
features:
  usage_analytics: true                # required
  external_usage_connectors: true
usage_connectors:
  - id: claude-code                    # ^[a-z][a-z0-9_-]{1,31}$
    source: anthropic_claude_code      # or openai_usage_completions
    admin_key_env: ANTHROPIC_ADMIN_KEY
    account_id: main-org               # your label for the provider organization
    provider_id: anthropic             # optional: labels possible overlap with this gateway's traffic
    finalization_hours: 48             # optional, 1-720; default 48
```

| Field | Default | Meaning |
| --- | --- | --- |
| `id`, `source`, `admin_key_env`, `account_id` | required | As above. Source plus account, and source plus credential, are unique across connectors. Keep the account label and the connector ID stable: records are keyed by the label, and finality and overlap are read through the ID. |
| `provider_id` | none | A configured provider. Days on which this gateway also served that provider are marked `possible_overlap`; the two are never added together. |
| `finalization_hours` | `48` | Days that ended less than this ago are `provisional`. |

Connectors are boot configuration; up to 16. The flag is an operator control and needs PostgreSQL (migration 148 creates `gateway_external_usage` and its runs).

## Synchronize

`POST /api/v1/admin/external-usage/connectors/{id}/sync` with `{"from": "2026-10-15", "to": "2026-11-15"}` (UTC dates, `to` exclusive, at most 31 days, ending no later than tomorrow). Global administrator, `quotas:manage`, CSRF; audited as `external_usage.sync`.

- Every page is read (cursor repeats refused, 512 KiB per page). A record repeated within one fetch refuses the synchronization.
- A record's identity is its day (or bucket), actor, tool surface, customer type or project, tier and model. A new identity is a `new_record`; the same identity with different measures replaces them and counts as a `corrected_record`; an identity the provider no longer reports for a synchronized day is set to zero and counts as a `withdrawn_record`.
- Claude Code users are stored as a pseudonym (`user_` and an unkeyed SHA-256 digest of the email scoped to the account label), never the email itself. The digest is not encryption: someone with database access and a list of candidate emails can test them. No API response returns actors. API-key actors keep the key name; records of key names that repeat on a day are summed. OpenAI rows keep the key ID, and a repeated OpenAI record refuses the synchronization.
- Subscription usage, when the report carries it, is kept apart with a `/subscription` (or other customer type) suffix on the tool.
- Sessions of a Claude Code record without a model breakdown are kept under model `[none]`.
- Two synchronizations of overlapping periods cannot overwrite each other with older data: a snapshot whose fetch began before a newer overlapping run committed answers `409 sync_superseded`.
- Rate limits: the providers' admin endpoints rate limit per organization. A `429` answers `503 connector_rate_limited` with the provider's `Retry-After`, and nothing is written; the Claude Code source makes one call (or more, when paged) per day, so retry later or sync a shorter period.
- Claude Code's `estimated_cost` is read as cents and shown as `provider_estimated_usd`; rows without an estimate show `null`, never zero. OpenAI completions usage has no cost: use [provider cost connectors](/docs/features/billing-connectors/) for charges.

`GET /api/v1/admin/external-usage/connectors` lists connectors as `usage_connectors` (the YAML names: `id`, `source`, `account_id`, `provider_id`, `finalization_hours`; credential configured or not) with their last five runs.

## Report

`GET /api/v1/admin/external-usage?from=2026-10-15&to=2026-11-15` (at most 92 days) returns one row per day, connector, tool and model: `actors`, `requests`, `sessions`, input, output, cache-read and cache-write tokens, `provider_estimated_usd`, `state` (`synced` or `provisional`), `gateway_requests_same_provider` (every gateway request recorded for that provider and day, including refused ones) and `possible_overlap`. `coverage` gives, per connector, `days_covered`, `days_final` and `days_not_synced`, so a day without rows is either covered with no usage or not synchronized. At most 5,000 rows; `truncated` says when more exist. The response states `enforcement: none`.

## Console

**Budget & TPM → Finance & reconciliation → External usage** lists connectors and runs, synchronizes the last 30 days (global administrator) and shows the report. Reading needs global `quotas:read`; team roles see nothing.

## Limits

- Reporting only, after the fact; no enforcement and no reservation.
- Copilot and Cursor are not implemented.
- **Upgrade.** Migration 148. Upgrade every replica before turning the flag on.
