Delegated Identity (API Integration)
Governing per-employee AI usage from a product that holds one API key.
This guide is for teams integrating their own product with AgentWatch: a SaaS platform, an internal portal, or an agent/workflow runtime that calls upstream LLMs on behalf of many end users.
1. The problem
Section titled “1. The problem”Your product registers employees (CSV, Okta/SCIM, or SSO) and then runs work for them — a cron job, an agent, a chain of sub-agents — that calls an LLM. You want AgentWatch to show which employee that spend, that prompt, and that DLP verdict belonged to.
There are two ways to tell AgentWatch who the user is, and they suit different architectures:
| Per-employee key | Delegated identity | |
|---|---|---|
| What your backend stores | one ak_ per employee |
one service key |
| Per-request header needed | none | yes — the identity header |
| Works with any LLM client | yes (identity rides in Authorization) |
needs a client that can set a header |
| Employee shown in Usage Insights | ✅ | ✅ |
| Per-employee budget / rate limit / DLP policy | ✅ | ✅ |
You cannot have both “one secret” and “no header” — anything that claimed otherwise would be guessing at who the user was. Pick the row that matches how much secret material you are willing to hold.
A middle path many teams prefer: one service key per workload or per customer-tenant (dozens, not thousands) plus the identity header for the human. Small enough to manage, and it still puts a hard boundary between your customers’ organizations.
2. Your employees never need to receive a key
Section titled “2. Your employees never need to receive a key”This is the part that surprises most integrators: AgentWatch mints the employee’s gateway key server-side and never puts it on the wire.
- SCIM 2.0 —
POST /scim/v2/<org-id>/Userscreates the user, provisions their tenant, and mints theirak_key. The SCIM response is a standard SCIM user object: no key in the payload. - CSV import —
POST /admin/tenants/import-csvdoes the same for a bulk onboarding; the response carries only{line, id, email, status}. - SSO — a first login provisions the user. With
autoCreateUsers=falsethe user landsssoStatus='pending'and no key is minted until a tenant admin approves them (POST /api/tenant/sso/approve-user).
So you can provision thousands of governable identities without your product
ever receiving, transporting, or storing a per-employee secret. Those identities
are what makes X-Zscaler-User: john.doe@acme.com mean something.
3. Marking a key as a service key
Section titled “3. Marking a key as a service key”Every API key is a personal key by default — it is one employee, and it cannot act as anyone else. Nothing about an existing integration changes until an admin deliberately reclassifies a key.
In the admin UI (recommended)
Section titled “In the admin UI (recommended)”Sign in as a tenant admin → Employee Management:
- Add Service Account Key creates a non-human account whose key is a service key straight away. Copy the key from the dialog shown when it is created and hand it to your backend. The full key is shown only once; afterwards the API Key column shows it masked and nobody, admins included, can read it back. If it is lost, Rotate the key on its row (the old one stops working at once) and hand over the new one.
- The Key Type column on every row switches an existing key between Personal, Service — collect all, Service — require identity, and Service — Auto Create Users (§4a). The change takes effect on the next request.
Or over the API
Section titled “Or over the API”Get a tenant-admin JWT first (valid 7 days by default — AUTH_SESSION_DAYS; revocable on logout):
curl -s -X POST https://<your-agentwatch-host>/api/auth/login \ -H 'Content-Type: application/json' \ -d '{"email":"admin@yourcompany.com","password":"…","slug":"<your-org-slug>"}'# → {"token":"eyJhbGciOi…", …} ← use as: Authorization: Bearer <token>## `slug` scopes the login to your organization. It is the same token the admin# UI uses, so anything you can do in Employee Management you can do over the API.Then:
PUT /admin/keys/identity-policyAuthorization: Bearer <tenant-admin JWT>Content-Type: application/json
{ "tenantId": "<the key owner's tenant id>", "keyType": "service", "attributionMode": "attribute-to-key", "label": "acme-prod backend"}| Field | Values | Meaning |
|---|---|---|
keyType |
personal (default) · service |
Is this key one human, or does it act for humans? |
attributionMode |
attribute-to-key (default) · require-identity |
What a service key does when a request carries no identity header |
unknownIdentity |
reject (default) · auto-create |
What a service key does when the header names somebody who does not exist yet (§4a) |
autoCreateDomains |
["acme.com", …] |
Required with auto-create. The only domains that key may create employees in |
autoCreateAnyDomain |
true · false (default) |
Drops the allowlist entirely (§4b) |
GET /admin/keys/identity-policy lists every classified key in your
organization, along with the identity header name this deployment is configured
with. A tenant admin can only classify keys inside their own org.
Tip — make the service key its own identity. Create a service-account user
(e.g. svc-acme-prod@yourcompany.com) and use its key as the backend
credential. It then has the same rotate / suspend / resume lifecycle as any
employee, and un-delegated traffic shows up as a clearly named actor rather than
inside a human’s usage.
4. Calling the gateway
Section titled “4. Calling the gateway”POST /v1/chat/completionsAuthorization: Bearer ak_<your one service key>X-Zscaler-User: john.doe@acme.com ← who this call is for
X-Agent-Run-Id: run_2026-08-28T03:00_invoice-recon ← stitches the whole runx-aw-tag-agent: invoice-reconcilerx-aw-tag-parent-agent: orchestratorx-aw-tag-step: 3X-Session-Id: <conversation id> ← optional, groups turns- The identity header name is set by the
USER_HEADERenvironment variable (defaultX-Zscaler-User).X-AgentWatch-Useris always accepted as an alias, so an API integration never has to send a vendor-branded header. X-Agent-Run-Idgroups every LLM call in one agent run — parent and sub-agents alike — under a singleagent_run_idyou can filter on. If you don’t send one, AgentWatch derives a run id from the key plus a 15-minute window so uninstrumented agents still group.x-aw-tag-<key>headers need no configuration. Up to 12 tags per request; keys ≤64 chars, values ≤128.
What happens for every combination
Section titled “What happens for every combination”| Key type | Identity header | Result |
|---|---|---|
| personal | absent | 200 — attributed to the key’s owner (unchanged behavior) |
| personal | its own email | 200 — benign echo |
| personal | someone else | 403 identity_not_permitted + audit alert |
| service | absent, attribute-to-key |
200 — attributed to the service identity. Telemetry is never dropped |
| service | absent, require-identity |
401 identity_required |
| service | unknown to this org | 401 identity_unknown — never fails open |
| service | in a different org | 403 identity_cross_org |
| service | a login with no gateway identity | 403 identity_not_provisioned — provision them first |
| service | suspended / disabled | 403 identity_suspended |
| service | provisioned in this org | 200 — attributed to that employee |
| service + Auto Create Users | unknown, domain on the allowlist | 200 — the employee is created, then attributed to (§4a) |
| service + Auto Create Users | unknown, domain NOT on the allowlist | 403 identity_auto_create_refused (reason: not-allowlisted) |
| service + Auto Create Users (any domain) | unknown, any domain | 200 — the employee is created and attributed to (§4b) |
Two guarantees worth stating plainly:
- The org comes from the key, never from the header. A delegated request is resolved strictly inside the service key’s organization, so one customer’s key can never attribute usage to another customer’s employee. The one extension is a key you have explicitly allowed to act in your own sub-tenants (§4c) — still never a sibling, never another tenant.
- Delegation moves the whole governance stack, not just the label. Rate limits, budgets, and DLP policy for a delegated request are the named employee’s, because everything downstream reads the same resolved tenant.
4a. Auto Create Users — governing people you haven’t enrolled yet
Section titled “4a. Auto Create Users — governing people you haven’t enrolled yet”By default a service key can only act for employees that already exist: an
address it has never seen is 401 identity_unknown, because attributing usage
to somebody with no record would produce rows nothing downstream can govern.
That is the right default, and the wrong one for an integrator whose employee
list lives in their product. Auto Create Users closes that gap: turn it on
for a service key, name the email domains it owns, and the first time the
identity header carries a new address on one of those domains, AgentWatch
creates that employee — a real user record with its own ak_ gateway identity,
budgets, rate limits, team and policy — and serves the request attributed to
them. No pre-enrolment step, no second secret.
They join the service account’s own team, so their spend rolls up exactly where that integration’s spend already does and they appear in every team filter from the first request. If the service account has no team, they join your organization’s default team (the team named “Default Team”, otherwise your oldest team, otherwise a “Default Team” created for you). Move anyone afterwards like any other employee — a person who already belongs to a team keeps it.
PUT /admin/keys/identity-policyAuthorization: Bearer <tenant-admin JWT>
{ "tenantId": "<the service key's tenant id>", "keyType": "service", "attributionMode": "attribute-to-key", "unknownIdentity": "auto-create", "autoCreateDomains": ["acme.com", "acme.co.uk"], "label": "acme-prod backend"}What it will and will not create. The identity header is a plain string on a credential your whole backend shares, so the feature is deliberately bounded:
| Bound | Behavior |
|---|---|
| Off by default | A key that has not opted in behaves exactly as before (401 identity_unknown) |
| Domain allowlist | Only addresses on a listed domain (or its subdomains). An empty list is rejected by the API with AUTO_CREATE_REQUIRES_DOMAINS. ["*"] is not a wildcard entry: it switches the key to any-domain mode (§4b) |
| Service keys only | A personal key can never reach this path |
| The org comes from the key | A created employee always lands in the service key’s organization. The header carries no org and cannot imply one |
| Plan seat limit | Enforced exactly as in Add employee — the gateway is not a side door around maxUsers |
| Rate cap | 50 new employees per organization per hour (AW_AUTO_CREATE_MAX_PER_HOUR); over that, 429 with reason: rate-limited |
| Never for administrators | An address that resolves to an admin or super-admin login is refused (reason: privileged) |
| Never for pending SSO users | Somebody an admin deliberately left awaiting approval stays pending (reason: sso-pending) |
| No login is created | The account has no usable password. It is a governed gateway identity; reset the password if that person should also get console access |
Every creation is written to Settings → Audit Logs as
security.identity_auto_created, naming the address, the service key that
caused it, and the client IP. Refusals appear in the identity-alert feed (§6)
like every other rejected assertion. In Usage Insights, an auto-created
employee’s first request carries the tag identity_auto_created, so you can
always ask “which of these people did the gateway create for us, and when”.
When NOT to use it. If your employee list is already in an IdP, SCIM or a CSV import gives you joiner/leaver handling, roles and team assignment that a gateway header cannot. Auto Create Users is for the case where your product is the system of record and pre-enrolment is the thing you are trying to avoid.
4b. Auto Create Users — any domain (unrestricted)
Section titled “4b. Auto Create Users — any domain (unrestricted)”Your end users will not all share one domain. Contractors, consultants, agency staff, BYOD and personal addresses all legitimately join a tenant’s traffic, and they cannot be enumerated up front. So a tenant admin can select Service — Auto Create Users (any domain) in the Key Type column for a key in their own organization, or send:
PUT /admin/keys/identity-policy{ "tenantId": "<the service key's tenant id>", "keyType": "service", "attributionMode": "attribute-to-key", "unknownIdentity": "auto-create", "autoCreateAnyDomain": true}"autoCreateDomains": ["*"] is accepted as shorthand for the same thing. The row
is then marked ANY DOMAIN in the console so nobody has to read a policy blob
to discover it.
This needs no super-admin. It is the same authority you already hold over your own employee list: an SSO provider’s Auto-create users with no domain restriction provisions anyone who authenticates through your IdP, and you can add any-domain employees by hand or by CSV import. Setting a key in another organization still requires a super-admin, as it does for every other field on that endpoint.
This removes one bound — the domain allowlist. Everything else in the table above still applies, unchanged: the address must still be a valid email, the employee still lands in the service key’s organization, the plan seat limit and the 50/hour cap are still enforced, administrators and pending-SSO logins are still never auto-created, and every creation is still audited.
⚠ Understand what you are turning on: every address your backend puts in the
identity header becomes an employee, whatever its domain — including a typo or
an address that is not yours. Each one consumes a seat against maxUsers, and
the hourly cap means a runaway integration is throttled rather than stopped.
Prefer §4a’s allowlist whenever you can list the domains; use this when you
genuinely cannot.
4c. Sub-tenant routing — one key, each sub-tenant’s own policy
Section titled “4c. Sub-tenant routing — one key, each sub-tenant’s own policy”If your workspace has sub-tenants, each one has its own DLP, guardrails, budgets and rate limits — but until now they applied only to that sub-tenant’s own keys. A client already using your workspace’s service key could not be governed by a sub-tenant’s rules without being re-keyed.
Sub-tenant routing lets a service key act inside one of your sub-tenants. The employee named in the identity header is then found in that sub-tenant, and the request is governed and recorded exactly as if that employee had used their own key there: the sub-tenant’s DLP, guardrails, budgets and rate limits apply, and the row appears in the sub-tenant’s Usage Insights. Your workspace authorises this; the sub-tenant does not have to.
Before you start: the sub-tenant must exist (create it under Sub-tenants in the sidebar, which appears when your plan includes the Sub-tenants module), and the employees your clients name must exist in the sub-tenant, or the key must have Auto Create Users on so they are created there on first sight. There is no fallback to your own workspace’s employees.
Turn it on (one key at a time): Settings → Employee Management, on the
service key’s row click Sub-tenants: off (shown on every service key), tick
Enable sub-tenant routing for this key, and optionally add rules, one per
line: header: value -> sub-tenant, or header -> sub-tenant to route
whenever that header is present, whatever its value. Or over the API:
curl -X PUT https://<your-agentwatch-host>/admin/keys/identity-policy \ -H "Authorization: Bearer <workspace-admin JWT>" \ -H "Content-Type: application/json" \ -d '{ "tenantId": "<service key owner tenantId>", "subTenantDelegation": { "enabled": true, "rules": [ { "header": "x-agent-name", "value": "claims-bot", "subTenant": "claims" }, { "header": "x-agent-name", "value": "billing-bot", "subTenant": "billing" }, { "header": "x-claims-agent", "value": "*", "subTenant": "claims" }, { "header": "body.agent_id", "value": "5e8f1f9a-426c-4624-b71a-a09c70df3906", "subTenant": "claims" }, { "header": "query.tenant_id", "value": "acme", "subTenant": "billing" } ] } }'Rules on the request payload (2.0.401). A rule key can also be body.<field>,
which reads a field of the JSON request body and may be nested up to four levels
(body.metadata.agent_id), or query.<parameter>, which reads a query-string
parameter. Field and parameter names are case-sensitive, but values still match
case-insensitively. Only a string, number or boolean value matches. Conversation
fields (messages, input, prompt, system, instructions, contents,
tools) are refused with SUB_TENANT_RULE_RESERVED_FIELD. A rule matches what
reaches AgentWatch, so the field has to be in your backend’s call to /v1/.... It
is not enough for the field to appear in your own application’s browser traffic.
Prefer body.metadata.<field> or a header to a new top-level body field: the body
is forwarded to the provider as sent, and some providers reject unknown top-level
fields.
subTenant takes a sub-tenant’s slug or id; it is checked when you save and
stored by id, so renaming a sub-tenant does not break a rule. Sending
"subTenantDelegation": null turns it off; "enabled": false pauses it and
keeps the rules.
Choosing the sub-tenant on a request — either way works:
# 1. The client names itcurl https://<your-agentwatch-host>/v1/chat/completions \ -H "Authorization: Bearer <service key>" \ -H "X-Zscaler-User: jane@acme.com" \ -H "X-AgentWatch-Sub-Tenant: claims" \ -H "Content-Type: application/json" \ -d '{"model":"gpt-4o","messages":[{"role":"user","content":"hello"}]}'
# 2. A rule chooses it from a header the client already sendscurl https://<your-agentwatch-host>/v1/chat/completions \ -H "Authorization: Bearer <service key>" \ -H "X-Zscaler-User: jane@acme.com" \ -H "X-Agent-Name: claims-bot" \ -H "Content-Type: application/json" \ -d '{"model":"gpt-4o","messages":[{"role":"user","content":"hello"}]}'A rule matches when the request header equals its value (case-insensitive). A
rule whose value is * matches whenever that header is present, whatever it
carries, for clients whose agents differ by which header they send (for
example x-generate-agent1) rather than by its value. A rule with a value
beats a * rule only when it is listed first. Rule order is priority: header,
query and body rules form one list, checked top to bottom, and the first rule that
matches decides. In the console, Alt+↑ / Alt+↓ moves a line. Over the API, the
array order is the priority. A rule that an earlier present-only rule on the same
key would always pre-empt is refused at save with SUB_TENANT_RULE_SHADOWED.
Before 2.0.401, a rule with a value beat a * rule wherever each sat, so check
the order of rules saved on 2.0.400. The explicit X-AgentWatch-Sub-Tenant
header outranks every rule: when it names a different sub-tenant than a matching
rule, the header wins and the row is tagged identity_sub_tenant_overrode
(e.g. rule:x-agent-name). A header that cannot be used is refused on its own
terms and never falls back to the rule. Before 2.0.402 that disagreement was
refused with 403 sub_tenant_conflict. A request with
neither the header nor a matching rule is resolved in your own workspace,
exactly as before. Confirm which headers and values your clients actually send
before writing rules.
The bounds:
- Only a service key, and only one you have enabled.
- Only a direct sub-tenant of the key’s workspace — never a sibling’s or another tenant’s. A name that is not yours is refused the same way whether or not it exists, so the header cannot be used to discover workspaces.
- Not an archived or suspended sub-tenant, and only while your plan includes Sub-tenants.
- The request must name the employee. With no identity header there is
nobody to place in the sub-tenant, so it is refused (
400) rather than attributed to the service key under your workspace’s policy. - If the header and a matching rule name different sub-tenants, the explicit
X-AgentWatch-Sub-Tenantheader wins and the row is taggedidentity_sub_tenant_overrode. - Never a silent fallback. A request that names a sub-tenant and cannot be placed there is refused. Serving it under your workspace’s policy would apply DLP the client did not ask for.
- The employee must exist in the sub-tenant. An employee who exists only in
your workspace is not found there (
401 identity_unknown). If the key has Auto Create Users on, the employee is created in the sub-tenant using the key’s domain allowlist; the new account counts against the sub-tenant’s seats and hourly cap and joins the sub-tenant’s default team.
Usage rows carry two extra tags: identity_sub_tenant (the sub-tenant’s slug)
and identity_sub_tenant_via (header, or rule:<header>). Every refusal is a
durable security.sub_tenant_refused entry in your workspace’s audit log,
naming the sub-tenant that was asked for.
Trust model
Section titled “Trust model”Your product asserts who the user is; AgentWatch trusts that assertion because your backend is the authenticated party holding the service key. This is the same trust model as a Zscaler or Netskope proxy stamping the corporate user on outbound traffic — which is why the header is named the way it is. Keep the service key server-side; never ship it to a browser or a desktop client.
5. What you see in Usage Insights
Section titled “5. What you see in Usage Insights”- Employee column — the delegated employee, exactly as if they had used their own key.
- Audit trail — filter by header/tag:
agent_run_idfor one workflow run,identity_mode(delegated/service) to separate machine-driven traffic from human traffic,identity_viafor “everything my backend sent”. - Usage by Tag —
GET /admin/analytics/usage-by-tag?tagKey=agent_run_idaggregates cost and tokens for a whole agent run. - DLP violations — attributed to the employee whose prompt triggered them.
Multi-call agent runs are grouped as a filterable set today. A visual parent→child trace tree is not built yet; Observe’s “distributed trace” view is a per-request waterfall (gateway → DLP → routing → provider).
6. Security monitoring
Section titled “6. Security monitoring”Every rejected identity assertion is recorded:
GET /admin/alerts/identity— the recent-rejections feed for your org (impersonation attempts, unknown identities, cross-org attempts, suspended users, missing-required identity).- Settings → Audit Logs — a durable
security.*entry for every rejection, and for every change to a key’s identity policy. The durable write always happens; only the dashboard feed and webhooks are throttled, so a misbehaving client can’t flood your alert list. - Webhook —
security.suspicious_activityfires with the key, the asserted identity, the client IP, and the path.
An impersonation attempt is a real signal: it means a credential that is supposed to represent one person tried to speak for another. Alert on it.
7. Configuration reference
Section titled “7. Configuration reference”| Setting | Where | Default |
|---|---|---|
| Identity header name | USER_HEADER env |
X-Zscaler-User (X-AgentWatch-User always accepted) |
| Key classification | PUT /admin/keys/identity-policy |
personal |
| Missing-identity behavior | attributionMode |
attribute-to-key |
| Unknown-identity behavior | unknownIdentity |
reject |
| Domains Auto Create Users may use | autoCreateDomains |
(empty — creates nothing) |
| Any-domain (unrestricted) mode | autoCreateAnyDomain (tenant admin, own org) |
false |
| Sub-tenant routing | subTenantDelegation (enabled, rules) |
off |
| Header that names a sub-tenant | — | X-AgentWatch-Sub-Tenant |
| Sub-tenant lookup cache | AW_SUB_TENANT_LOOKUP_TTL_MS env |
30000 (30 s) |
| Auto Create Users cap | AW_AUTO_CREATE_MAX_PER_HOUR env |
50 per org per hour |
| Alert de-duplication window | AW_IDENTITY_ALERT_THROTTLE_MS env |
300000 (5 min) |
Policy is stored in system_settings — no SQL schema change, so adopting
this is a plain image upgrade.
8. Troubleshooting
Section titled “8. Troubleshooting”| Symptom | Cause | Fix |
|---|---|---|
403 identity_not_permitted |
the key is still personal |
classify it as a service key |
401 identity_unknown |
the employee isn’t provisioned in the key’s org | provision via SCIM / CSV / admin API first, or switch the key to Auto Create Users (§4a) |
403 identity_auto_create_refused, reason: not-allowlisted |
Auto Create Users is on, but that address’s domain isn’t listed | add the domain to autoCreateDomains |
429 identity_auto_create_refused, reason: rate-limited |
more than 50 new employees in an hour | bulk-provision the rest with SCIM / CSV, or raise AW_AUTO_CREATE_MAX_PER_HOUR |
403 identity_auto_create_refused, reason: seat-limit |
the org is at its plan’s maxUsers |
upgrade the plan or remove unused employees |
403 identity_auto_create_refused, reason: privileged |
the address is an admin login | administrators are never auto-created; provision them explicitly |
400 AUTO_CREATE_REQUIRES_DOMAINS |
auto-create was sent with no domain list |
name the domains, or set autoCreateAnyDomain: true |
403 Access denied on the policy endpoint |
the key belongs to a different organization | only a super-admin can reclassify another org’s key |
403 identity_not_provisioned |
the user can log in but was never given a gateway identity (common for admin/SSO-only accounts) | provision them via SCIM / CSV / admin API |
401 identity_required |
key is require-identity and the header was missing |
send the header, or switch to attribute-to-key |
403 sub_tenant_not_permitted |
the key is not enabled for sub-tenant routing, or the named workspace is not a sub-tenant of the key’s workspace | enable it on the key (§4c); check the slug |
403 sub_tenant_inactive |
the sub-tenant is archived or suspended | reactivate it, or route elsewhere |
403 sub_tenant_feature_off |
your plan no longer includes Sub-tenants | ask your provider to enable it |
400 sub_tenant_requires_identity |
a sub-tenant was named without an employee in the identity header | send the identity header |
401 identity_unknown with a sub-tenant named |
the employee is not in that sub-tenant (they may exist in yours; there is no fallback). The message names the sub-tenant searched and the header or rule that chose it, and the body carries subTenant |
provision them in that sub-tenant, or enable Auto Create Users on the key |
| All usage lands on one row | header not reaching the gateway | confirm the client forwards custom headers; try X-AgentWatch-User |
| Policy change didn’t take effect | 30s policy cache | wait 30s (immediate on a single-replica deployment) |