Attribution in AI Cost Management

When AI requests run through your AI Gateway, you need to know where they came from. Attribution connects each request to an identity and context.

When attribution is in place, you can use the data in the following ways:

  • The cost management Dashboard breaks spend down by user, group, team, or tag so you can see where costs are concentrated.
  • The events Explorer lets you filter and compare any of those dimensions over time.
  • Budgets can target a specific user, group, team, or tag value, so you can track spend against a cap at whatever scope matters to your organization.

How attribution works

Attribution works in two tracks as each request passes through the gateway: identity resolution and tag capture.

  • Identity resolution: The gateway evaluates the token or credential against the auth strategy attached to the model and resolves a subject. Group and team membership follows from that subject automatically.
  • Tag capture: The gateway reads the request headers and records them as tags on the usage event. The gateway captures headers prefixed with ai-cost- without configuration: the part after the prefix becomes the tag key.

    Data planes on AI Gateway 2.3 or later capture ai-cost- headers without any setup. The attribution plugin, which reads tools’ own headers, header mappings, tags on every request, and groups from tokens, needs KONG_CUSTOM_PLUGIN_STREAMING_ENABLED=on on every data plane. KONG_CUSTOM_PLUGIN_STREAMING_ENABLED is enabled by default on AI Gateway 2.x on Dedicated Cloud Gateways. See Set up header-based attribution for details.

Subject

The subject is the primary identity attributed to a usage event.

AI Cost Management sets the subject from the following priority chain, using the first value that applies:

  1. A valid Kong Identity Principal UUID is on the request.
  2. A valid gateway Consumer or AI Consumer UUID is on the request.
  3. An openid-connect auth strategy without principals authenticated the request, and the token has a sub claim. The sub is the raw value from the token’s sub claim, between 1 and 255 characters with no line break.

When none of these applies, the event has no subject and is unattributed. Unattributed events still appear in totals, and you can still match them by group, tag, and model. In the dashboard and the events view, they show as Unattributed.

Automatic user provisioning

Automatic user provisioning creates a Kong Identity principal from the caller’s token the first time they authenticate. It depends on users and teams defined in the org chart.

To use automatic user provisioning, upload your org structure first, so that a newly provisioned principal matches the correct team and cost center.

Groups and teams

Groups and teams record the organizational context of the request’s principal.

Teams never come from request headers. Under the attribution plugin, no header sets a group either. On a data plane without the plugin, an ai-cost-group or ai-cost-groups header still sets OIDC groups, as oidc/<name>. Install the attribution plugin to stop clients from setting their own groups.

Groups

Groups appear in the event’s groups field as an array of typed references. They come from any of the following sources:

  • The request principal’s Kong Identity group memberships (as principal/UUID)
  • The request consumer’s gateway consumer groups (as consumer_group/UUID)
  • JWT group or groups claims, when the attribution plugin is installed on the gateway (as oidc/GROUP)

The attribution plugin keeps the token’s groups that match its Groups from tokens patterns (group_patterns), or every group when there are no patterns. It drops a claim whole when more than 64 groups in it match.

Each source has its own limit:

  • 200 Kong Identity groups
  • 64 Consumer groups
  • 64 OIDC groups When a source exceeds its limit, events stop getting groups from that source, and keep the groups from other sources.

Teams

Teams appear in the event’s teams field as an array of principal/UUID references. They come from Kong Identity team membership of the request’s principal only. A single event can hold a maximum of 200 teams.

Teams are created when you upload your org structure. See Org chart in AI Cost Management.

Tags and headers

Tags are key-value pairs attached to a usage event. Tags hold context the caller controls: the project, the ticket, the environment, or any other dimension useful for cost allocation.

The gateway captures tags from request headers. You can set a tag by sending a header named ai-cost- followed by the tag key. For example, to set the project tag to atlas, send the header ai-cost-project: atlas.

Tag and header rules

The ai-cost- headers follow these rules:

  • Key: Lowercase letters and digits in words joined by single hyphens, at most 128 characters, such as cost-center. A key with ., _, or a double hyphen doesn’t produce a tag.
  • Value: Between 1 and 256 characters, no control characters, case-sensitive.
  • Header names arrive in lowercase, so ai-cost-Project and ai-cost-project are the same header.
  • Each ai-cost- header must appear once per request. The gateway drops the tag when a header is duplicated, even when both values are equal.
  • The gateway drops tags with empty values.
  • With the attribution plugin, values over 256 characters are cut to 256, and a request keeps at most 64 tags, dropping the lowest-precedence tags first. See Tag precedence.
  • Without the plugin, keep values within 256 characters, because one invalid value can remove all of the event’s tags.
  • Don’t put PII in a tag, for example a name, email, or username. User identity comes from the auth strategy on the model.

Some tools send native headers that the attribution plugin maps to standard tag keys, so you don’t need ai-cost- headers for those tags. See Attribution integrations for the supported tools and the tags each one sends.

Tag precedence

When two sources give the same tag key, the attribution plugin keeps the value from the source that comes first in the following list:

  1. Tags on every request (operator_tags).
  2. Mapped headers (headers).
  3. Mapped prefixes (prefixes).
  4. ai-cost- headers.
  5. Other AI Gateways’ metadata headers, listed in Integrations.
  6. Tools’ own headers, such as Claude Code and Codex CLI headers.

Standard tag keys

The following standard tag keys define a shared vocabulary for common dimensions. A tag can use any valid key. These conventional names help tags from different tools and teams align.

Tag key

Category

Meaning

project Project context The project this request belongs to.
ticket Project context The issue or ticket this request relates to.
environment Project context The deployment environment, for example production or staging.
use-case Project context The intended use case.
feature Project context The product feature driving the request.
customer-account Project context The customer account associated with the request.
experiment Project context The experiment this request is part of.
experiment-variant Project context The variant within the experiment.
deployment Project context The deployment identifier.
agent-session Agent A session identifier for the AI agent.
agent-id Agent The agent’s unique identifier.
agent-parent Agent The identifier of the parent agent in a multi-agent setup.
agent-request-kind Agent The kind of request the agent is making.
agent-type Agent The type of agent.
agent-name Agent The agent’s display name.
agent-surface Agent The surface the agent is running on.
agent-plugin Agent The plugin or extension that sends the tool’s requests to the gateway, with its version when the tool reports one.
agent-version Agent The release of the coding tool. The gateway sets it from the User-Agent header of recognized coding tools.
scm-provider Source control The source control provider, for example github or gitlab.
scm-repository Source control The repository name.
scm-branch Source control The branch name.
scm-commit Source control The commit SHA.
scm-pull-request Source control The pull or merge request identifier.
scm-issue Source control The source control issue identifier.
ci-provider CI The CI provider, for example github_actions or gitlab_ci.
ci-pipeline CI The pipeline name or identifier.
ci-run CI The run identifier.
chat-provider Chat The chat platform, for example slack.
chat-organization Chat The chat organization or enterprise.
chat-workspace Chat The workspace within the chat platform.
chat-channel Chat The channel name.
chat-channel-id Chat The channel’s unique identifier.
chat-channel-type Chat The channel type, for example dm or public.
chat-trigger Chat The event that triggered the request, for example slash-command.
chat-command Chat The specific command or action invoked.
workflow-name Workflow The workflow or pipeline name.
workflow-run Workflow The workflow run identifier.

Custom tag keys

You can define custom keys for any dimension the standard keys don’t cover. A custom key follows the same rules as any other tag key.

To avoid conflicts with future standard keys, prefix custom keys with a namespace, for example acme-cost-center- or internal-team-.

Set up attribution

Attribution has two independent parts: the subject on each event and the tags.

Set up subject attribution

Identity attribution comes from the AI Auth Strategy on the AI Model. AI Gateway evaluates the strategy on each request and attributes it to the authenticated subject, so no further configuration is needed.

The strategy type determines which subject you get:

  • A key-auth strategy attributes the request to its AI Consumer.
  • An openid-connect strategy with principals.enabled attributes the request to a Kong Identity principal. Pair it with a trusted IdP and automatic user provisioning to create a principal on first login.
  • An openid-connect strategy without principals.enabled attributes the request to the token’s raw sub claim, with no Principal or Consumer involved. Keep credential_claim at its default, sub. The usage event doesn’t record which claim was used, and a claim such as email puts personal data in the subject.

Principal-based attribution also depends on the org chart. Upload your org structure before the first request, so a newly provisioned principal already has a team and cost center to join.

Set up header-based attribution

Header-based attribution captures ai-cost- headers from each request and records them as tags on the usage event. Data planes on AI Gateway 2.3 or later capture ai-cost- headers without any setup.

The attribution plugin adds more sources: tools’ own headers, header mappings, tags on every request, and groups from tokens. Install it on each AI Gateway where you need these sources.

Prerequisites

The following prerequisites apply to the attribution plugin:

  • Your AI Gateway data planes are running with KONG_CUSTOM_PLUGIN_STREAMING_ENABLED=on. If you try to enable attribution on an AI Gateway with data planes that are missing this flag, you will see a Conflict error.
  • You have the AI Gateways Admin role on the AI Gateway, because the plugin is written to the gateway with your credentials.

While the attribution plugin is installed, a data plane without KONG_CUSTOM_PLUGIN_STREAMING_ENABLED=on rejects the gateway’s whole configuration. Set the flag on every new data plane before connecting it to the control plane.

Set up

Set up header-based attribution from the Konnect UI or the API.

Attribution plugin schema

Plugin updates are manual. To update a gateway to the latest plugin version, select Update to in its actions menu on the Gateways tab, or send a PUT with the gateway’s stored configuration.

FAQs

A usage event has no subject when the request carried no usable identity. Check that an auth strategy is attached to the model and that the token or credential is valid for the strategy’s type.

Automatic user provisioning can create a principal the first time a caller authenticates. Check whether automatic user provisioning is enabled on the trusted IdP and the AI Gateway auth strategy, and whether your org chart already lists that user.

Groups on a usage event come from the request’s Kong Identity group memberships, gateway consumer groups, and JWT claims.

  • For OIDC attribution, check that the attribution plugin is installed on the gateway, that the token includes group or groups claims, and that the Groups from tokens patterns match the group names.
  • For principal attribution, check that the principal has the expected group memberships in Kong Identity.
  • Check that the source doesn’t exceed its group limit. If a source is over its limit, it stops adding groups to the event.

A tag could be missing from a usage event when its header doesn’t meet the tag rules. Check that the header name is exactly ai-cost- followed by a valid key, that the value is between 1 and 256 characters and contains no control characters, and that the gateway received the header.

The gateway drops tags with empty values without an error. With the attribution plugin, a value over 256 characters is cut to 256. Without the plugin, one invalid value can remove all of the event’s tags. If you sent a header more than once in the same request, the gateway also drops the tag.

You may see a Conflict error if the AI Gateway you selected has data planes that are running without KONG_CUSTOM_PLUGIN_STREAMING_ENABLED=on.

Data planes without this flag can’t load the attribution plugin, so the toggle can’t be enabled. These data planes still capture ai-cost- headers, which don’t need the plugin.

Start new data planes with KONG_CUSTOM_PLUGIN_STREAMING_ENABLED=on, then enable the toggle again. KONG_CUSTOM_PLUGIN_STREAMING_ENABLED is enabled by default on AI Gateway 2.x on Dedicated Cloud Gateways.

A zero cost means the model had no price configured when the request ran. Cost is separate from identity attribution. See Price catalog in AI Cost Management to configure a price, and the analytics and debugging page to check which events show as unpriced.

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!