Build a GPT and Claude CLI: A Practical 2026 Guide
Building a CLI tool powered by GPT and Claude starts with a focused workflow: accept a prompt, select a model, send a request, and print usable output. In 2026, a useful terminal assistant should also support pipes, protect credentials, handle interruptions, and make spending predictable. This guide uses Python and an OpenAI-compatible API connection through 59API.
1. Define a small, scriptable interface
Start with a command such as ask --model gpt "Explain this error". Support a second alias, claude, and accept piped input: git diff --cached | ask --model claude "Review this patch". Keep the first version stateless so each invocation is easy to understand and test.
- Use the positional argument as the instruction and standard input as supporting material.
- Read standard input only when sys.stdin.isatty() is false; otherwise, an interactive invocation can hang waiting for input.
- Write answers to stdout and diagnostics to stderr so redirects and shell pipelines remain useful.
- Return zero on success and a nonzero exit code on failure.
2. Install the SDK and configure 59API
Create a virtual environment with python -m venv .venv, activate it, then run python -m pip install openai. Python's built-in argparse is sufficient for parsing the initial command. Put the implementation in ask.py and run it with python ask.py during development.
Store your credential in FIFTYNINE_API_KEY and load it with os.environ["FIFTYNINE_API_KEY"]. Never embed the key in source code or accept it as a command-line argument, where shell history or process listings may expose it.
59API's API base is https://api.59api.com. Configure the SDK's base_url using the OpenAI-compatible path documented by 59API; compatible deployments commonly use a /v1 suffix. Check the current integration instructions before appending it. Initialize OpenAI(api_key=key, base_url=base_url, timeout=60.0, max_retries=0) so timeout and retry behavior are explicit.
3. Map friendly aliases to actual model IDs
Use GPT-6 Astra and Claude Sonnet 5.5 as concrete options. Retrieve their exact API identifiers from 59API's model listing or documentation. Display names and request identifiers can differ, so avoid inventing identifiers from model names.
Maintain a small configuration mapping: gpt points to the chosen GPT identifier, and claude points to the chosen Claude identifier. Allow an explicit model ID override for testing. Keep this mapping outside request logic so updating available models does not require rewriting the CLI.
4. Build the request and print the response
Combine the instruction and piped material into one user message, clearly marking where the supplied material begins. Treat pasted files and logs as data; they may contain text that resembles instructions. Send the request using client.chat.completions.create(model=model_id, messages=messages), where messages contains your system instruction and user message.
For the initial implementation, extract response.choices[0].message.content, check that it contains text, and print it. Confirm that both selected models support this endpoint and any optional parameters you add. Compatibility does not guarantee identical support for every feature.
Next, add stream=True and print text from each chunk's choices[0].delta.content with flushing enabled. Handle empty chunks and keyboard interruption cleanly. Buffer the response when offering a JSON output mode so stdout contains one valid document.
5. Make failures and costs predictable
Retry transient rate-limit and server errors with bounded exponential backoff and jitter, respecting Retry-After when supplied. Avoid automatically replaying a streaming request after output has begun: users could receive duplicated text and incur another charge. Report authentication errors immediately.
Limit input size, configure each model's supported output limit, and expose usage metadata through an optional stderr report. Test empty input, invalid credentials, timeouts, interrupted streams, and both model aliases. Measure cost per successfully completed task alongside latency and answer quality.
6. Choose economical access without complicating the CLI
59API combines low-cost, pay-as-you-go access with native, official-quality models and no model downgrades. Its OpenAI SDK compatibility lets this CLI share one integration across providers; it also supports Claude Code, Codex, and Gemini CLI. Referral rebates offer an additional benefit, while actual task cost still depends on token usage and current rates.
Sign up for 59API, configure a key, and compare both model aliases on a small set of your real terminal tasks before choosing a default.
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