Gemini CLI
Configure and use AGTCloud in the Gemini CLI.
Gemini CLI is an open-source terminal agent launched by Google, allowing users to understand projects, analyze files, and use commands and tools after obtaining authorization within the terminal. By configuring the Gemini API address, API Key, and model ID, Gemini CLI can use AGTCloud's Gemini Generate Content compatible entry.
This tutorial only covers the basic connection and security verification process of the Gemini CLI. File modification, shell commands, function calls, and other agent capabilities depend on the target model and the actual support of the corresponding model for Gemini Parts, Function Call, and other protocol structures.
First verify basic conversations, then test tool capabilities
Successfully returning ordinary text only means the basic generation link is connected, but does not mean file editing, command execution, or all built-in tools are available. Please keep manual confirmation for the first test and do not enable automatic approval.
Preparation
Before you start, please confirm that you have:
- Install Node.js
20or higher; - Prepare Bash, Zsh, or PowerShell terminals;
- Create the AGTCloud API Key on the API Keys page;
- Copy the model ID from the Model Marketplace which is available to the current API Key and supports Gemini Generate Content;
- Prepare a test directory that does not contain sensitive code and production credentials.
MODEL_ID is the placeholder to be replaced, not the actual model name. Please use the model ID visible with the current API Key that supports Gemini Generate Content.
Install Gemini CLI
It is recommended to install the current stable version. Different installation methods yield the same gemini command.
Using npm for global installation:
npm install -g @google/gemini-cliSuitable for macOS, Linux, WSL, and Windows environments with Node.js 20+ installed.
Check the version after installation:
gemini --versionIf it prompts that the command cannot be found, please open the terminal again and confirm that the npm global command directory has been added to the PATH.
Configure AGTCloud
The Gemini CLI requires four environment variables: API Key, model ID, custom Gemini API address, and Bearer authentication method.
Temporary configuration on the current terminal:
export GEMINI_API_KEY="your-api-key"
export GEMINI_MODEL="MODEL_ID"
export GOOGLE_GEMINI_BASE_URL="https://api.agtcloud.cn"
export GEMINI_API_KEY_AUTH_MECHANISM="bearer"These variables only apply to the current terminal session and need to be reset after closing the terminal.
Configuration item description
| Environmental variables | Purpose |
|---|---|
GEMINI_API_KEY | The AGTCloud API Key created on the API Keys page |
GEMINI_MODEL | A model ID visible to the current API Key that supports Gemini Generate Content |
GOOGLE_GEMINI_BASE_URL | Gemini API address, use https://api.agtcloud.cn |
GEMINI_API_KEY_AUTH_MECHANISM | Set as bearer, send the AGTCloud API Key via the Authorization Header |
Do not add /v1 to the base URL
Set GOOGLE_GEMINI_BASE_URL to https://api.agtcloud.cn exactly as shown. The Gemini SDK appends /v1beta/models/{model}:generateContent automatically. If you use https://api.agtcloud.cn/v1 instead, the final path may incorrectly contain /v1/v1beta/....
This tutorial does not set GOOGLE_GENAI_API_VERSION, so Gemini CLI uses the Gemini API's default v1beta version. Do not put the full generateContent request path in the Base URL.
Save user-level configuration
After confirming that the temporary configuration works, you can save the variables in Gemini CLI's user-level .env file. This file may contain a real API Key, so do not commit it to a code repository.
User-level file path:
~/.gemini/.envFile content:
GEMINI_API_KEY=your-api-key
GEMINI_MODEL=MODEL_ID
GOOGLE_GEMINI_BASE_URL=https://api.agtcloud.cn
GEMINI_API_KEY_AUTH_MECHANISM=bearerGemini CLI searches for environment files in the current working directory and its parent directories. A project-level .env or .gemini/.env file may take precedence over the user-level configuration. If your settings are not taking effect, check these files first.
Fixed API Key Authentication Method
Gemini CLI supports various Google authentication methods. When using through AGTCloud, you should choose Gemini API Key, rather than Google login or Vertex AI.
Merge the following configuration into the user-level settings.json without overwriting the existing theme, permissions, or other settings:
{
"security": {
"auth": {
"selectedType": "gemini-api-key"
}
}
}Configuration file location:
| System | Path |
|---|---|
| macOS / Linux / WSL | ~/.gemini/settings.json |
| Windows | %USERPROFILE%\.gemini\settings.json |
After saving, exit Gemini CLI completely and restart it. selectedType selects API Key authentication, while GEMINI_API_KEY_AUTH_MECHANISM=bearer makes Gemini CLI send the Bearer header required by AGTCloud. Both settings are required.
Choose a model
The default model is determined by GEMINI_MODEL. If you need to temporarily specify a model for a single session, you can run:
gemini --model MODEL_ID/model is an additional way to select a model, but its list may not include every model available to the current AGTCloud API Key. If a model is missing, copy its full ID from the Model Marketplace and specify it with GEMINI_MODEL or the interactive-session --model option.
Verify connection
Perform a minimal generation test
Run in a directory without sensitive files:
gemini -p "Please only reply: AGTCloud connection successful." --output-format jsonIf it can return a JSON containing the response content, it indicates that the Base URL, API Key, Bearer authentication, and model ID have established a basic connection.
Execute read-only file test
Enter the secure test project containing README.md:
gemini -p "@README.md Please summarize the project purpose in three points without modifying any files." --output-format jsonNormal reading and summarizing of the file without any modifications indicates that the basic file analysis chain is usable.
Start interactive session
After passing the basic test, run:
geminiKeep the default manual approval for the first use. Check each file and command operation that Gemini CLI is ready to execute; do not directly enable auto-approval mode.
Frequently Asked Questions
| Issue | What to check | Resolution |
|---|---|---|
Cannot find the gemini command | Node.js version, installation, and PATH | Confirm that Node.js is version 20 or later, reopen the terminal, and run gemini --version |
| Gemini CLI still requests a Google login | security.auth.selectedType | Merge the gemini-api-key setting, then exit and restart Gemini CLI |
| Environment variables do not take effect | Environment files in the current directory, parent directories, and user directory | Check for a higher-priority .env or .gemini/.env file, and do not print the full API Key |
Returns 401 | API Key and Bearer-authentication variables | Use a valid API Key and confirm GEMINI_API_KEY_AUTH_MECHANISM=bearer |
Returns 403 | API Key permissions and model visibility | Confirm that the current API Key can access the configured model and check the account status |
Returns 404 | Base URL, API version, and model ID | Use https://api.agtcloud.cn as the Base URL. Do not append /v1, /v1beta, or a full endpoint |
| Request access to Vertex AI / Google | Conflicting Google environment variables | Clearing or disabling GOOGLE_GENAI_USE_VERTEXAI, GOOGLE_GENAI_USE_GCA, GOOGLE_API_KEY, etc., are not included in this tutorial |
| The model is unavailable | GEMINI_MODEL or --model | Re-copy the full model ID supporting Gemini Generate Content from the Model Marketplace |
| The text is normal, but the tool fails | Model capabilities and Gemini Function Call compatibility | Temporarily limited to normal dialogue and read-only analysis, then verify tool call capabilities based on actual errors |
| Bearer configuration is not available | Gemini CLI version | Update to the current stable version, do not modify the local installation source code, and do not manually write keys into custom headers |
If you encounter issues that cannot be located, record the request time, API Base URL, model ID, Gemini CLI version, HTTP status, and redacted error messages, then review Errors and Troubleshooting. Do not submit the full API Key or .env files containing credentials.
Safety advice
- Do not write real API keys into repositories, share scripts, screenshots, logs, or chat logs;
- It is recommended to first use temporary environment variables for verification, and only save user-level configurations after success;
- Set appropriate local access permissions for
.envfiles and avoid syncing to public cloud drives; - Maintain manual approval when first using tool capabilities and validate them in non-production projects;
- Before sharing troubleshooting information, delete the full Key, username, local path, and business source code.