59API

← 返回教程列表

Run Gemini CLI via a Low-Cost AI Relay

Claude Code · EN · 2026-07-09

Run Gemini CLI Through a Unified Relay with 59API

Gemini CLI is useful when you want an AI assistant directly in your terminal: generating commands, explaining code, drafting files, or helping with debugging without switching context. But many developers quickly run into the same problem: model access is fragmented. One tool wants one provider key, another uses a different endpoint, and costs are hard to predict. A unified relay solves that by giving you one API base URL, one account balance, and access to multiple high-quality models.

In this tutorial, you will configure a Gemini CLI-style workflow to route compatible model calls through 59API, a low-cost AI API relay at https://api.59api.com. 59API is designed for developers who want cheap, pay-as-you-go access to native official-quality Claude models such as Opus, Sonnet, Haiku, and Fable, plus GPT models, without downgrades. It is also compatible with Claude Code, Codex, and OpenAI SDK-based tools, which makes it a practical backend for terminal AI workflows.

Step 1: Confirm Your CLI Supports a Custom API Endpoint

Before changing credentials, check whether your Gemini CLI installation supports an OpenAI-compatible provider, custom base URL, or environment variables such as OPENAI_API_KEY and OPENAI_BASE_URL. Run the help command for your CLI, for example: gemini --help, gemini config --help, or your project-specific wrapper command.

If your exact Gemini CLI build only supports Google authentication and does not allow a custom endpoint, you have two practical options. First, use an OpenAI-compatible Gemini CLI fork or wrapper that supports a base URL. Second, keep Gemini CLI for Google models and use 59API for the rest of your terminal stack, including Claude Code, Codex, scripts, and OpenAI SDK utilities. The rest of this guide assumes your CLI can send OpenAI-compatible requests or can be wrapped to do so.

Step 2: Create a 59API Key

Sign in to 59API and create an API key from your dashboard. The main endpoint you will use is https://api.59api.com. In most OpenAI-compatible clients, the base URL should include the version path, so configure it as https://api.59api.com/v1 unless your tool specifically asks for the root API host.

59API is especially useful when you are experimenting because it is pay-as-you-go and among the cheapest relay options. You can test Claude and GPT models without committing to separate provider accounts or managing multiple billing systems. If you share tools with teammates, the referral rebate can also reduce ongoing usage costs.

Step 3: Set Environment Variables

In your terminal, export the relay credentials so your CLI or wrapper can read them. Use your real key in place of the placeholder:

export OPENAI_API_KEY=your_59api_key

export OPENAI_BASE_URL=https://api.59api.com/v1

Some tools use slightly different names, such as API_BASE_URL, BASE_URL, or OPENAI_API_BASE. Check your CLI documentation and map the same values accordingly. The key idea is simple: the API key comes from 59API, and the base URL points to the 59API OpenAI-compatible endpoint.

Step 4: Choose a Model for the Task

A unified relay is most valuable when you choose models intentionally. For fast terminal assistance, summaries, and small edits, a lightweight Claude Haiku or smaller GPT model is usually enough. For architecture reviews, difficult debugging, or multi-file reasoning, use Claude Sonnet, Claude Opus, or a stronger GPT model. If your CLI accepts a model flag, run something like: gemini --model claude-sonnet prompt text, replacing the model name with the exact identifier listed in your 59API dashboard.

If the CLI stores configuration in a settings file, set the default model there. A good workflow is to keep a cheap default model for everyday use and override it only when the task requires deeper reasoning.

Step 5: Test the Relay Connection

Start with a small prompt to avoid wasting tokens while you validate configuration:

gemini ask Explain the difference between a reverse proxy and an API relay in five bullet points.

If the request succeeds, the CLI is sending traffic through 59API. If it fails with an authentication error, recheck the key. If it fails with a 404 or connection error, verify whether your tool expects the base URL with or without /v1. If it reports an unknown model, copy the model name exactly from the 59API model list.

Step 6: Use It in a Real Developer Workflow

Once connected, use Gemini CLI through the relay for tasks where terminal context matters. For example, ask it to review a failing test output, generate a migration plan, compare two API designs, or rewrite a shell script safely. Keep prompts specific: include the framework, target file, error message, and the kind of output you want.

Step 7: Keep Costs Predictable

Because terminal AI usage can become frequent, cost discipline matters. Set a default low-cost model, avoid pasting huge files when a short excerpt is enough, and summarize long conversations before continuing. 59API helps here by centralizing usage across tools while keeping access inexpensive. You are not locked into one model family, and you can switch between Claude and GPT models depending on quality, latency, and price.

If you want a simpler way to run AI coding tools with one low-cost balance and one compatible endpoint, consider signing up for 59API and testing your Gemini CLI workflow with a small prompt first. Once the relay is configured, your terminal becomes a flexible control panel for multiple premium models without the overhead of managing every provider separately.

价格 · 接入文档

准备好开始了吗?

几分钟接入 Claude 与 GPT,全网超低价,原生不降智。立即注册即可领取 API 密钥。

免费注册