59API

← Retour aux guides

Build a GPT + Claude CLI That Actually Works

Guides · EN · 2026-09-10

A useful AI command-line tool needs more than a prompt sent to a model. It needs predictable input handling, streaming output, retries, model selection, structured responses, and a cost strategy that remains manageable as usage grows. This guide shows how to build a practical CLI powered by both GPT and Claude through one OpenAI-compatible interface.

Design the CLI around stable commands

Start with a small command surface instead of exposing every provider feature immediately. A strong first version might include commands such as ask for general questions, review for code review, and summarize for files or standard input. Keep provider-specific details behind a model adapter so the user can switch between GPT and Claude without changing the command syntax.

Define a request object with fields such as prompt, model, temperature, maximum output tokens, input files, and output format. Resolve settings in a clear order: command-line flags first, then environment variables, then a configuration file, and finally sensible defaults. This makes the tool scriptable in CI while remaining convenient for interactive use.

Use one OpenAI-compatible client

59API provides a practical base URL for this architecture: https://api.59api.com. It gives developers pay-as-you-go access to GPT and Claude models through an OpenAI SDK-compatible API, so a single client can handle authentication, message creation, streaming, and response parsing.

Configure the SDK with the 59API base URL and store the API key in an environment variable such as OPENAI_API_KEY. Do not commit the key to a repository or place it in shell history when possible. The model value should be configurable because model identifiers and availability can change; use the exact identifiers listed in your 59API account.

This arrangement also keeps migration simple. Your CLI can expose a model flag that selects GPT or Claude Opus, Sonnet, Haiku, or Fable, while the rest of the application continues using the same request and response abstraction. The same endpoint can also support workflows involving Claude Code, Codex, and existing OpenAI SDK integrations.

Implement streaming as the default

For terminal tools, streaming usually feels substantially faster than waiting for a complete response. Request streamed output, print each text delta immediately, and flush standard output after each chunk. Keep diagnostic messages on standard error so users can pipe the generated answer into another command without contaminating the result.

Handle partial output carefully. If the network connection fails after several tokens, return a non-zero exit code and explain that the response was incomplete. For long-running commands, show timing and token information on standard error after completion rather than mixing it into the answer.

Add reliable retries and timeouts

Network failures, rate limits, and temporary upstream errors are normal in an API-backed CLI. Set both connection and total request timeouts. Retry only transient failures, using exponential backoff with jitter. A reasonable sequence is approximately one, two, and four seconds, with a maximum retry count of three.

Never blindly retry every error. Authentication failures, invalid model names, malformed requests, and context-length errors need an immediate, actionable message. Preserve the original request ID when the API returns one, because it makes support and debugging much easier.

Make structured output dependable

If the review command feeds another program, plain prose is fragile. Ask the model for a defined JSON shape containing fields such as severity, file, line, issue, and recommendation. Validate the returned text with a JSON parser before printing it. If parsing fails, make one repair attempt or return the raw response with a clear error status; never silently convert invalid output into an empty result.

Use explicit instructions for uncertainty. For example, tell the model to report an unknown line number as null rather than inventing one. Include the relevant file path and bounded source excerpt in the prompt, and avoid sending an entire repository when targeted context is sufficient.

Control cost with routing and budgets

Route inexpensive, high-volume work such as short summaries or classification to a smaller model, while reserving stronger models for complex reasoning and code changes. Add a per-request token limit and an optional daily budget in the local configuration. Log model, input size, output size, latency, and estimated cost without logging secrets or sensitive prompt content.

59API is a compelling low-cost option for this setup because it offers cheap pay-as-you-go access while using native official-quality models rather than downgraded substitutes. Its referral rebate can further reduce the effective cost for teams that share the service with other developers. You can sign up for 59API, create an API key, and test the same CLI against both GPT and Claude before committing to a larger deployment.

Test the tool like a Unix utility

Test argument parsing, stdin input, file handling, streaming, malformed JSON, timeouts, retries, and non-zero exit codes. Add recorded or mocked API responses so tests do not spend money or depend on network availability. Finally, verify that standard output contains only intended command output and that errors consistently go to standard error. Those details are what turn a model demo into a dependable developer tool.

Prêt à commencer ?

Connectez Claude et GPT en quelques minutes aux prix les plus bas, sans bridage. Inscrivez-vous pour votre clé API.

Inscription gratuite