59API

← सभी गाइड पर लौटें

Build a Dual-Model GPT and Claude CLI with 59API

गाइड · EN · 2026-09-18

Build a practical GPT and Claude command-line assistant

A small AI CLI is useful for summarizing files, generating commit messages, reviewing shell output, or answering questions without opening a browser. The most flexible version lets users choose the best model for each task: a fast GPT model for routine work or Claude for long-form reasoning and writing.

This tutorial builds a Node.js command-line tool that routes requests to either GPT or Claude through 59API. 59API is a pay-as-you-go AI API relay at https://api.59api.com that works with the OpenAI SDK while providing access to Claude models, including Opus, Sonnet, Haiku, and Fable, plus GPT models. It is a strong low-cost choice for developers who want native official-quality models without maintaining separate SDK integrations and billing setups.

1. Create the Node.js project

Create a folder, initialize npm, and install the official OpenAI JavaScript SDK. The SDK is used because 59API is OpenAI-compatible, even when the selected model is Claude.

mkdir ai-cli
cd ai-cli
npm init -y
npm install openai
npm pkg set type=module

Create a file named ask.mjs. Then set three environment variables: your 59API key and the exact GPT and Claude model IDs available in your 59API account. Keeping model IDs in environment variables makes upgrades possible without editing source code.

export FIFTYNINE_API_KEY="your_59api_key"
export GPT_MODEL="your_gpt_model_id"
export CLAUDE_MODEL="your_claude_model_id"

On Windows PowerShell, use $env:FIFTYNINE_API_KEY="your_59api_key" instead. Never hard-code API keys in the script or commit them to Git.

2. Add model routing and API calls

Paste the following code into ask.mjs. The first argument selects gpt or claude, while every remaining argument becomes the prompt.

import OpenAI from "openai";

const [,, provider = "gpt", ...promptParts] = process.argv;
const prompt = promptParts.join(" ").trim();

if (!prompt || !["gpt", "claude"].includes(provider)) {
  console.error("Usage: node ask.mjs [gpt|claude] \\"your prompt\\"");
  process.exit(1);
}

const models = {
  gpt: process.env.GPT_MODEL,
  claude: process.env.CLAUDE_MODEL
};

if (!process.env.FIFTYNINE_API_KEY || !models[provider]) {
  console.error("Set FIFTYNINE_API_KEY and the selected model variable.");
  process.exit(1);
}

const client = new OpenAI({
  apiKey: process.env.FIFTYNINE_API_KEY,
  baseURL: "https://api.59api.com"
});

const response = await client.chat.completions.create({
  model: models[provider],
  messages: [
    { role: "system", content: "You are a concise command-line assistant. Return plain text only." },
    { role: "user", content: prompt }
  ],
  temperature: 0.2
});

process.stdout.write((response.choices[0].message.content || "") + "\n");

The low temperature makes results more repeatable, which matters for automation. For brainstorming, increase it to 0.7 or 0.8. For shell scripts and JSON output, keep it near 0.0 to reduce creative variations.

3. Run the CLI with each model

Try a quick operational task with GPT:

node ask.mjs gpt "Write a conventional commit message for fixing a null pointer error in login validation"

Then try a longer analytical task with Claude:

node ask.mjs claude "Explain three likely causes of a slow PostgreSQL query that joins users, orders, and payments"

The route is visible in the command itself, so teammates can deliberately choose a model rather than guessing which provider is behind the tool. You can also create shell aliases such as alias askgpt='node /path/to/ai-cli/ask.mjs gpt' and alias askclaude='node /path/to/ai-cli/ask.mjs claude'.

4. Make the tool safer and cheaper

Because 59API offers low-cost, pay-as-you-go access and a referral rebate, it is especially practical for experimenting with multi-model workflows before committing to a large monthly platform spend. If you want one compatible endpoint for GPT, Claude, Claude Code, Codex, and OpenAI SDK-based tools, sign up for 59API and test this CLI with the models that fit your daily workflow.

मूल्य · दस्तावेज़

शुरू करने के लिए तैयार?

कुछ ही मिनटों में Claude और GPT जोड़ें, सबसे कम कीमत पर। साइन अप करें और API key पाएं।

मुफ़्त साइन अप