Cost Attribution Tags
Cost attribution tags let an organization attribute usage and cost to its own dimensions, such as departments, business groups, subsidiaries, projects, or cost centers. Declare the tag keys, then set key=value pairs on caller API keys, teams, and members, or read the values from a caller's JWT.
Every request is attributed to the tag values it resolves when it runs. Cost Analysis breaks down spend by those values, tag budgets cap it, and rate limit policies can match and split traffic on it.
Model and provider key tags also select traffic for rate limits and budgets. Model tags can set where guardrails apply.
Tags work the same way in AISIX Enterprise and AISIX Cloud.
Prerequisites
Before starting, prepare the following:
- The owner or admin role in the organization, or a custom role with
createpermission ontag_keys, to declare tag keys. Setting tags on a resource needs permission to create or update that resource. - For the Admin API examples, the control-plane URL and an admin token with write scope:
# AISIX_CP includes /api and has no trailing slash.
# The local AISIX Enterprise quickstart uses http://localhost:8080/api.
export AISIX_CP="YOUR_AISIX_CONTROL_PLANE_API_URL"
export AISIX_TOKEN="YOUR_ADMIN_TOKEN"
export TEAM_ID="YOUR_TEAM_ID"
export ENV_ID="YOUR_ENVIRONMENT_ID"
With the connection values ready, declare the tag keys the organization will use.
Declare Tag Keys
A tag key must be declared in the organization before any resource can carry it. Declaring keys up front keeps the vocabulary consistent, so department is never also spelled dept or Department.
- Open Tag Keys in the organization navigation, then select New tag key.
- Enter the Key, such as
department. - Optionally enter a Display name and a Description.
- Select Declare key.
To declare a key through the Admin API:
curl -sS -X POST "${AISIX_CP}/tag_keys" \
-H "Authorization: Bearer ${AISIX_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"key": "department",
"display_name": "Department",
"description": "Cost center the usage is charged to."
}'
Tag keys follow these rules:
- A key starts with a lowercase letter and contains only lowercase letters, digits, and underscores, up to 63 characters:
^[a-z][a-z0-9_]{0,62}$. - A key cannot be changed after it is declared. The display name and description can be edited at any time.
- An organization can declare at most 32 keys. A request to declare one more returns
400with the codeTAG_KEY_LIMIT, and declaring a key that already exists returns409. - Members whose roles read
tag_keys, including the built-in member role, can read the declared keys. Creating, updating, and deleting them take the matchingtag_keyspermissions. - The same keys serve as access tags on environments, models, provider keys, MCP servers, and A2A agents. Roles can use them to select objects. On caller API keys and teams, tag-scoped role rules match the cost attribution tags.
Delete a Tag Key
Deleting a tag key is refused with 409 and the code TAG_KEY_IN_USE while a budget, rate limit policy, guardrail scope, or trust provider's tag_claims uses it. Remove the references named in the error message first.
Otherwise, deletion removes the key from every caller API key, team, and member that carries it, and from the access tags of environments, models, provider keys, MCP servers, and A2A agents. Later requests are no longer attributed to it, and role rules that select by the key no longer match any object.
Recorded usage keeps its original tags. Cost Analysis continues offering the key, marked Deleted, for any selected range whose usage carries it.
Set Tags on API Keys, Teams, and Members
Tags can be set on three resources. Each one carries its own tags, as an object of tag key to tag value:
| Resource | Dashboard | Admin API |
|---|---|---|
| Caller API key | The Cost-attribution tags section of the key's create and edit forms, under the environment's API keys | POST /environments/{environment_id}/api_keys and PATCH /environments/{environment_id}/api_keys/{api_key_id} |
| Team | The team's create and edit forms, under Teams | POST /teams and PATCH /teams/{team_id} |
| Member | The Tags panel on a member row in Members, and the Create user form in AISIX Enterprise | POST /members and PATCH /members/{user_id} |
The dashboard offers the declared keys and suggests values the organization already uses for the selected key. GET /tag_keys/{key}/values returns the same suggestions, up to 500 distinct values.
Configured values from caller API keys, teams, members, and resources with access tags take priority. Caller tag values found in the last 30 days of usage fill the remaining space, which is how values read from JWT claims appear. Reading values from usage requires the usage read permission.
In a very large organization, the usage lookup can time out. The response then contains only configured values, and you can still enter another value manually.
Tag values follow these rules:
- A value is free-form text of 1 to 128 characters.
- A value cannot contain control characters, such as a line break, and cannot start or end with whitespace, including non-breaking and full-width spaces. The control plane rejects such a value with
400. - Every key in a resource's
tagsmust be declared in the organization.
In the Admin API, PATCH replaces the whole tags object. Send the complete set you want to keep. Send null or {} to clear every tag, and omit tags to leave them unchanged. For example, the following request sets two tags on a team:
curl -sS -X PATCH "${AISIX_CP}/teams/${TEAM_ID}" \
-H "Authorization: Bearer ${AISIX_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"tags": {
"department": "rnd",
"subsidiary": "emea"
}
}'
A member's tags belong to their membership in this organization, not to their account. A member whose roles can update members can change the tags, while changing the member's roles takes the role_assignments permission.
Members who join through an invitation, single sign-on, or SCIM directory sync start without tags. Tags remain editable for directory-managed members because directory sync neither reads nor writes them.
To take values from the identity provider at request time, see Read Tags From JWT Claims.
How a Request Resolves Its Tags
When a request authenticates, the gateway resolves its tags from the caller API key that authenticated it and, for a JWT caller, from the verified token. For each tag key, the nearest value wins:
- For a JWT caller, the value the trust provider's
tag_claimsreads from the token. See Read Tags From JWT Claims. - Otherwise, the caller API key's own value.
- Otherwise, the value set on the team that the key is bound to.
- Otherwise, the value set on the member that owns the key.
Only the key's own team and member bindings participate. Other teams that the member belongs to contribute no tags.
A request carries one value per tag key, and values from different keys combine. One request can therefore take department from its key, business_group from its team, and subsidiary from its member. A request cannot supply its own tags; the gateway never reads them from request headers or the body.
For example, suppose the organization declares department, business_group, and subsidiary, and sets the following:
| Resource | Tags |
|---|---|
Team platform | department=infra, business_group=cloud |
| Member Alice | department=rnd, subsidiary=emea |
Key K1, bound to platform and owned by Alice | department=ml |
Key K2, bound to platform and owned by Alice | None |
Key K3, owned by Alice, with no team | None |
Requests resolve these tags:
| Key | department | business_group | subsidiary |
|---|---|---|---|
K1 | ml, from the key | cloud, from the team | emea, from the member |
K2 | infra, from the team | cloud, from the team | emea, from the member |
K3 | rnd, from the member | Not set | emea, from the member |
Read Tags From JWT Claims
For callers that authenticate with a JWT, a trust provider can supply tags from the verified token. Its tag_claims field maps tag keys to claim paths, which the gateway reads on every request.
Usage analysis, exports, tag budgets, and tag rate limits all use the result. You do not need to tag each caller API key or keep those tags synchronized with the identity provider.
The value applies to the request only. It is never written back to the caller API key, team, or member, so their pages do not show it.
The claim must be in the JWT that reaches the gateway. A field that only the identity provider's userinfo endpoint returns cannot be read, so configure the identity provider to add it to the token. For MCP clients that use interactive sign-in, the claim must be in the ID token.
Configure Tag Claims
First declare each tag key. Then configure the provider:
- In the environment, open OIDC providers and create or edit a provider.
- Under Tags from token claims (optional), select Add tag.
- Select a declared key and enter the claim path.
- Save the provider and confirm that its row shows the mapping.
To set the same mappings through the Admin API, use tag_claims when you create or update the provider:
export OIDC_PROVIDER_ID="YOUR_OIDC_PROVIDER_ID"
curl -sS -X PATCH "${AISIX_CP}/environments/${ENV_ID}/oidc_providers/${OIDC_PROVIDER_ID}" \
-H "Authorization: Bearer ${AISIX_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"tag_claims": {
"department": "department",
"subsidiary": "org.subsidiary"
}
}'
PATCH replaces the whole tag_claims object. Send null or {} to remove every mapping, and omit the field to leave them unchanged.
The control plane returns 400 INVALID_REQUEST when a key is undeclared or does not match the tag-key format. It also rejects more than 32 mappings or an empty claim path. A mapped key cannot be deleted until its mapping is removed.
How a Claim Becomes a Tag Value
The gateway reads a claim path as one top-level claim name first, so https://example.com/department works. When the token has no claim of that name, dots traverse nested objects, so org.subsidiary reads subsidiary inside org.
It then converts the claim's value:
| Claim value | Tag value |
|---|---|
"rnd" | rnd, unchanged |
1042 | 1042, the number as text |
["rnd", "ml", "rnd"] | ml,rnd, the distinct elements sorted as text and joined with commas |
["rnd"] | rnd, the element itself |
true, null, {}, or [] | Missing |
["rnd", true], an array holding anything but strings and numbers | Missing |
The result must follow the same rule as any other tag value: 1 to 128 characters, no control characters, and no whitespace at the start or end. For an array, each element and the joined result must both satisfy this rule. A value that breaks it counts as missing and is never truncated or trimmed.
An array produces one tag value, not several. A user whose groups claim is ["ml", "rnd"] carries ml,rnd, so a budget on groups=rnd does not count that user's requests. Commas inside an element are not escaped, so ["a,b"] and ["a", "b"] produce the same value.
Fall Back and Set Defaults
A tag claim is resolved per tag key and takes precedence over the caller API key, team, and member. When the claim is missing, the request falls back to the next of them for that key alone.
To keep requests without the claim visible, set a default on the caller API key, such as department=unassigned. Those requests then appear in Cost Analysis under that value, and you can set a budget on it. A request with no value for a tag key from any source is not subject to a budget or rate limit policy on that key.
When Values Change
A value changed at the identity provider takes effect when a new token is issued. Usage already recorded stays with the value it carried when it ran, so a move between departments does not re-attribute history.
A user signed in through interactive MCP sign-in keeps the claims recorded at sign-in, including across session refreshes. The user receives a new claim value only after signing in again. A change to the provider's tag_claims applies to later requests in either case.
Choose Low-Cardinality Claims
Prefer claims with few distinct values, such as a department or cost center. Each distinct value is a separate group in Cost Analysis and a separate counter in a rate limit policy that groups by the tag.
Avoid mapping a per-person value, such as an email address, when many people share one caller API key. Usage is summarized per key and tag set, so this greatly increases the volume of summary data. The value also enters usage records and exports, so consider the privacy impact.
Behavior on Shared Keys
Tags read from claims can differ between requests on one caller API key, which has two visible effects:
- Tag budgets can reject one token and allow another on the same key. The
aisix_budget_*gauges are labeled by key, so they reflect the verdict of the most recent request. - The usage of a completed batch carries the tags of the caller whose request observed the completion, not the caller who submitted the batch.
Troubleshoot Tag Claims
To see what a claim produced, group by the tag key in Cost Analysis, or expand the request's row in Request Logs to read its Tags. A request that shows a value from its key, team, or member, or that falls in Not set, did not get a value from its claim.
The reason appears only in the gateway's debug log. Allow debug lines for the aisix::auth target through log_level or RUST_LOG, for example info,aisix::auth=debug, and restart the gateway. Each dropped claim then writes one claim yields no tag value line per request, with provider, tag_key, and reason. The line never includes the value.
reason | Meaning |
|---|---|
claim_absent | The token has no claim at that path. |
value_type | The value is a boolean, null, or an object. |
empty_array | The value is an empty array. |
array_element_type | An array element is not a string or a number. |
length | A value, an array element, or the joined result is not 1 to 128 characters. |
control_character | A value, an array element, or the joined result contains a control character. |
surrounding_whitespace | A value, an array element, or the joined result starts or ends with whitespace. |
Attribution Is Fixed When the Request Runs
The gateway records the resolved tags and the key's team on every usage event of the request, including retries, failovers, and the gateway's own rejections. The control plane stores them with the event and attributes cost by what was recorded.
Tag changes apply to later requests only. Changing resource tags, claim mappings or values, or a key's team binding does not re-attribute recorded usage. Neither does deleting a tag key.
Usage recorded before a tag was set has no value for it and appears as Not set. The same applies to usage from a gateway that does not record tags.
Use Model and Provider Key Tags
Use the same declared tag keys to configure policies on a model alias or upstream credential. Open that resource's edit form, choose Add tag under Access tags, select a Tag key, and enter its Tag value. Save the resource.


Through the Admin API, update the model or provider key with its complete tags object, for example {"tags":{"department":"docs-research"}}. The model and provider key tags are independent of the effective caller tags.
- Rate Limit Policies can match model or provider key tags and give each credential its own quota bucket.
- Budgets can cap spend for a model or provider key tag value across the organization.
- Guardrail scope can apply to or exclude requests by a tag on the entry model.
These policy options require vNEXT or later. Finish rolling all control plane instances and gateways to the same version before enabling them. The upgraded control plane publishes existing access tags on models and provider keys.
If an older control plane instance saves a model or provider key after its tags have been republished, send its complete current tags object to the upgraded Admin API. Use PATCH /environments/{environment_id}/models/{model_id} for a model or PATCH /provider_keys/{provider_key_id} for a provider key. This republishes the existing tags to the gateways.
Budget by Tag
A caller tag budget caps the spend attributed to one tag value across every environment in the organization. In Budgets, choose the Tag scope and Caller source, then select the key and enter its value.
In the Admin API, set scope to tag and scope_ref to <key>=<value>, such as department=rnd. The key must be declared, and the value is everything after the first =. limit_cents uses the minor unit of the organization's currency.
curl -sS -X POST "${AISIX_CP}/budgets" \
-H "Authorization: Bearer ${AISIX_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"display_name": "R&D monthly",
"scope": "tag",
"scope_ref": "department=rnd",
"limit_cents": 50000,
"period": "month",
"hard_stop": true
}'
A caller tag budget follows the same period, hard-stop, and alert rules as other budgets. Each source and tag pair can have one budget. Spend is counted from the tags recorded on each usage event.
After a hard-stop tag budget reaches its limit, later requests with the same tag value receive HTTP 429 with code budget_exceeded. The response sets scope to tag and scope_ref to the key=value pair.
Rate-Limit by Tag
A conditional rate limit policy accepts tag:<key> as a condition dimension and in group_by. The key must be declared. For example, the following policy gives each department its own limit of 600 requests per minute:
curl -sS -X POST "${AISIX_CP}/environments/${ENV_ID}/rate_limits" \
-H "Authorization: Bearer ${AISIX_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"name": "per-department-rpm",
"group_by": ["tag:department"],
"limits": { "rpm": 600 }
}'
Two caller API keys that resolve department=rnd share one counter, and a key that resolves department=sales has its own. A request without a value for the tag does not match a condition on it, even a negated one, and is not subject to a policy that groups by it.
Read Tags in Usage Events and Request Logs
Every usage event carries tags, the request's resolved tags, and team_id, the team bound to the caller API key. Both fields are omitted when empty.
Observability exporters deliver them to external systems, while Request Logs shows them in row details and exports. See Attribute Events to a Team and Tags for each exporter's field names. Tags never become Prometheus metric labels.
The usage events API exposes model and credential tag snapshots in resource_tags. The fields are requested_model for the addressed entry, route_target_model for the selected intermediate model group, model for the model attributed to the event, and provider_key for the credential used on the attempt. Only resources identified for that event can contribute tags. Later edits affect future snapshots.
Next Steps
Use Cost Analysis to group and inspect attributed usage. Then use Budgets and Budget Alerts and Notifications to cap and monitor tag spend.