AI agents · OpenClaw · self-hosting · automation

Quick Answer

Short-Lived Credentials for AI Agents: Best Practices 2026

Published:

The short answer

Never give an AI agent a standing key. Have a broker mint a credential per task, scoped to one resource, valid for minutes, injected at the tool boundary, and revoked when the task ends. Every major platform supports this natively in 2026; the work is wiring it into your agent runtime.

Native lifetimes you can rely on (October 2026)

PlatformMechanismLifetimeScope controls
GitHubGitHub App installation access tokenAlways expires after 1 hourPer-repository and per-permission at mint time
AWSSTS AssumeRole900–43,200 s (15 min–12 h); role chaining max 1 hSession policies narrow the role (result = intersection); session tags for ABAC
Google CloudShort-lived service-account access tokenDefault max 1 hour; up to 12 h only if org policy allowsImpersonate a dedicated, minimally-privileged service account
HashiCorp VaultDynamic secrets with leasesAny TTL you set; revoked automatically on expiryPer-role policies; revoking an AWS lease deletes the keys

The seven practices

  1. One credential per task, not per agent. An agent identity is long-lived; its credentials should not be. When the orchestrator starts a task (“open a PR on repo X”), it requests a token for exactly that.
  2. Scope at mint time. A GitHub App token can be limited to one repository and contents: write; an AWS session policy can cut a broad role down to one bucket prefix. Anything the task doesn’t need is removed before the token exists.
  3. Shortest lifetime that finishes the job. 15 minutes for a deploy step, 1 hour for an interactive coding session. If a task runs longer, re-mint; don’t raise the TTL.
  4. Keep the long-lived secret out of the model. The private key, Vault token or OIDC trust lives in the broker or tool server. The model calls a tool; the tool attaches the short-lived credential to the outgoing request. Nothing the model can print or be injected into reveals a secret.
  5. Revoke on completion. Expiry is the backstop, not the plan. Vault leases and GitHub installation tokens can be revoked explicitly when the task ends or the agent is stopped.
  6. Log issuance with context. Record agent ID, task ID, scope, TTL and requester for every mint. AWS session tags and Google service-account impersonation logs carry this into the provider’s audit trail.
  7. Pair it with egress control. A token is only useful to an attacker if it can be sent somewhere. Deny-by-default outbound networking for the agent sandbox turns a leaked 1-hour token into a non-event.

Reference flow

agent → tool call "create_pr(repo)" → broker
broker: check policy(agent, task, repo) → mint GitHub App token (repo-scoped, 1 h)
tool server: attach token → GitHub API
task done → broker revokes token → audit log entry

Common mistakes

  • Personal access tokens in .env. Long-lived, broad, tied to a human. Replace with a GitHub App.
  • Long TTL “to avoid failures”. Fix the refresh path instead; GitHub’s SDKs regenerate installation tokens automatically when they expire.
  • One role for all agents. You cannot revoke one misbehaving agent without stopping all of them.

The end-to-end architecture (boundary injection, egress, file primitives, audit) is in how to give AI agents credentials without leaking them; identity, sandboxing and rotation drills are in how to secure AI agent credentials.

Last verified: October 7, 2026.

Sources