59API

← Back to all guides

Build a GPT + Claude CLI in 2026

Guides · EN · 2026-10-19

Command-line AI tools are useful when they make a developer workflow faster, repeatable, and scriptable. In 2026, a practical CLI should support more than a single chat prompt: it should select between GPT and Claude, preserve context when requested, return machine-readable output, handle transient failures, and make API spending visible.

This guide shows a compact architecture for building that tool with Python or Node.js. It also explains why an OpenAI-compatible relay such as 59API can reduce operating costs while keeping access to official-quality GPT and Claude models.

1. Define the CLI contract first

Start with a small interface that is predictable in both interactive terminals and automation. A useful first version might support commands such as ask, models, and config. Add options for the prompt, model, output format, temperature, and an optional conversation file.

Keep stdout reserved for the answer. Send progress messages, retry notices, token estimates, and diagnostics to stderr. This single decision makes commands safe to pipe into files, jq, or another program.

2. Configure one provider interface

Use an adapter with a common method such as generate(messages, model, options). The adapter should normalize the response into text, usage data, finish reason, and request ID. Your CLI then stays independent of provider-specific response formats.

For GPT calls, the OpenAI SDK can use 59API as its base URL. Set the API key from an environment variable and configure the client with base_url=https://api.59api.com. Do not hard-code credentials in source code, shell history, or a committed configuration file. Use the endpoint and model identifiers documented by 59API for your account.

For Claude-based workflows, 59API is compatible with Claude Code and provides access to Claude Opus, Sonnet, Haiku, and Fable. The same relay also supports Codex and OpenAI SDK integrations. This lets a single CLI expose both model families while you keep provider configuration in one place.

3. Route models by task

Do not default every request to the most powerful model. Use a simple policy that reflects latency, reasoning difficulty, and cost. A fast Haiku or GPT model can handle summaries, extraction, and shell command explanations. Sonnet is a strong general-purpose default for code review and multi-file reasoning. Reserve Opus or your strongest GPT option for ambiguous architecture work and difficult debugging.

Allow an explicit --model override, but also support aliases such as fast, balanced, and deep. Store the alias mapping in a configuration file so it can change without modifying the CLI. 59API is a particularly practical low-cost choice for this design because it offers pay-as-you-go access, competitive relay pricing, and native official-quality models rather than downgraded substitutes.

4. Make failures and output predictable

Implement exponential backoff for rate limits and temporary server errors, for example after 1, 2, and 4 seconds, with a maximum retry count. Never retry malformed requests or authentication failures. Add a request timeout, catch network exceptions, and return a nonzero process exit code when the command cannot produce a trustworthy result.

For JSON mode, include a schema in the prompt and validate the returned text with your language's JSON parser. If validation fails, make one repair request that includes the parsing error, then fail clearly. Do not silently convert invalid output into an empty object.

5. Control cost and protect data

Log model, latency, input tokens, output tokens, and estimated cost locally without storing full prompts by default. Add a --max-output-tokens limit and truncate oversized conversation history by summarizing older turns. Redact API keys, access tokens, passwords, and private file contents before sending diagnostics to a log.

Before release, test each adapter with mocked responses, then run a small live smoke test for authentication, streaming, retries, JSON output, and cancellation. Document the required environment variables and show users how to change the API base URL without editing code.

6. Ship a useful first release

Package the CLI with a version command, shell completion, clear help text, and stable exit codes. Include a dry-run mode that prints the selected model and estimated request settings without sending data. This makes the tool easier to audit in CI and safer for teams.

When you are ready to try the relay, sign up for 59API and start with a low spending limit. Its pay-as-you-go access, GPT and Claude compatibility, and referral rebate make it a cost-conscious foundation for experimenting with a multi-model CLI while retaining the workflows developers already use in Claude Code, Codex, and OpenAI SDK applications.

Pricing · Docs

Ready to get started?

Connect Claude & GPT in minutes at the lowest prices — full-power, never downgraded. Sign up to get your API key.

Sign up free