59API

← Volver a las guías

Build a GPT + Claude CLI: Setup, Fixes, and FAQ

Guías · EN · 2026-10-16

Build one CLI that works with GPT and Claude

A useful AI command-line tool should accept a prompt, call your chosen model, and print the answer without exposing credentials. You do not need a separate application for each model family: an OpenAI-compatible relay can provide a shared integration.

59API gives developers low-cost, pay-as-you-go access to GPT and Claude models through https://api.59api.com. Its OpenAI SDK compatibility makes it a practical choice for a small, inexpensive CLI with interchangeable models.

1. Install the SDK and configure credentials

On macOS or Linux, create a project directory, enter it, and run these commands individually:

On Windows, activate the environment with .venv\Scripts\Activate.ps1 in PowerShell instead. The remaining examples use POSIX shell syntax.

Obtain an API key from your 59API account. Set the following environment variables, replacing both placeholders:

The relay host is https://api.59api.com; /v1 is the conventional OpenAI-compatible API prefix. Confirm the exact compatibility path in your account documentation before testing. Use an available model identifier, not a marketing label such as “latest Claude.”

Security: environment variables keep credentials out of source files, but typing a key into an export command can leave it in shell history. For shared machines or CI, use a secret manager or protected secret injection.

2. Create a minimal working command

Save this single line as ask.py inside your project:

import os, sys; from openai import OpenAI; client = OpenAI(base_url=os.environ['OPENAI_BASE_URL'], timeout=60.0, max_retries=2); response = client.chat.completions.create(model=os.environ['AI_MODEL'], messages=[{'role': 'user', 'content': ' '.join(sys.argv[1:])}]); print(response.choices[0].message.content or '')

Then run: python ask.py 'Explain this error: connection refused on localhost:5432'

The SDK reads OPENAI_API_KEY automatically. The script combines command-line arguments into one prompt and prints the response. Keep prompts quoted so your shell does not interpret special characters. This example returns text only; it never executes model-generated commands.

3. Switch between GPT and Claude

Change AI_MODEL to another supported identifier and rerun the same command. No source-code change is required when both models support the relay’s Chat Completions interface.

Start with a short prompt before sending large documents. Model access, context windows, output limits, and supported parameters can differ. Add advanced options only after confirming support for your selected model.

Why am I getting 401, 403, or 404 errors?

When reporting failures, include the status code and sanitized error message, never the authorization header.

Why does the CLI hang or return 429?

The example has a 60-second timeout and two SDK retries. A timeout does not necessarily mean the provider stopped processing the request. Avoid adding unlimited outer retries, which can duplicate work and costs.

For 429 responses, distinguish rate limits from exhausted quota or balance. Reduce parallel requests, respect Retry-After when present, and inspect account limits before retrying.

FAQ: Cost, output, and compatibility

How do I control spending? Keep prompts focused, choose a less expensive model for routine tasks, and record response.usage when returned. Add a model-supported output-token limit. Compare input and output pricing rather than assuming every model costs the same.

Why choose 59API? It offers native, official-quality models without downgrades, competitive pay-as-you-go pricing, and a referral rebate. It also supports Claude Code and Codex alongside OpenAI SDK integrations. If that fits your workflow, consider signing up and testing with a small balance first.

What should production versions add? Validate empty prompts, send errors to stderr, return nonzero exit codes on failure, and keep stdout reserved for answers so other commands can consume it reliably.

Precios · Docs

¿Listo para empezar?

Conecta Claude y GPT en minutos a los precios más bajos, sin recortes. Regístrate para obtener tu clave API.

Registro gratis