Skip to content
Aventail™ co-founder speaks to BBC Radio Wales on AI safety and AI agents.Read the story
Aventail™
All guides

OpenCode + Gemini: API key and Vertex AI setup

13 min read

Use Gemini in OpenCode two ways: a Google AI Studio API key or Vertex AI. Exact commands, model IDs, how to check the route, and what Google does with prompts.

OpenCode talks to Gemini natively, in two ways. With a Gemini API key from Google AI Studio, run /connect, choose Google and paste the key. With Vertex AI, sign in with gcloud auth application-default login and set a Google Cloud project in opencode.json.

No proxy is needed for either. The model then appears as google/gemini-4-argon or google-vertex/gemini-4-argon. The two paths reach the same models under different data terms, and that is the real choice: on the free tier of the API Google uses your prompts to improve its products, on the paid tier it does not, and Vertex AI adds region control and a route to zero data retention.

Gemini API key (Google AI Studio)

Vertex AI

Provider ID in OpenCode

google

google-vertex

Sign-in

API key through /connect, or an environment variable

Application Default Credentials or a service-account file, plus a project

Config file

Not needed

Project and location

Model reference

google/gemini-4-argon

google-vertex/gemini-4-argon

Billing

Free tier, or a project with a billing account

Your Google Cloud project

Prompts used to improve Google's products

Free tier: yes. Paid tier: no

No, not without your permission or instruction

Kept for abuse monitoring

55 days

Flagged prompts up to 90 days for customers on the online terms; Gemini 4 Carbon up to 30 days in your environment

Zero data retention

Not guaranteed

Possible, under Google's listed conditions

Region

No region setting in the docs read

The location you set; OpenCode defaults to us-central1

Suits

One developer who wants to start today

A company with a Google Cloud agreement, or region and retention requirements

Facts in this table were read on 15 November 2026 and are sourced at the end.

What you need

Minimum

Notes

OpenCode

OpenCode 2 or OpenCode 1

OpenCode's docs now describe OpenCode 2

For the API key path

A key from aistudio.google.com/apikey

Gemini 4 Argon and Carbon need the paid tier

For the Vertex path

The gcloud CLI and a Google Cloud project

Application Default Credentials alone do not activate the provider

A model with function calling

gemini-4-argon, gemini-4-carbon, gemini-4-hydrogen or gemini-3.8-flash

All four have a 1M-token context window

Step 1: install OpenCode

curl -fsSL https://opencode.ai/v2/install | bash

npm install -g @opencode/cli

brew install anomalyco/tap/opencode-v2

These are the OpenCode 2 commands from OpenCode's docs. The curl installer replaces an OpenCode 1 binary.

Step 2: connect with a Gemini API key

Create a key at aistudio.google.com/apikey. In OpenCode:

/connect
/models

Choose Google in the provider list, paste the key, then select a Gemini model. To log in without opening the TUI, run opencode auth login and use the picker.

OpenCode's providers page has no section for the Gemini API, so two details here come from models.dev, the catalogue OpenCode says it uses. The provider ID is google, and the environment variables that can supply the key are GOOGLE_API_KEY, GOOGLE_GENERATIVE_AI_API_KEY and GEMINI_API_KEY. Run opencode models to see the exact IDs on your install.

To supply the key as an environment variable, set it on the OpenCode server process. For a one-off private server:

GEMINI_API_KEY=your-key opencode --standalone

For the shared background service:

opencode service set env GEMINI_API_KEY your-key
opencode auth list

The connection then appears in auth list with the type environment. A saved account takes precedence over an environment connection for the same provider.

Step 3: or connect through Vertex AI

Google Vertex uses Application Default Credentials and needs a project before its models appear. Create the credentials:

gcloud auth application-default login

Set the project and location in opencode.json:

{
"$schema": "https://opencode.ai/config.json",
"providers": {
"google-vertex": {
"settings": {
"project": "my-project",
"location": "us-central1"
}
}
}
}

You can set both from the environment instead. OpenCode reads the project from GOOGLE_VERTEX_PROJECT, GOOGLE_CLOUD_PROJECT, GCP_PROJECT or GCLOUD_PROJECT, in that order, and the location from GOOGLE_VERTEX_LOCATION, GOOGLE_CLOUD_LOCATION or VERTEX_LOCATION:

GOOGLE_CLOUD_PROJECT=my-project GOOGLE_VERTEX_LOCATION=europe-west4 opencode --standalone

For a service account, point GOOGLE_APPLICATION_CREDENTIALS at the JSON file and still provide a project:

GOOGLE_APPLICATION_CREDENTIALS=/secure/vertex.json \
GOOGLE_CLOUD_PROJECT=my-project \
opencode --standalone

In OpenCode 2 the single google-vertex provider covers Gemini, Anthropic and OpenAI-compatible models in the Vertex catalogue. The old google-vertex-anthropic ID is gone. Google's docs now call Vertex AI the Gemini Enterprise Agent Platform; OpenCode keeps the Vertex name.

Step 4: set the default model

Pick a model for the session with /models. To make it the default for new work, set model in opencode.json:

{
"$schema": "https://opencode.ai/config.json",
"model": "google/gemini-4-argon"
}

Use google-vertex/gemini-4-argon for the Vertex path. For one command-line run:

opencode run --model google/gemini-4-argon "Refactor parseToken"

Provider and model IDs are case-sensitive.

Step 5: check which model answered

  • The prefix tells you the service. A model ID that starts with google/ goes to the Gemini API with your key. One that starts with google-vertex/ goes to your Google Cloud project. If both are connected, the ID you picked in /models shows which one the session uses.

  • opencode auth list shows the active connections and whether each is a saved account or an environment variable.

  • Google's side, API key path. The Logs dashboard in AI Studio (aistudio.google.com/logs) lists GenerateContent calls for projects on the paid tier with logging on.

  • Google's side, Vertex path. Request-response logging is off by default. Turning it on writes requests to a BigQuery table, and ends zero data retention for that model.

What works and what does not

Works: chat, file edits, shell commands and subagents. Google lists function calling as supported on the Gemini 4 models and Gemini 3.8 Flash. Gemini caches prompts automatically, so OpenCode's cache-lifetime setting does not apply to it.

Does not work, or works differently:

  • Gemini 4 Argon and Carbon are not on the free tier. A key from a project without billing can use Gemini 4 Hydrogen and Gemini 3.8 Flash, within rate limits.

  • Signing in with a Google account is not a built-in method. A community plugin, opencode-gemini-auth (MIT licence), adds Google OAuth. Its own README says Google ended consumer access on 18 June 2026, that the flow now works only with Gemini Code Assist Standard and Enterprise, and that Google treats Gemini CLI OAuth in third-party software as a policy violation. The README recommends an API key as the lowest-risk option.

  • Vertex stays hidden without a project. If google-vertex models do not appear, set a project in provider settings or one of the project variables.

  • The 1M window on Gemini 4 is shared. Google counts input and output against the same limit.

  • Thinking is billed as output. Gemini 4 Argon and Carbon cost $2 per million input tokens and $10 per million output tokens through 30 November 2026, then $4 and $20.

  • OpenCode 2 reads outside the project without asking. external_directory is allowed by default. To restore the OpenCode 1 behaviour, add { "action": "external_directory", "resource": "*", "effect": "ask" } to permissions.

  • OpenCode Console is a different path. It also offers Gemini models, through OpenCode's own gateway and under OpenCode's privacy terms.

Google also documents an OpenAI-compatible endpoint, and OpenCode's custom-provider recipe can point at it. The built-in provider needs no config, so start there.

What Google does with your prompts

This section covers the API paths OpenCode uses. The Gemini app and Workspace are in our guide Is Gemini safe for work?

Gemini API, free tier

Gemini API, paid tier

Vertex AI

Used to improve Google's products

Yes. Human reviewers may read prompts and responses, disconnected from your account and key

No

No, not without your permission or instruction

Kept for abuse monitoring

55 days

55 days. Flagged content may be reviewed by authorised Google staff

Flagged prompts up to 90 days, only for customers on the online Terms of Service. Gemini 4 Carbon: prompts and responses up to 30 days, stored in your environment

Your own request logs

Not available

On by default for projects created on or after 26 October 2026. Kept up to 55 days; you can choose 7, 14 or 28, or turn logging off

Off by default

Caching

In memory, 24-hour limit

In memory, 24-hour limit

In memory, plus durable caching at rest for up to 24 hours on supported models. Both can be turned off per project

Zero data retention

No

Not guaranteed. Google says to use Vertex AI if you need it

Possible, under the conditions on Google's zero data retention page

For users in the UK, the EEA and Switzerland, Google applies the paid-tier data terms to the free tier as well. Google's terms tell developers not to submit sensitive, confidential or personal information on the free tier.

Settings to change before you open a private repository:

  1. Use a project with billing enabled. The Gemini API counts as a paid service only when the key belongs to a Cloud project with an active billing account.

  2. Decide on logging. In the AI Studio Logs dashboard, turn generateContent logging off or shorten its retention if you do not want stored copies.

  3. Do not share log datasets with Google. A shared dataset is handled under the free-tier terms and may be used for training.

  4. For zero retention, use Vertex. Leave request-response logging off, and turn off durable caching for the project if your policy forbids data at rest. Customers with a Google Cloud Master Agreement are exempt from abuse-monitoring prompt logging by default.

  5. Pick a regional location on Vertex. With the global endpoint, requests and cached data can be served from any region.

The same setup for a team: Gemini as an approved provider

OpenCode has a control for this without any extra product. A policies block can deny every provider and then allow one, and a config file in the system directory (/etc/opencode on Linux) or a macOS managed preference outranks user and project files:

{
"policies": [
{ "action": "provider.use", "resource": "*", "effect": "deny" },
{ "action": "provider.use", "resource": "google-vertex", "effect": "allow" }
]
}

That is OpenCode's documented example with the provider ID changed. It stops OpenCode using any other provider, whatever key a developer adds. It still sends every repository to Google, and it keeps no record of requests.

Aventail™ Connect configures OpenCode to send model requests through Aventail™ Link, the inference engine and gateway on each machine. Routing rules are set once in Aventail™ Control and evaluated on the device. Google Gemini is a provider option in Control's API key vault, and Aventail™ states that it supports Gemini 4 Argon and Carbon.

  1. Install Link on each machine and choose Connect next to OpenCode in Connected apps. Do not add an Aventail™ provider inside OpenCode.

  2. Add the Gemini key to the API key vault (Fleet Admin or Super Admin). It belongs to your organisation's own Google project.

  3. Tag the machines. Rules match devices, so machines that work on sensitive repositories get their own tag and policy.

  4. Write two policies. Sensitive machines permit only On device and serve a local model. Other machines route to the Gemini connection, or use On device first with Gemini as a fallback hop.

  5. Check Requests in Control after a harmless prompt. If no destination is permitted, the request fails with an error.

Routing to hosted providers is in beta. The hosted Control service receives device and routing events, and does not receive prompts or responses. The Gemini connection uses an API key from your own Google account.

Which path to take

OpenCode + Gemini by hand

OpenCode with Aventail™ Connect

Who it suits

One developer, or a team that sends all code to one Google project

A team with an approved Google account and some repositories that must stay local

Setup

/connect and a key, or gcloud and a project

Connect button in Link, rules in Control

Where the key lives

OpenCode's credential store, or the environment

Control's API key vault

Who picks the model

The developer

Chosen in Link, within the routing rule

Local models for sensitive work

A separate provider the developer selects

A local-only policy for tagged machines

Record of requests

AI Studio logs, if enabled

Requests in Control

Cost

Google's token prices

Free for three devices; Starter is £50 a month excluding VAT with ten devices; Google's charges on your own account

Frequently asked questions

Does OpenCode support Gemini?
Yes, natively. OpenCode ships runtime packages for the Gemini API and for Vertex AI, and its catalogue lists the Gemini 4 models and Gemini 3.8 Flash for both.

What is the Gemini model ID in OpenCode?
The provider prefix plus Google's model code: google/gemini-4-argon for the API key path, google-vertex/gemini-4-argon for Vertex. Pick from /models if you are unsure.

Which environment variable does OpenCode read for a Gemini key?
The models.dev catalogue lists GOOGLE_API_KEY, GOOGLE_GENERATIVE_AI_API_KEY and GEMINI_API_KEY for the google provider. Set one on the OpenCode server process, or use /connect.

Can I use my Google AI Pro or Gemini subscription in OpenCode?
Not through a built-in method. The community plugin that did this says Google ended consumer access on 18 June 2026 and treats the OAuth flow in third-party tools as a policy violation. Use an API key.

Is Gemini free in OpenCode?
Gemini 4 Hydrogen and Gemini 3.8 Flash have a free tier with rate limits, and on that tier Google uses your content to improve its products. Gemini 4 Argon and Carbon are paid only.

Should a company use the API key or Vertex AI?
The paid API tier stops training and keeps prompts for 55 days for abuse monitoring. Vertex AI adds a regional location, an exemption from abuse logging for Master Agreement customers, and documented steps to reach zero data retention.

Why do Vertex models not appear in OpenCode?
The provider needs a project it can resolve. Signing in with gcloud is not enough. Set project under providers.google-vertex.settings, or GOOGLE_CLOUD_PROJECT.

Sources

  1. OpenCode, Intro and installation: https://opencode.ai/docs/

  2. OpenCode, Providers: https://opencode.ai/docs/providers/

  3. OpenCode, CLI providers and credentials: https://opencode.ai/docs/cli/providers/

  4. OpenCode, Models: https://opencode.ai/docs/models/

  5. OpenCode, Config: https://opencode.ai/docs/config/

  6. OpenCode, Policies: https://opencode.ai/docs/policies/

  7. OpenCode, Migrate from V1: https://opencode.ai/docs/migrate-v1/

  8. OpenCode, Console models and privacy: https://opencode.ai/docs/zen/

  9. models.dev, provider catalogue used by OpenCode (google and google-vertex entries): https://models.dev/api.json

  10. Google, API keys: https://aistudio.google.com/docs/api-key (ai.google.dev/gemini-api/docs redirects to aistudio.google.com/docs)

  11. Google, Models: https://aistudio.google.com/docs/models and model pages for gemini-4-argon and gemini-3.8-flash

  12. Google, Gemini Developer API pricing: https://aistudio.google.com/docs/pricing

  13. Google, Billing: https://aistudio.google.com/docs/billing

  14. Google, Gemini API Additional Terms of Service (effective 23 March 2026): https://aistudio.google.com/docs/terms

  15. Google, Zero data retention in the Gemini Developer API: https://aistudio.google.com/docs/zdr

  16. Google, Abuse monitoring: https://aistudio.google.com/docs/usage-policies

  17. Google, Logs and datasets: https://aistudio.google.com/docs/logs-datasets and Data logging and sharing: https://aistudio.google.com/docs/logs-policy

  18. Google, OpenAI compatibility: https://aistudio.google.com/docs/openai

  19. Google Cloud, Gemini Enterprise Agent Platform and zero data retention (updated 12 November 2026): https://docs.cloud.google.com/gemini-enterprise-agent-platform/resources/zero-data-retention

  20. Google Cloud, Abuse monitoring: https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/abuse-monitoring

  21. opencode-gemini-auth, README: https://github.com/jenslys/opencode-gemini-auth

  22. Aventail™, Connect overview: https://docs.aventail.co.uk/docs/connect/overview

  23. Aventail™, Routing: https://docs.aventail.co.uk/docs/fleet/routing

  24. Aventail™, OpenCode with Aventail™ Connect: https://aventail.co.uk/guides/opencode-with-aventail-connect

  25. Aventail™, Gemini 4 Argon vs Carbon: https://aventail.co.uk/guides/gemini-4-argon-vs-carbon

  26. Aventail™, Pricing: https://aventail.co.uk/#pricing

All sources were read on 15 November 2026.

We use optional cookies and tracking to identify visiting companies, load our contact form, measure Google Ads download clicks and understand page use with PostHog on our homepage, Connect and How it works pages, Ollama and LM Studio comparison guides and campaign pages. Cookie policy