x-portkey-metadata header as AWS STS session tags during AssumeRole. Each unique tag set produces a distinct STS session, so AWS records cost and usage per application, team, or environment — no extra IAM roles required.
This feature is available exclusively for Enterprise self-hosted gateway customers. It is not available on the Portkey managed cloud.
How It Works
- Client sends
x-portkey-metadatawith key-value pairs on each request. - Gateway parses the metadata, sanitises it (drops reserved
aws:prefixes, enforces AWS limits), and passes the pairs asTags.member.N.Key / Tags.member.N.Valuein the STSAssumeRolePOST. - AWS STS issues temporary credentials tagged with those values.
- Gateway uses those credentials to call Bedrock
InvokeModel. - Tags appear in CUR 2.0 (
iamPrincipal/prefix), CloudTrail, and Cost Explorer.
Prerequisites
Step 1: Configure IAM Permissions on AWS
Session tags requirests:TagSession in two places: the caller’s IAM policy and the target role’s trust policy. Missing either one causes an AccessDenied error. Complete this setup before enabling the feature on the gateway.
1a. Caller principal (the gateway’s execution role)
The IAM role the gateway runs under (e.g. an ECS task role, an EC2 instance profile, or a Kubernetes service account role) must be allowed to both assume and tag the target Bedrock role. Add or update the policy attached to the caller role:<TARGET_ACCOUNT_ID> and <BEDROCK_ROLE_NAME> with the actual values for the role Portkey assumes to invoke Bedrock.
Cross-account setups: When the gateway and the Bedrock role live in different AWS accounts, both the caller permissions (in the gateway account) and the target trust policy (in the Bedrock account) must allow
sts:TagSession. This is the most common source of AccessDenied errors.1b. Target role trust policy (the Bedrock invocation role)
Open the target role in IAM, go to Trust relationships → Edit trust policy, and ensure theAction includes both sts:AssumeRole and sts:TagSession:
The
Condition block is optional but recommended. If you configured an external ID when setting up the assumed role in Portkey, include it here.1c. Bedrock invocation permissions (no change needed)
The existing permission policy on the target role (bedrock:InvokeModel, bedrock:InvokeModelWithResponseStream) does not need modification. Session tags only affect the AssumeRole call, not the Bedrock API calls themselves.
Step 2: Enable Session Tags on the Gateway
Once IAM permissions are in place, enable the feature by setting the environment variable on the gateway container:Example: ECS task definition (excerpt)
Example: ECS task definition (excerpt)
Example: Kubernetes deployment (excerpt)
Example: Kubernetes deployment (excerpt)
Example: Docker Compose (excerpt)
Example: Docker Compose (excerpt)
Step 3: Send Metadata with Requests
Pass key-value pairs in thex-portkey-metadata header (or the SDK metadata option). Every key-value pair becomes an STS session tag.
Step 4: Verify Tags in CloudTrail
After sending a tagged request, confirm the tags appear in theAssumeRole CloudTrail event:
tags array in requestParameters:
tags field is absent, double-check:
- Gateway version is
≥ 2.19.0 AWS_BEDROCK_STS_SESSION_TAGS_ENABLED=trueis set- The gateway was restarted after adding the variable
Step 5: View Costs in AWS Cost Explorer
Session tags flow into AWS Cost and Usage Reports (CUR 2.0) with theiamPrincipal/ prefix. To view them in Cost Explorer:
- Open Billing and Cost Management → Cost Allocation Tags.
- Filter for User-defined cost allocation tags.
- Locate your tag keys (e.g.
app,team,env) and click Activate.
Tags take up to 24 hours to appear after the first tagged API call, and a further 24 hours to activate in Cost Explorer.
- In Cost Explorer, group or filter by Tag → select your activated tag key.
Tag Constraints and Sanitisation
The gateway automatically sanitises metadata before passing it to AWS STS. Understanding the constraints helps avoid silent tag drops.
Source: AWS STS session tag limits, AssumeRole API reference
Credential Caching
The gateway caches STS temporary credentials to minimizeAssumeRole API calls. No custom cache configuration is needed — caching works out of the box.
Cache key composition
Each cached credential is keyed by the combination of:
Parameters not in the cache key: model name, Portkey API key, virtual key slug, request body. These do not affect the STS call and are irrelevant to credential identity.
TTL and reuse
When does a new AssumeRole call happen?
A new STSAssumeRole call is made only when no cached credential exists for the computed cache key. In practice:
- Same metadata, same role — Reuses cached credentials for up to 5 minutes. One STS call per 5-minute window.
- Different metadata, same role — Each unique metadata set triggers its own STS call and produces a separate cached entry. For example, 3 distinct
teamvalues means 3 STS calls (then cached for 5 min each). - Gateway restart — Clears the in-memory cache. Redis cache (if configured) survives restarts.
- Source credential rotation — When the gateway’s own credentials change (e.g. ECS task role refresh), the
sourceCredentialHashchanges and new STS calls are made.
Estimating STS call volume
ai-platform, mobile, backend) through 1 Bedrock role in 1 region, expect ~3 STS calls every 5 minutes — regardless of request volume.
AWS STS has no hard per-second rate limit, but sustained high call rates may trigger throttling. The 5-minute cache keeps call volume well within safe bounds for typical deployments. If you have hundreds of unique metadata combinations, consider limiting the metadata keys sent as session tags to a small set of high-cardinality dimensions (e.g.
team and env, not per-request IDs).Troubleshooting
Requests fail after enabling the feature flag
Requests fail after enabling the feature flag
If Bedrock requests fail immediately after enabling
AWS_BEDROCK_STS_SESSION_TAGS_ENABLED, the most likely cause is missing sts:TagSession permissions. As a quick mitigation:- Disable the feature flag: set
AWS_BEDROCK_STS_SESSION_TAGS_ENABLEDtofalse(or remove it). - Restart the gateway.
- Complete the IAM setup in Step 1, then re-enable.
Reference
- AWS: Pass session tags in STS
- AWS: AssumeRole API — Tags parameter
- AWS: IAM principal cost attribution for Bedrock
- AWS: Using IAM principal for cost allocation
- Portkey: Connect Bedrock with Assumed Role
Portkey is now PRISMA AIRS AI Gateway. See it in action.
Contact Us

