Content

How to Connect GitHub to an AI Agent (2026)

Most of what you want an agent to do on GitHub needs no push access at all, and a search for "create an issue" can just as easily surface a security-vulnerability report. How to scope a GitHub agent to exactly the operations it needs.

IntegrationsOct 21, 2026

Key takeaways

  • -A semantic search for "create an issue" can surface GitHub's security-vulnerability report ahead of the plain issue-creation call, and they are not the same action.
  • -Most agent work on GitHub, triage, labelling, commenting, opening pull requests, needs read plus a narrow write, not push access to protected branches.
  • -Fine-grained tokens scope to specific repositories with per-permission settings; a classic token's repo scope reaches every repository the account can touch.
  • -Only operations added with swy add method can run, so a mis-picked or overly broad action fails before it reaches GitHub rather than depending on the model's judgement.
  • -GitHub's secondary rate limits punish the bursty request pattern an agent naturally produces, and a retried write can create a duplicate comment or issue.

Ask an agent to create an issue in a repository, and a search over GitHub's catalog of operations can surface "report a security vulnerability to maintainers" ahead of the plain issue-creation call, because reporting a vulnerability is also, technically, creating a report about a problem. They are not the same thing. One of them notifies maintainers of a security issue, and an agent that picks it because it looked like the nearest match has done something very different from what you asked.

That mix-up is a small version of a bigger question: does your agent need to push code at all? Most do not. Triaging issues, replying to a review, labelling, opening a pull request from a branch, summarising a diff, none of that requires the ability to write to a protected branch. Teams tend to grant push access because it is the default shape of a token, not because the use case needs it.

Nothing about a GitHub token or the agent's instructions stops it from reaching for a stronger action than the task needs, whether that is a security report instead of an issue, or a push to main instead of a pull request. Swytchcode's GitHub integration fixes this by making the agent's exact set of allowed operations explicit and checked before anything reaches GitHub.

The problem: a token's scope is usually bigger than any one task needs

A classic personal access token is scoped by capability across everything the account can reach. Grant it broad write access and you have granted that on every repository the account touches, including private ones in other organisations. Start from the opposite end: work out the smallest write the agent genuinely needs, grant that, and widen later if you must. Nobody has ever successfully narrowed a token after people came to depend on it.

There is a second risk specific to GitHub: issue bodies, pull request descriptions, code comments and README files are text that anyone with a GitHub account can write. An agent that reads them is reading attacker-controlled content, and if that agent also holds a write credential, an issue filed by a stranger is a path to action. This is not hypothetical reasoning about models in general; it is the ordinary consequence of letting a public repository's contents reach something that can act.

How Swytchcode solves it

Swytchcode downloads a catalog of GitHub's operations and only runs the ones you have explicitly added. If the agent decides to delete a branch, or picks the security-advisory call when it meant to open an issue, the call fails at the runtime before a request reaches GitHub, because it was never added, not because an instruction got reinterpreted. Checking what a method actually does with swy info before allowing it is what catches a mix-up like the one above.

For the credential itself, a fine-grained personal access token is scoped per repository with per-permission settings, so you can grant issue write on one repository and nothing anywhere else, which is a reviewable permission in a way a broad classic token is not. A GitHub App is the stronger version of the same idea for anything multi-tenant, where installation is per organisation and permissions are declared up front. Either way the credential lives outside your project, in Swytchcode's local credential store, and is injected only when a call executes, so it never enters the model's context and a prompt injection asking for it finds nothing to return.

Setting up GitHub with Swytchcode

npm install -g swytchcode
swy login
swy init --mode=sandbox

swy get github
swy discover "create an issue in a repository" --project github
swy info github.issue.create        # confirm it is the operation you meant
swy add method github.issue.create  # allow exactly this one
swy auth connect github

An ordering detail worth knowing: swy discover searches the whole catalog, but swy info only works on a provider you have already fetched, so swy get github comes first or info will report a real operation as missing. Only methods added with swy add method can run.

Policies: build agents you can trust with write access

If the agent does write code, make it open a pull request rather than pushing directly. That is not caution for its own sake: a PR is a review gate your team already operates, with CI already attached, so routing agent changes through it gives you the existing safety machinery for free and a diff a human reads before anything lands. If the agent must push, push to its own branch and let branch protection do the rest.

For the operations that genuinely are irreversible, deleting a branch, closing someone else's PR, publishing a release, or touching a sensitive repository, a rule is better than trust:

{
  "id": "release-guard",
  "target": ["github.issue.create"],
  "when": { "field": "repository", "operator": "in", "value": ["infra", "billing"] },
  "action": {
    "type": "REQUIRES_APPROVAL",
    "message": "Changes to sensitive repositories need approval"
  },
  "approval_timeout": "2h"
}

A rule in version control, evaluated at execution time, rather than an instruction the model is trusted to follow. The reasoning is in a prompt is not a policy.

GitHub's API is how issues, pull requests, and repository content get read and changed, and the real risk is that a token's scope and a repository's contents are both bigger and more exposed than any one agent task needs, a classic token reaches every repo the account can touch, and anyone can write the issue text the agent reads. Swytchcode solves it by only running the specific operations you have added, scoping the credential to one or a few repositories, routing agent writes through a pull request, and backing the operations that can't be undone with a policy instead of trust.

FAQ

How do I connect GitHub to an AI agent?

Create a fine-grained token scoped to the specific repositories involved, or a GitHub App if you are connecting to other people's accounts, then allow only the operations the agent needs. With the Swytchcode CLI: swy get github, swy discover "create an issue in a repository" --project github, swy info github.issue.create, swy add method github.issue.create, swy auth connect github.

What GitHub permissions should an AI agent have?

The narrowest write that makes the use case work, on the smallest set of repositories. Most agent work, triage, labelling, commenting, opening pull requests, needs read plus one or two narrow writes, not push access to protected branches. Grant issue write on one repository rather than repo scope across an entire account.

Should I use a fine-grained token or a classic token?

Fine-grained, for an agent. A classic token with broad repo access applies to every repository the account can reach, including private ones in other organisations. A fine-grained token is scoped per repository with per-permission settings, which is something a security review can actually sign off.

Can an AI agent push directly to main?

It can if you give it that, and it is usually the wrong choice. Have it push to its own branch and open a pull request, so your existing review and CI gates apply to its changes the same way they apply to a person's. Branch protection then does the rest.

Is it safe to let an AI agent read GitHub issues?

Reading is low risk by itself, but issue bodies are text anyone can write, so an agent reading them while holding a write credential has a path from reading to acting. Limit what it can do rather than trying to sanitise what it reads.

Why is my GitHub agent hitting rate limits?

GitHub applies secondary rate limits on top of the hourly budget, and they target exactly the pattern an agent produces: a lot of requests in a short window, often against the same endpoint. An agent asked to triage a few hundred open issues will burst, get throttled, retry, and burst again. Pace the work rather than fanning it out at once, and make sure a secondary-limit response triggers a backoff rather than an immediate retry, which only extends the block.

How do I audit what an AI agent did on GitHub?

Use GitHub's audit log for what landed, and swy audit network plus swy audit policy for what was attempted, including refusals. An agent's own chat history is not an audit trail, because it records what the model believed it did and omits the calls that never reached GitHub.

Can I build an AI agent on top of GitHub with Swytchcode?

Yes. Allow exactly the triage, comment, and PR operations a given agent needs, scope the token or GitHub App to the repositories involved, and build on top through the CLI, the SDK, or an MCP client.

Can Swytchcode stop an agent from pushing to main or touching a sensitive repository, even if it's asked to?

Yes. If a push-to-main operation was never added to the allowlist, the call fails at the runtime before it reaches GitHub, whatever the instruction says. For repositories you've marked sensitive, the approval policy above stops the change from landing until a human signs off, rather than relying on the agent to recognise the repository is special.

If an agent retries a GitHub call after a timeout, will it duplicate a comment or an issue?

It can, if nothing is protecting the write. A retried write here is a duplicate comment or a duplicate issue rather than an error, the same class of problem as a retried payment, so give writes an idempotency key where the method supports one rather than assuming a retry is harmless.

More content