Building Managed Agents That Use GitHub Without Exposing Your Token
You can run the GitHub CLI (gh) inside a Gemini API Managed Agents without exposing your GitHub Personal Access Token (PAT) to the sandbox.
The Network Configuration egress proxy injects your real token into outbound requests. The agent uses a dummy token locally, so the real token never enters the sandbox.
How it works#
- Dummy token: The agent exports
GH_TOKEN="dummy"inside the sandbox to satisfygh's local auth checks. - Interception: The egress proxy intercepts requests to
api.github.comorgithub.com. - Transformation: The proxy replaces the dummy token in the
Authorizationheader with your real token. - Security: The real token remains in the control plane and is never visible inside the sandbox.

Prerequisites#
-
Gemini API Key: Get your key from Google AI Studio.
-
GitHub Personal Access Token (PAT): I recommend a Fine-grained PAT restricted to the specific repositories your agent needs.
To create one:
- Go to GitHub -> Settings -> Developer settings -> Personal access tokens -> Fine-grained tokens or click here.
- Click Generate new token.
- Under Repository access, select your target repositories.
- Under Permissions -> Repository permissions, configure:
- Contents:
Access: Read and write(to clone and push code). - Pull requests:
Access: Read and write(to open, review, and comment on PRs). - Issues:
Access: Read and write(to create and comment on issues).
- Contents:
- Click Generate token and copy it. It won't be shown again.
Example#
This Python example spins up the agent in an isolated, ephemeral Linux environment, configures the network allowlist with header transformations, and runs gh.
Python
# GitHub PAT from host
=
# Base64-encode for Git HTTPS auth
= f
=
=
# Shim: auto-installs gh, injects dummy token, ensures git HTTPS and no prompt
=
=
=
# Logged in as philschmid; cloned octocat/Spoon-Knife to /workspace/Spoon-Knife
GitHub uses different auth schemes depending on the request. API calls to api.github.com expect a Bearer token, while Git operations against github.com require base64-encoded Basic auth.
The wildcard entry ("domain": "*") lets the sandbox reach everything else, e.g. downloading the gh binary, fetching release assets, etc. You can lock this down to specific domains like objects.githubusercontent.com if you prefer tighter controls.
Persistent "GitHub agent"#
To make the agent autonomous and avoid setup delays, you can create a managed agent that auto-installs the gh CLI. It reuses the environment after the first interaction to keep the installed gh binary across calls.
Python
# GitHub PAT from host
=
= f
=
=
=
# Shim: auto-installs gh, injects dummy token, forces HTTPS and disables interactive prompts
=
The first interaction clones the base environment and triggers gh installation.
Python
=
# ... currently logged-in GitHub user is **philschmid**.
For subsequent calls, pass interaction_1.environment_id to reuse the same container, keeping the installed gh binary and inheriting the network transforms. Optionally pass previous_interaction_id=interaction_1.id as well if you want to retain conversation history across turns.
Python
=
# Based on the inspection of pull request #12 in the
Conclusion#
You can give Managed Agents access to GitHub without putting secrets in the sandbox. A dummy token satisfies gh locally, and the egress proxy injects the real PAT on the way out.
For more on Managed Agents, environments, and network transforms, see the Managed Agents docs and Network configuration.
Thanks for reading! If you have any questions or feedback, please let me know on Twitter or LinkedIn.