Most coding agents end their work the same way: push a branch and open a pull request. That needs a Git credential, and the easiest one to reach for is the worst: your own personal access token, with access to everything you can touch, valid for months, pasted into an environment variable.

This post compares the credential types GitHub offers for automation, from GitHub's own documentation, and recommends a default for agents: a GitHub App installation token, minted per run, scoped to one repository, and handed to Git without being written to disk.

What an agent actually needs

Before choosing a credential, list the operations. A typical coding task needs:

  • Read the repository: clone or fetch.
  • Write one branch: push the agent's work.
  • Open a pull request, which is an API call, not a Git operation.

It should not need: pushing to the default branch, changing workflows, administering the repository, or touching any other repository. Every credential option below should be judged by how close it gets to exactly that list.

The options

Classic personal access tokens

Broad scopes across every repository you can access, and they act as you. Avoid them for agents.

Fine-grained personal access tokens

GitHub's token documentation lists the improvements: each token is limited to resources owned by a single user or organization, can be limited to specific repositories, and carries only the permissions you select. The docs add that tokens always include read-only access to all public repositories.

Good, but they still act as a person, they live as long as the expiry you set, and creating one per agent run is manual work.

Deploy keys

An SSH key attached to one repository. GitHub's deploy key documentation notes that deploy keys are read-only by default but can be given write access, only grant access to a single repository, and are usually not protected by a passphrase, making the key easily accessible if the server is compromised.

Write access on a deploy key is all-or-nothing for that repository, and a deploy key cannot call the API to open a pull request. Useful for read-only clones; limited for agents that need to open PRs.

GitHub App installation tokens

A GitHub App is an identity of its own, installed on the repositories you choose. Your backend uses the app's private key to mint installation access tokens. GitHub's docs say the token expires after one hour, and that installation tokens can be scoped to single or multiple repositories, with fine-grained permissions.

For an agent run, that means you can mint a token that:

  • works on exactly one repository,
  • has contents: write and pull_requests: write and nothing else,
  • expires an hour after it was created,
  • shows up in history as the app, not as a person.

Side by side

Fine-grained PAT Deploy key GitHub App token
Acts as A person The key The app
Repository scope Selected repositories One repository Selected repositories, narrowable per token
Permissions Selected Read, or read and write Selected, narrowable per token
Lifetime Until the expiry you set Until removed One hour
Can open a PR Yes No Yes
Minted per run by code Not practical Not practical Yes

Recommended setup

  1. Create a GitHub App for your agent with the minimum permissions: contents read and write, pull requests read and write. Do not grant workflow permissions, so the agent cannot change your CI.
  2. Install it only on the repositories agents should work on.
  3. Keep the private key in your backend, never in the agent's environment.
  4. Per run, mint a token for the one repository the task is about, with only the permissions that task needs. A read-only analysis task gets a read-only token.
  5. Protect the default branch so pushes must come through a pull request with review. The agent's token can push branches, and branch protection decides what reaches main.

Handing the token to Git without writing it down

GitHub's docs show the installation token used as the HTTP password with the username x-access-token, for example in a clone URL. Putting the token in the URL works, but Git then stores the URL, token included, in .git/config, where any later process can read it, and where it ends up in a snapshot or an uploaded workspace.

A credential helper that reads the token from the environment keeps it off disk:

git -c credential.helper= \ -c credential.helper='!f() { test "$1" = get || exit 0; echo username=x-access-token; echo "password=${GH_TOKEN}"; }; f' \ push origin agent/fix-flaky-test 

The empty credential.helper= first clears any helpers configured elsewhere, so the token is not also saved by a credential store. The function answers only get requests, so Git never asks it to store or erase anything.

The token is still visible inside the run

Keeping the token off disk does not hide it from the code running alongside Git. Any process in the same environment can read GH_TOKEN, as our post on secrets in environment variables explains. That is why the token's scope and lifetime matter more than how it is passed: if the agent's code is compromised, the attacker gets one repository, branch pushes and PRs, for less than an hour.

Two further steps reduce even that:

  • Separate pushing from running. Let the agent's code run without any Git credential, then have a separate, trusted step read the diff and push it. The agent never holds the token at all.
  • Review before pushing. Put an approval in front of the push for repositories where an unreviewed branch is itself a risk.

Where Sandscale fits

If your agent runs in a Sandscale sandbox, you can hand it a token through Vault: reference the secret when you start the sandbox, and the value reaches the sandbox's environment without passing through your code, the API's answer, a log or the audit log, which names the variable but not its value. For per-run GitHub App tokens, mint the token in your backend and pass it for that run only. Either way, the code in the sandbox can read it, so scope it as if it will be read.

Originally published on the Sandscale blog.