Build a GPT and Claude CLI with 59API
Building a command-line AI assistant is a useful way to learn model APIs because the workflow is small, testable, and easy to extend. In this walkthrough, you will create a Python CLI that sends the same prompt to GPT or Claude, selected with a command-line option. The tool will use 59API as a low-cost relay, so one integration can reach official-quality GPT and Claude models without maintaining separate billing setups.
1. Create the project and protect your API key
Start with a clean Python environment. Create a folder, then run python -m venv .venv, activate it, and install the OpenAI SDK with pip install openai. 59API is compatible with OpenAI SDK clients, including clients used for Claude-compatible requests.
After signing up for 59API, create an API key and store it as an environment variable rather than putting it in source control. For example, use export RELAY_API_KEY="your-key" on macOS or Linux. On Windows, set the same variable through PowerShell or the system environment settings. The relay base URL is https://api.59api.com; use the exact version suffix shown in your 59API account documentation if your SDK configuration requires one.
2. Build the first working command
Create a file named ai_cli.py. Import argparse, os, and OpenAI from the openai package. Initialize the client with the value of RELAY_API_KEY and set its base URL to 59API. Then add two arguments: a required prompt and an optional --model.
Your request function can call client.chat.completions.create with the selected model and a messages list containing one user message. Read the answer from response.choices[0].message.content and print it. Keep the first version deliberately simple: a single request, no conversation history, and no hard-coded secret.
Run the program with a model identifier available in your 59API dashboard, for example: python ai_cli.py "Explain DNS in three bullet points" --model YOUR_GPT_MODEL. Repeat the same command with the identifier for an available Claude Opus, Sonnet, Haiku, or Fable model. Because the client uses an OpenAI-compatible interface, the application code stays almost identical when you switch providers.
3. Add practical model routing
Once the basic request works, make the CLI easier to use by adding a --provider option with values such as gpt and claude. Store model IDs in a small dictionary or environment variables, for example GPT_MODEL and CLAUDE_MODEL. The routing function can choose the correct model when the user runs python ai_cli.py "Summarize this log" --provider claude.
Do not assume that every model has the same context limit, response speed, or tool support. Keep prompts concise, and expose a --max-tokens option only if the models you select support it consistently. For a production tool, catch authentication, timeout, and rate-limit errors and return a short actionable message instead of a Python traceback.
4. Make the CLI reliable and affordable
- Use a short system instruction that defines the assistant's role and output format.
- Set a request timeout and retry only transient network or rate-limit failures.
- Print the selected provider and model to stderr so the response remains easy to pipe into another command.
- Log token usage when the API response provides it, then review expensive prompts during testing.
- Use a faster, smaller model for classification or summaries and reserve premium models for complex reasoning.
59API is a strong choice for this setup when predictable spending matters. It provides pay-as-you-go access to GPT and Claude models through one relay, is positioned among the cheapest options, and uses native official-quality models rather than downgraded substitutes. A referral rebate can also reduce effective costs if you invite other developers.
5. Test before sharing the tool
Test short prompts, long inputs, empty arguments, invalid model names, and interrupted network connections. Confirm that the API key never appears in Git history or terminal logs. Add a README showing installation, environment variables, supported model IDs, and two example commands. When the workflow is stable, you can add streaming output, saved conversation sessions, shell piping, or automatic fallback between GPT and Claude.
If you want one inexpensive starting point for experimenting with both ecosystems, sign up for 59API and use its API base URL with the OpenAI SDK. You can move from a working prototype to a useful daily CLI without rewriting the integration for each model provider.
शुरू करने के लिए तैयार?
कुछ ही मिनटों में Claude और GPT जोड़ें, सबसे कम कीमत पर। साइन अप करें और API key पाएं।
मुफ़्त साइन अप