# Giving an AI Agent GitHub Access Safely: Scoped Tokens in Practice

> Why a broad OAuth login is risky for an AI agent, and how a single-repo fine-grained token with pull-request review keeps the damage small.

- Author: Elber Dalfidan (https://dalfidan.dev/about/)
- Published: 2026-11-23
- Tags: ai, agents, security, github
- Canonical URL: https://dalfidan.dev/writing/ai-agent-github-scoped-token/
- Language: en

![A padlock on a code-built repository door with a single key, on a calm desk at dusk](https://dalfidan.dev/posts/ai-agent-github-scoped-token/cover.webp)

If an AI agent or scheduled bot needs to touch your GitHub repos, give it a fine-grained personal access token that can reach one repository, with only the permissions it needs and a short expiry. Then make it land changes through pull requests on a protected branch, so a human approves every merge. This does not make the agent safe. It makes the damage it can do small, visible, and reversible.

## Why is a broad OAuth login risky for an agent?

The quickest way to connect a tool to GitHub is to log in and accept whatever it asks for. For many tools that means the `repo` scope, or a classic personal access token with that scope.

GitHub's own scope reference describes `repo` as granting "full access to public and private repositories," including code, commit statuses, collaborators, invitations, deployment statuses, and repository webhooks. It also grants access to manage some organization-owned resources, such as projects, team memberships, and webhooks.

A classic token is just as wide. GitHub's docs say a classic token grants access to every repository inside the organizations you belong to, plus all of your personal repositories.

For an agent, that is a large blast radius. Agents read untrusted input: issues, comments, web pages, READMEs. A prompt injection or a plain bug can turn that input into actions that reach every repo you own and every organization you belong to.

A token can never do more than its owner can. Scopes only narrow access. The problem is that most of us can do a lot.

## What can a fine-grained token actually limit?

GitHub recommends fine-grained personal access tokens over classic ones "whenever possible." According to the docs, they give you four levers:

| Lever | What it limits |
|---|---|
| Resource owner | The token only reaches resources owned by one user or one organization. |
| Repository access | You can restrict it to specific repositories under that owner. |
| Permissions | You grant individual permissions (Contents, Pull requests, Issues, and so on) at read or write level, instead of broad scopes. |
| Expiration | You pick a lifetime. Organization or enterprise owners can enforce a maximum lifetime. |

Organizations get one more control. Owners can require approval before a fine-grained token can access organization resources. Until it is approved, the token can only read public resources.

There are trade-offs. GitHub lists gaps, including repos where you are an outside collaborator, several organizations with one token, Packages, and the Checks API. You can also create at most 50 fine-grained tokens.

## How do I set one up for an agent?

Follow GitHub's flow under Settings, Developer settings, Personal access tokens, Fine-grained tokens. As you go, make these choices:

- [ ] **Name and description.** Say which agent uses it and why.
- [ ] **Expiration.** Pick a short, finite lifetime. GitHub allows no expiry, but an expiry turns a forgotten token into a dead one.
- [ ] **Resource owner.** Choose the single account or organization that owns the target repo.
- [ ] **Repository access.** Choose "Only select repositories" and pick one repo.
- [ ] **Permissions.** For an agent that edits code and opens pull requests, start with Contents (read and write) and Pull requests (read and write). Add Issues only if it must comment or triage. Metadata (read) comes with it.
- [ ] **Leave off what it does not need.** Administration, Secrets, Actions, Webhooks, and Workflows are the dangerous ones. Administration write can change repository settings. Workflows write lets the token change GitHub Actions workflow files, which run code with their own credentials.
- [ ] **Store it as a secret.** Use your platform's secret store or environment variables. Never put it in the repo, in a prompt, or in a log.
- [ ] **Delete it when the job ends.** GitHub deletes tokens unused for a year, but do not rely on that.

Each REST endpoint's documentation states whether fine-grained tokens can use it and which permission it needs. If the agent gets a 403, check that page before widening anything.

## Why add pull-request review as a second safety net?

A scoped token limits where the agent can write. Branch protection limits how its writes reach the code you ship.

On your default branch, turn on a branch protection rule (or a ruleset) with these settings, all documented by GitHub:

- **Require pull request reviews before merging.** Changes can only reach the branch through a pull request with the required number of approvals.
- **Dismiss stale pull request approvals when new commits are pushed.** If the diff changes after approval, the approval is dropped. This stops an agent from adding unreviewed content to an approved PR.
- **Require approval of the most recent reviewable push.** Someone other than the person who pushed last must approve. The token acts as you, so the agent's pushes count as yours. Check how this fits your setup before relying on it, especially if you work alone.
- **Require status checks before merging.** Tests and linters must pass first.
- **Do not allow bypassing the above settings.** This one is easy to miss. By default, branch protection rules do not apply to repository admins. If you are an admin, a token you create inherits that, so an agent using your token could skip review unless you turn this on.

By default, a protection rule also blocks force pushes and branch deletion on the protected branch. Keep those defaults.

Check your plan. GitHub documents protected branches for public repos on the Free plans, and for private repos on Pro, Team, and Enterprise plans.

## What can the token still not stop?

Scoping shrinks the risk. It does not remove it. Within its one repo, an agent with Contents write can still:

- **Read everything in the repo,** including any secret someone committed by mistake.
- **Push to, rewrite, or delete unprotected branches.** Protection applies only to the branches your rule matches.
- **Open convincing but harmful pull requests.** Review only helps if the reviewer reads the diff. A rubber stamp is not a control.
- **Leak the token itself.** If the agent's environment is compromised, the token works for anyone until it expires or you delete it.

Also, both fine-grained and classic tokens are tied to the user who created them and become inactive if that user loses access. For shared, long-running automation, GitHub recommends a GitHub App instead.

## The takeaway

Give an agent the smallest key that does the job. That means one repo, two or three permissions, and a short expiry. Then put a protected branch with real review, and no admin bypass, between the agent and anything you ship. If the agent misbehaves, the worst case becomes a bad pull request you can close. It should not be a rewritten history across every repo you own.

Last verified: 2026-11-23 against GitHub Docs. GitHub's token features and plan availability change, so check the linked pages before relying on details.

## Sources

- "Managing your personal access tokens," GitHub Docs, accessed 2026-11-23. https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens
- "Scopes for OAuth apps," GitHub Docs, accessed 2026-11-23. https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/scopes-for-oauth-apps
- "About protected branches," GitHub Docs, accessed 2026-11-23. https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches
- "Permissions required for fine-grained personal access tokens," GitHub Docs, accessed 2026-11-23. https://docs.github.com/en/rest/authentication/permissions-required-for-fine-grained-personal-access-tokens
