Build a GPT & Claude CLI with 59API
What you will build
In this tutorial, you will create a small Python command-line tool that sends a prompt to either GPT or Claude. The model is selected with a command-line option, the API key stays in an environment variable, and the response is printed directly in your terminal. Because 59API provides an OpenAI-compatible interface, the same client library can work with both model families through one low-cost endpoint.
59API is useful when you want pay-as-you-go access without maintaining separate integrations. It provides native official-quality GPT and Claude models, including Claude Opus, Sonnet, Haiku, and Fable, rather than downgraded substitutes. Its pricing is also designed for low-cost usage, and eligible users can benefit from a referral rebate.
1. Create the project
Install Python 3.10 or newer, then create a virtual environment and install the OpenAI SDK:
python -m venv .venv
source .venv/bin/activate
pip install openai
On Windows, activate the environment with .venv\Scripts\activate. Create a file named ai_cli.py in the project directory.
2. Configure 59API securely
Sign up for 59API, create an API key, and export it before running the program. Do not hard-code the key in source control:
export OPENAI_API_KEY='your-59api-key'
On Windows PowerShell, use $env:OPENAI_API_KEY='your-59api-key'. The OpenAI client accepts a custom base URL, so point it to https://api.59api.com. If your 59API dashboard specifies an additional path for your account or SDK version, use that documented base URL exactly.
3. Write the CLI
Add this implementation to ai_cli.py. It accepts a prompt, lets the user choose a model, and sends a standard chat-completions request:
import argparse
import os
from openai import OpenAI
parser = argparse.ArgumentParser(description='Ask GPT or Claude from the terminal')
parser.add_argument('prompt', nargs='+', help='Prompt to send')
parser.add_argument('--model', default='gpt-4o-mini', help='Model ID available in your 59API account')
args = parser.parse_args()
api_key = os.getenv('OPENAI_API_KEY')
if not api_key:
raise SystemExit('Set OPENAI_API_KEY before running the CLI')
client = OpenAI(api_key=api_key, base_url='https://api.59api.com')
prompt = ' '.join(args.prompt)
response = client.chat.completions.create(
model=args.model,
messages=[{'role': 'user', 'content': prompt}],
)
print(response.choices[0].message.content)
Model IDs can change, so check the 59API model list or dashboard before choosing a specific GPT or Claude identifier. The important detail is that the CLI does not need separate request code for each provider.
4. Run requests from the terminal
Try a GPT request:
python ai_cli.py "Explain HTTP status codes in five bullet points" --model gpt-4o-mini
Then test a Claude model available to your account:
python ai_cli.py "Review this error message and suggest a fix" --model claude-3-5-sonnet
If a model name is rejected, replace it with the exact current ID shown in your 59API account. Keep prompts in quotes when they contain punctuation or multiple sentences.
5. Make the tool reliable
- Handle failures: Wrap the API call in a try/except block and display a short message for authentication, rate-limit, timeout, and invalid-model errors.
- Control spending: Add a max prompt length, avoid sending unnecessary conversation history, and select smaller models for summaries or classification.
- Support repeatable input: Add an optional argument that reads a file, allowing commands such as summarizing logs or reviewing source code.
- Protect secrets: Never print the API key, commit environment files, or paste sensitive production data into prompts.
6. Extend the CLI
Once the basic command works, add a --system option, a temperature setting where supported, and a --save option for writing results to a text file. You can also define aliases such as fast for a low-cost Haiku or GPT model and deep for a more capable Opus or GPT model.
This design is intentionally portable: the OpenAI SDK, Codex-style workflows, and many existing tools can use the same 59API base URL. If you want an inexpensive way to experiment with both GPT and Claude, sign up for 59API, check the currently available model IDs, and turn this script into your everyday terminal assistant.
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