Attributing inference calls to users when using Amazon Bedrock via AgentCore Gateway
A platform team gives its developers Claude Code, the command line coding agent from Anthropic. The company has three rules for it. Every model call must stay inside the company's AWS account. Every call must be attributed to the developer who made it. Each developer must have a limit on how much model capacity they use. The team built the setup on Amazon Bedrock and Amazon Bedrock AgentCore Gateway, and then asked which component in the path can tell one user from another. This post answers that question.
The answer depends on how the user authenticates to the gateway. I tested five setups, from a direct Amazon Bedrock call to a gateway that accepts tokens from the company's identity provider. For each setup I show what each layer in the request path sees, what the admin pushes to developer laptops, and the gaps that remain. The post closes with the three controls that keep every Claude Code call on the gateway.
The setup is the same in every test:
- Client. Claude Code on the developer laptop, configured through managed settings.
- Gateway. An AgentCore Gateway with an inference target for Amazon Bedrock. Claude Code calls the gateway inference endpoint,
https://<gateway-id>.gateway.bedrock-agentcore.<region>.amazonaws.com/inference.
- Model. Anthropic Claude models on Amazon Bedrock, called by the gateway through the Amazon Bedrock Runtime API.
- Controls on the gateway. A rate limit, a Policy in AgentCore policy engine, and an AWS Lambda request interceptor.
- Identity. AWS IAM Identity Center connected to the company's identity provider for the IAM setups; the identity provider alone for the JWT setup.
The building blocks
This section explains the parts of the setup for readers who have not used them. Readers who know AgentCore Gateway can skip to How a request flows .
Why Amazon Bedrock
Claude Code can call Claude models on Amazon Bedrock; in that configuration it uses the default AWS SDK credential chain, so AWS credentials and AWS Identity and Access Management (IAM) control who can call which model, and model use appears on the company's AWS bill. Model providers do not have access to Amazon Bedrock logs or to customer prompts and completions. You control data retention for inference requests per Region. Model invocation logging is disabled by default; when you enable it, Amazon Bedrock collects the full request data, response data, and metadata in your account, so treat that log destination as sensitive.
Why a gateway in front of Amazon Bedrock
Amazon Bedrock authorizes each call with IAM. It does not give a platform team a place to add its own rules on model traffic, such as a token budget per developer or a rule that one team may not use a given model. AgentCore Gateway is a fully managed AI gateway that provides a single, secure entry point for agentic traffic, connecting agents to tools, to other agents, and to large language models. For model traffic it exposes one HTTPS endpoint, authenticates the caller, applies rules, and forwards the request to Amazon Bedrock. Claude Code lets you override the Amazon Bedrock endpoint URL in its settings for routing through a gateway, so no proxy on the laptop is needed.
Three gateway features matter in this post:
- Rate limits . The gateway groups traffic into buckets by dimension keys and enforces a rate per bucket. Request limits support periods of one second or one minute; token limits apply to inference targets and support one minute. A dimension key can be a target, a model, a JWT claim, or the IAM caller. An entry value of
*creates independent per-entity buckets, which gives per-user limits.
- Policy in AgentCore . A policy engine attached to the gateway evaluates Cedar policies on each request. A policy can allow or deny by principal, action and request fields, for example "this user may not call this model".
- Interceptors . An AWS Lambda function that the gateway invokes before it calls the target (a request interceptor) or after the target responds (a response interceptor). The function can inspect the request, change it, or reject it. Teams use interceptors for logic the other two features do not cover, such as a monthly budget stored in a database.
For the second and third features to work per user, the component must know which user is calling. That is the question of this post.
How the gateway authenticates callers
Inbound authorization validates users who attempt to access targets through the gateway. Two types that appear in this post:
- IAM identity. The gateway authorizes through the credentials of the AWS IAM identity trying to access the gateway. The client signs each request with Signature Version 4 (SigV4), and the caller needs the
bedrock-agentcore:InvokeGatewaypermission on the gateway.
- JSON Web Token (JWT) . The client sends a bearer token from your OAuth 2.0 identity provider. The authorizer uses the configured discovery URL to fetch the public keys and validates the token. You configure allowed audiences, which the authorizer checks against the
audclaim, and can add allowed clients, allowed scopes, or required custom claims. At least one of these is required; if more than one is set, the authorizer verifies them all.
In both cases used here, the gateway then calls Amazon Bedrock with its own execution role, it is an IAM role you give the gateway when you create it. The gateway therefore has two identities: inbound, it verifies the caller; outbound, it acts as its own role.
How developers get AWS credentials
For the IAM authorizer, developers need AWS credentials. The company uses AWS IAM Identity Center, connected to its corporate identity provider. A developer signs in once, and IAM Identity Center returns temporary credentials for a permission set role. All developers assigned to the same permission set share the same role. The role session name is the user name. With attributes for access control , IAM Identity Center passes selected user attributes, such as
email , to the account as session tags; the values come from the Identity Center directory, which your identity provider can synchronize through SCIM.A source identity is a separate IAM feature. It is a text label set when a role is assumed. It cannot change for the life of the session and it stays when that session assumes another role. IAM Identity Center does not set one. Setup 4 adds it with a second role.
How a request flows
Every setup in this post passes the same six stops:
- Sign-in. The user authenticates to the identity provider, directly or through IAM Identity Center.
- Claude Code. Claude Code sends a credential with each request: a SigV4 signature or a bearer token.
- AgentCore Gateway. The gateway authenticates the request and applies rate limits.
- Policy in AgentCore. The policy engine evaluates Cedar policies.
- Lambda interceptor. The request interceptor runs.
- Amazon Bedrock. The gateway calls the model with its execution role. Amazon Bedrock writes the invocation log and AWS CloudTrail event.
In every gateway setup tested, stop 6 that is the bedrock invocation, recorded the gateway execution role, not the user.

*Figure 1: Caller identity at each layer, for the five setups. The right column shows what the admin pushes to developer laptops.
*
I ran tests between October 26 and November 2, 2026 with Claude Code 2.1.312, AgentCore Gateway inference targets for Amazon Bedrock, and IAM Identity Center with an external identity provider. The JWT tests used Amazon Cognito as the token issuer. Any OpenID Connect provider should work the same way with the gateway's JWT authorizer.
Setup 0 (Baseline): Direct to Amazon Bedrock
This is the baseline. The user signs in through IAM Identity Center and gets credentials for a permission set role. Claude Code signs each request with those credentials and calls Amazon Bedrock. The session name is the user name, so CloudTrail shows the user, and so does the model invocation log when you have enabled it.
Admin pushes: Claude Code managed settings only.
Gap: there is no gateway in the path, so there are no gateway rate limits, no Cedar policies and no interceptor.
Setup 1: IAM gateway with a custom header
The user signs in through IAM Identity Center. Claude Code signs the request with the permission set role credentials. A shell wrapper on the laptop sets the Claude Code environment variable
ANTHROPIC_CUSTOM_HEADERS to X-Amzn-Bedrock-AgentCore-Runtime-Custom-Caller: <user-name> , so Claude Code adds that header to every request. Claude Code supports fixed custom headers only, so the wrapper sets the value when Claude Code starts.What each stop sees: the gateway sees the shared role and the header. Cedar sees the role only. The interceptor sees the header value when
passRequestHeaders is true in the interceptor configuration.Admin pushes: a shell wrapper on every machine. It works for terminal launches only, not for IDE extensions or the desktop app.
Gap: the client sets the header value, so a user can send any name. Because the caller supplies the header value, a rate limit that uses a request header dimension such as
$.context.request.header.X-Amzn-Bedrock-AgentCore-Runtime-Custom-Caller must also include a trusted identity dimension, such as $.context.iam.principal , in the same rate limit.Setup 2: JWT gateway with an API key helper
The user signs in to the identity provider. IAM Identity Center is not in this path. Claude Code runs a token script, set as the
apiKeyHelper in managed settings , and sends the access token as a bearer token. Claude Code uses no AWS credentials. Claude Code caches the helper output and reruns the command after the cache lifetime.What each stop sees: the gateway verifies the token, and rate limits can use any claim, such as
email . Cedar sees the user, with the token claims as tags on the principal. The interceptor receives the bearer token and can read the claims. Amazon Bedrock sees the gateway execution role.Admin pushes: Claude Code managed settings and the token script.
The managed settings for this setup includes:
{ "apiKeyHelper": "/usr/local/bin/gateway-token", "env": { "CLAUDE_CODE_USE_BEDROCK": "1", "CLAUDE_CODE_SKIP_BEDROCK_AUTH": "1", "ANTHROPIC_BEDROCK_BASE_URL": "https://<gateway-id>.gateway.bedrock-agentcore.<region>.amazonaws.com/inference" }}Gaps: a stolen access token works until it expires, so keep the token lifetime short. Request headers reach the interceptor only when
passRequestHeaders is true; AWS warns that request headers can contain authentication tokens and credentials, so verify that the interceptor does not log them. Claude Code runs the apiKeyHelper command through the system shell with the user's permissions, so protect the script and its path like any other credential source. This is the only setup where the gateway, Cedar and the interceptor all see a verified user.Setup 3: IAM gateway with IAM Identity Center attributes
The team asked whether attributes for access control in IAM Identity Center help on an IAM gateway. I turned on the
email and userName attributes and sent requests as an IAM Identity Center user.IAM Identity Center adds the attributes to the AWS session as session tags. The tags travel inside the session token. The client sends the session token in the
X-Amz-Security-Token header.What each stop saw: the gateway rate limits resolved only the permission set role. Cedar could not resolve the user: the principal was the permission set role, with no tags. The interceptor received
X-Amz-Security-Token , but the token is opaque. It has no documented format, and no AWS API returns the tags of a session, so the Lambda function could not get the user from it. Amazon Bedrock logged the gateway execution role. CloudTrail showed the tags only in the AssumeRoleWithSAML sign-in event.Admin pushes: nothing new to laptops.
Gap: all users of one permission set look the same to the gateway, Cedar and the interceptor. The attributes still help in IAM policy conditions, as
aws:PrincipalTag/<key> . Setup 4 uses the email tag this way in a role trust policy.Setup 4: IAM gateway with a source identity
Setup 4 builds on Setup 3. It gives each session a per-user value that the gateway can read and that the user cannot change: a source identity.
- The user signs in through IAM Identity Center, as in Setup 3. The session has the
emailtag.
- The user machine uses this session to assume a second role, the gateway caller role. In the same call, it asks AWS STS to set the user's email as the source identity of the new session.
- AWS STS compares the requested email with the
emailtag on the IAM Identity Center session. If they are not equal, AWS STS refuses the call. So a user cannot set the email of a different user.
- Claude Code signs its requests to the gateway with the caller role session. Each request now carries the user's email as the source identity.
The check in step 3 is one condition in the trust policy of the caller role:
"Condition": { "StringEquals": { "sts:SourceIdentity": "${aws:PrincipalTag/email}" } }What each stop saw: the gateway rate limits resolved the
$.context.iam.sourceIdentity dimension to the user's email. A limit of zero requests on that email returned HTTP 429. A * entry of 60 tokens per minute returned HTTP 429 after the user had used the budget. Cedar could not resolve the user: the principal id was the caller role ARN without the session name, and Cedar policies that tested for the session name, the email, the email tag or a source identity attribute did not match. The interceptor received X-Amz-Security-Token for the caller role session, with no source identity in the request. Amazon Bedrock logged the gateway execution role. CloudTrail recorded the source identity in the AssumeRole event for the caller role. CloudTrail data events for the gateway recorded the caller session ARN and the source identity.Admin pushes: the credential helper, an AWS CLI profile that runs it through
credential_process , and Claude Code managed settings that set AWS_PROFILE to that profile. The AWS CLI has no configuration key that sets a source identity, so the helper must make the AssumeRole call. On the AWS side: the email attribute for access control in IAM Identity Center, the caller role, a permission set policy that allows sts:AssumeRole and sts:SetSourceIdentity on the caller role, and the gateway rate limit.Gaps:
- Remove
bedrock-agentcore:InvokeGatewayfrom the permission set and grant it only to the caller role.
- Gateway rate limits fail open.
- Role chaining limits the session to a maximum of one hour, so the helper runs at least hourly.
- AWS STS accepts a source identity of 2 to 64 characters, made of letters, digits, and the characters
_ + = , . @ -. An email address outside those rules fails theAssumeRolecall.
- The trust condition is only as strong as the
emailtag. IAM Identity Center takes the value from the Identity Center directory, so keep that attribute under directory control.
- Only the gateway rate limits and CloudTrail see the user. Cedar, the interceptor and Amazon Bedrock logs do not.
How to keep all the inference calls to AWS Bedrock via the AgentCore Gateway?
Three controls keep every Claude Code call on the gateway, not let direct bedrock calls go through. This assumes the AWS member account only runs inference for Claude Code and the gateway is in that account.
Laptop
Mobile device management pushes Claude Code managed settings that point to the gateway. A developer's own settings files cannot override a managed value. A local administrator can still edit the managed file, so this control sets the path and the next two enforce it.
Access
Developers get no Amazon Bedrock permissions in the account. With a JWT gateway, developers need no AWS credentials in the account at all. With an IAM gateway, the caller role allows only
bedrock-agentcore:InvokeGateway on the gateway. Developers also get no AgentCore control plane permissions. Only a platform administrator role can call UpdateGateway , CreateGatewayTarget , UpdateGatewayTarget , or change the rate limits, policy engine or interceptor configuration, because any of those calls changes what the gateway enforces.Account
A service control policy (SCP) on the account lets only the gateway execution role call models. The core statement denies model invocation for every other principal.
Add statements that deny both
bedrock:CallWithBearerToken and bedrock-mantle:CallWithBearerToken , which together prevent all API key based access to Amazon Bedrock, and that deny changes to the gateway execution role by anyone except a platform administrator role. Pin the gateway role trust policy to the gateway ARN with aws:SourceAccount and aws:SourceArn .Three limits apply. SCPs do not affect users or roles in the management account, so the Claude Code account must be a member account.
To detect attempts, alert on CloudTrail
InvokeModel and Converse events in the account whose caller is not the gateway execution role. Amazon Bedrock logs these operations as management events, which CloudTrail records by default. Restrict the query to events where eventType is AwsApiCall , because a streaming invocation also records a stream-completion event with eventType AwsServiceEvent that is not an API call.Takeaways
- Through the gateway, Amazon Bedrock always logs the gateway execution role. Per-user cost attribution from Amazon Bedrock logs needs the direct path or a client-side record.
- On an IAM gateway, IAM Identity Center attributes do not reach the gateway, Cedar or the interceptor.
- A source identity set by role chaining gives per-user gateway rate limits. It does not reach Cedar or the interceptor.
- A JWT gateway with your identity provider is the only tested setup where the gateway, Cedar and the interceptor all see a verified user.
- Managed settings, no Amazon Bedrock permissions for developers, and a one-account SCP keep every Claude Code call on the gateway.
Further reading
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article