Trả lời nhanh
Làm sao để gọi LLMwithCrypto từ Node.js?
Cài gói chính thức bằng npm install openai, sau đó tạo client với baseURL: "https://llmwithcrypto.com/api/v1" và apiKey: process.env.LLMWITHCRYPTO_API_KEY. LLMwithCrypto trả lời client.chat.completions.create() theo định dạng OpenAI cho 12 model open-weight, có streaming và tool call ở những model hỗ trợ, theo tài liệu ngày 23 tháng 10 năm 2026.
Bạn cần gì
| Node.js | 18 trở lên, ES module (.mjs hoặc "type": "module") |
|---|---|
| Gói | openai (chính thức) |
| Base URL | https://llmwithcrypto.com/api/v1 |
| Key | Trong biến môi trường LLMWITHCRYPTO_API_KEY, không bao giờ trong code hay trình duyệt |
Làm sao để gửi một truy vấn?
Tạo client một lần lúc khởi động và dùng lại ở mọi nơi. Giữ khóa trên server: khóa đặt trong code trình duyệt thì ai mở trang cũng đọc được, nên hãy gọi API từ backend và chỉ gửi kết quả về trình duyệt.
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://llmwithcrypto.com/api/v1',
apiKey: process.env.LLMWITHCRYPTO_API_KEY,
});
const resp = await client.chat.completions.create({
model: 'deepseek-v4-pro',
messages: [
{ role: 'system', content: 'Answer in two sentences.' },
{ role: 'user', content: 'What is a context window?' },
],
max_tokens: 200,
});
console.log(resp.choices[0].message.content);
console.log(resp.usage.prompt_tokens, 'in,', resp.usage.completion_tokens, 'out');
Làm sao chạy batch mà không chạm rate limit?
Chạy một số truy vấn cố định cùng lúc. Helper mapLimit nhỏ bên dưới giữ bốn truy vấn đang chạy và trả kết quả theo đúng thứ tự; không cần thêm package nào. Mỗi tài khoản có giới hạn request và token mỗi phút, hiển thị trong dashboard; liên hệ support để được nâng lên.
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://llmwithcrypto.com/api/v1',
apiKey: process.env.LLMWITHCRYPTO_API_KEY,
});
async function translate(text) {
const resp = await client.chat.completions.create({
model: 'qwen3-235b-a22b-instruct-2507',
messages: [
{ role: 'system', content: 'Translate into Spanish. Reply with the translation only.' },
{ role: 'user', content: text },
],
max_tokens: 200,
});
return resp.choices[0].message.content;
}
// at most 4 requests in flight
async function mapLimit(items, limit, fn) {
const out = new Array(items.length);
let next = 0;
async function worker() {
while (next < items.length) {
const i = next++;
out[i] = await fn(items[i]);
}
}
await Promise.all(Array.from({ length: limit }, worker));
return out;
}
const lines = ['Your order has shipped.', 'Your payment was received.', 'Your key was revoked.'];
console.log(await mapLimit(lines, 4, translate));
Làm sao stream câu trả lời?
Truyền stream: true và dùng for await trên kết quả. Mỗi chunk mang một đoạn text trong choices[0].delta.content; với stream_options: { include_usage: true }, chunk cuối có số token.
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://llmwithcrypto.com/api/v1',
apiKey: process.env.LLMWITHCRYPTO_API_KEY,
});
const stream = await client.chat.completions.create({
model: 'deepseek-v4.1-flash',
messages: [{ role: 'user', content: 'Write a haiku about latency.' }],
stream: true,
stream_options: { include_usage: true },
max_tokens: 120,
});
for await (const chunk of stream) {
const text = chunk.choices[0]?.delta?.content;
if (text) process.stdout.write(text);
if (chunk.usage) console.log(`\n${chunk.usage.prompt_tokens} in, ${chunk.usage.completion_tokens} out`);
}
Làm sao đọc số credit còn lại?
Endpoint credits không nằm trong OpenAI SDK, nên hãy gọi bằng fetch. Miễn phí; gọi trước một batch lớn để chắc số credit đủ dùng.
const res = await fetch('https://llmwithcrypto.com/api/v1/credits', {
headers: { Authorization: `Bearer ${process.env.LLMWITHCRYPTO_API_KEY}` },
});
const credits = await res.json();
console.log('credit left:', credits.balance_usd, 'USD');
Làm sao xử lý lỗi?
SDK ném OpenAI.APIError cho mọi lỗi HTTP, với status trong err.status và code của body trong err.code. Kiểm tra code, không kiểm tra message: insufficient_balance nghĩa là cần nạp thêm, model_not_found nghĩa là sai id, rate_limit_exceeded nghĩa là chờ rồi thử lại.
SDK đã tự thử lại một số lỗi hai lần kèm khoảng dừng (maxRetries). Với batch dài, giữ mặc định đó, giới hạn concurrency như trên, và ghi log những lần thất bại mà bạn bỏ. Trang errors liệt kê mọi status và status nào đáng thử lại. Các lần gọi thất bại không tốn gì: Bạn trả tiền theo token từ số dư của mình: token đầu vào và đầu ra được đếm bằng tokenizer của chính model và tính theo giá của model đó trên 1M token. Một lần gọi kết thúc bằng lỗi thì không mất phí.
Làm sao chuyển một app OpenAI đang có sang?
Đổi tùy chọn client, baseURL và apiKey, rồi thay mọi tên model gpt-… bằng id từ trang models. Prompt, code streaming và phần parse choices và usage giữ nguyên. Chạy test trên model mới trước khi chuyển traffic: tool calling và JSON output tuân theo những gì mỗi model hỗ trợ.
Node.js: các câu hỏi thường gặp
Tôi có thể gọi API từ trình duyệt không?
Về mặt kỹ thuật thì có, nhưng không bao giờ với khóa nằm trong trang. Hãy gọi từ server, hoặc từ một serverless function giữ khóa.
Vercel AI SDK có dùng được không?
Thư viện nào chấp nhận base URL tương thích OpenAI đều dùng được. API dùng đúng định dạng chat completions của OpenAI: trỏ OpenAI SDK, LangChain hoặc bất kỳ công cụ tương thích OpenAI nào vào base URL của chúng tôi với khóa của bạn, và đổi tên model.
Điều gì xảy ra khi hết credit?
Các lần gọi trả về 402 insufficient_balance và không tốn gì. Trang errors hướng dẫn cách xử lý.