Identity for Agents: One Role per Agent, One Session per Task, No Shared Keys
A lot of agents in production authenticate with a key copied from somewhere: an API key in an environment variable, shared by three agents and a cron job, with whatever permissions the person who created it happened to have. When one agent misbehaves, you can’t tell which one it was from the logs. You can’t narrow the key without breaking the others. Rotating it means coordinating every consumer.
Agents need what any workload needs, plus a bit more: their own identity, credentials narrowed to the current task, and a record of which person they were acting for.
Level 1: each agent gets its own workload identity
Every distinct agent runs under its own role. That’s an IAM role on AWS, a service account on Google Cloud, and a managed identity on Azure. The platform issues short-lived credentials to it automatically:
- Lambda: the function’s execution role.
- ECS: the task role (not the execution role, which is for ECS itself).
- EKS: EKS Pod Identity or IRSA, with one Kubernetes service account per agent.
- Outside AWS: IAM Roles Anywhere for workloads with X.509 certificates, or OIDC federation from CI.
There is no stored long-lived AWS access key to rotate. Temporary credentials can still be stolen and used until they expire or are revoked; CloudTrail attribution depends on the service and event types you enable. For platforms outside the cloud provider, SPIFFE is the standard way to give workloads verifiable identities.
For third-party APIs that only take API keys, keep the keys in a secrets manager, readable only by the one agent role that needs each key, and prefer providers that support scoped keys.
Level 2: narrow the credentials for each task
An agent’s role has to allow everything the agent might ever need to do. A single task usually needs far less. If the billing agent is handling a dispute for one customer, why should that session be able to read every customer’s invoices?
AWS STS lets you narrow a session when you create it. The agent host, which is trusted code rather than the model, assumes the agent’s role with an inline session policy. For identity-based permissions, the session gets the intersection of the role policy and session policy. Resource-based grants also matter: a grant directly to the session principal can bypass this implicit limit. Audit those grants and explicit denies. This illustrative host-side fragment also requires a trusted tenant-to-prefix mapping:
import json
import re
import boto3
sts = boto3.client("sts")
def task_credentials(role_arn: str, tenant_id: str, requested_by: str):
# tenant_id must also come from an authenticated, authorized tenant mapping.
if not re.fullmatch(r"[a-z0-9][a-z0-9-]{0,47}", tenant_id):
raise ValueError("invalid tenant identifier")
session_policy = {
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["s3:GetObject"],
"Resource": f"arn:aws:s3:::invoices-prod/{tenant_id}/*",
}],
}
resp = sts.assume_role(
RoleArn=role_arn,
RoleSessionName=f"billing-{tenant_id}"[:64],
Policy=json.dumps(session_policy),
DurationSeconds=900,
SourceIdentity=requested_by, # the human who started this task
Tags=[{"Key": "tenant", "Value": tenant_id}],
)
return resp["Credentials"]The inline policy permits reads from one validated prefix for a 15-minute session. Test the effective permissions with the real bucket policies and any resource-based grants; this fragment alone is not proof of tenant isolation. The host must choose role_arn and requested_by from trusted configuration and authenticated identity, never model arguments.
Two things have to be true for this to work:
- The role’s trust policy must allow
sts:AssumeRole,sts:SetSourceIdentityandsts:TagSessionfrom the agent host’s identity. - The model never sees or handles credentials. The host builds the session and passes it to the tool implementations. The model only chooses which tool to call.
Level 3: carry the human through
When an agent acts because a person asked it to, the audit trail should say so. SourceIdentity is set when the session is created. It can be used to attribute subsequent logged AWS activity and persists through supported role chaining. Enable the relevant CloudTrail data events as well as management events; a field cannot explain calls you did not log. A query for “everything done on behalf of alice@example.com” then covers her direct actions and her agents’ actions.
Session tags do the same for attributes you want to use in policies. The tenant tag above can drive attribute-based access control. For example, a role policy can allow access only where aws:ResourceTag/tenant equals aws:PrincipalTag/tenant.
Level 4: acting for users in other systems
When an agent acts in a user’s own accounts (their calendar, their GitHub, their CRM), it should use delegated, scoped tokens for that user, not a service account that can reach everyone’s data.
- Use the provider’s OAuth flow with the narrowest scopes the task needs, and store refresh tokens per user in a secrets store, encrypted.
- Where supported, use OAuth 2.0 Token Exchange (RFC 8693) to swap a user’s token for one scoped to the specific downstream service, rather than forwarding the user’s broad token.
- MCP’s authorization model follows the same principle: servers act as OAuth resource servers, and clients request tokens bound to a specific server with resource indicators, so a token issued for one server can’t be replayed against another.
Managed options exist too. Bedrock AgentCore Identity and similar services handle the OAuth flows and token storage for agents. The principle doesn’t change: the agent acts with the user’s permission, limited to the scopes the user granted.
Where this stops
Identity answers “who is acting, for whom, and with what permissions?” It doesn’t answer “should this action happen?” An agent with perfectly scoped, perfectly attributed credentials can still take a wrong action inside its scope. Approval gates for irreversible actions, and evals on behaviour, still apply. Good identity makes attribution and targeted revocation possible. Whether an allowed mistake is small still depends on the permissions, affected data and execution controls.