Skip to content
mentoor

Topic 1 of 5 · 4 min read

Scoped, short-lived tokens for agents

Goal: Pick the narrowest token scopes and lifetime for an agent task and reject an over-broad grant.

Give an agent a token that can do this one task, on these resources, for a short time, and nothing else. The easy path is to paste your own personal token into the agent's environment. That hands the agent everything you can do, for as long as the token lives.

Check yourself · 1 of 3

An agent only needs to post comments on pull requests in one repository. Which token fits best?

Choose one answer

Why it matters

An agent acts on text it reads, and some of that text is written by strangers. If a prompt injection or a plain model mistake makes the agent misuse a credential, the damage is capped by what the credential allows. OWASP names excessive permissions, along with excessive functionality and autonomy, as a root cause of agent incidents. A narrow token turns "the agent deleted production" into "the agent failed a call".

How to do it

  1. Write the task in one line. "Comment on pull requests in the docs repository."
  2. List the exact actions. Read the pull request, post a comment. Not push, not merge, not read other repositories.
  3. Map actions to the smallest scopes. Prefer fine-grained tokens, such as GitHub fine-grained personal access tokens, that name one repository and individual permissions. With OAuth, request only the scopes the task needs, never a catch-all such as admin or *.
  4. Set a short lifetime. Hours for a task, days at most for a scheduled job. Short expiry limits how long a leaked token is useful.
  5. Issue per task or per agent, not shared. One token per agent lets you revoke one without breaking the others.
  6. Keep the token out of the model's reach. The tool layer holds the secret and attaches it to calls. The model never sees it, so it cannot echo it into a prompt, a log or a message.

Before and after

# Before: the developer's own classic token
GITHUB_TOKEN: ghp_...redacted   # scopes: repo, workflow, admin:org
expires: never
# After: a token minted for the docs-review agent
GITHUB_TOKEN: github_pat_...redacted
repositories: [acme/docs]
permissions: { pull_requests: write, contents: read }
expires: 7 days

The first token can push to every repository and change the organisation. The second can only read one repository and comment on its pull requests.

What goes wrong

  • The convenience token. "It only runs on my laptop" stops being true when the agent reads an issue written by someone else.
  • Scope creep by error message. The agent hits a permission error and you widen the scope to make it go away. Add the one missing permission, not the whole family.
  • Never-expiring tokens. They outlive the project and the person who made them.
  • Secrets in the prompt. A token pasted into the conversation is now in logs and model context.

Examples

Narrowing a CI summariser

An agent summarises failed CI runs. It was given a token with repo and workflow scopes.

scopes_needed: [actions:read, contents:read]
scopes_removed: [workflow, repo:write]
expires: 24h

It only reads logs, so read permissions on one repository are enough. Rerunning workflows would need a separate, reviewed grant.

A permission error is not a reason to widen

The agent fails with 403: pull_requests:write required. Add that one permission to the token for that one repository. Do not switch to an organisation-wide admin token "just to unblock it".

Key points

  • Start from the task and list the exact actions before choosing any scope.
  • Prefer fine-grained tokens that name specific resources over a human's broad personal token.
  • Set an expiry that matches the task: hours or days, not never.
  • Issue one credential per agent or task so you can revoke it alone.
  • Keep the secret in the tool layer so the model never sees or repeats it.

Common mistakes

  • Reusing a developer's personal token because it already works.
  • Granting a broad scope after one 403 error instead of adding the single missing permission.
  • Using tokens that never expire, or one shared token for several agents.
  • Pasting the secret into the prompt or system message instead of the tool layer.

Check yourself · 2 of 3

Why should an agent token expire after hours or days?

Choose one answer

Check yourself · 3 of 3

The agent gets "403 Forbidden: pull_requests:write required". What is the best fix?

Choose one answer

Practice items, not counted towards your mastery.

Start here