How to test a free LLM API client without spending quota
Yes. Point your Chat Completions client at a local mock server that returns a canned reply. You can check the URL, request JSON, dummy authorization, response parser and known error paths without contacting a provider. This does not verify a real API key, model ID, free-tier limit or provider compatibility; make one deliberate provider smoke test only after the local checks pass.
What this local test proves
A request that reaches only 127.0.0.1 can exercise how your app builds a request and handles a response. It can expose a doubled /v1 path, a missing Authorization header, wrong JSON fields or a parser mismatch. The synthetic 429 branch gives you a controlled error to pass through your own retry handler after you wire in your client.
It cannot tell you whether a provider accepts the exact model name, account, region, key, input size or current quota. The answer below is synthetic. It is not generated by a model.
ChinaGenAI's existing fixture self-test checks request and response diagnosis examples in your browser. That tests the diagnostic tools. The local HTTP example here exercises a reader's own client against a server; the two checks cover different layers.
Run one loopback contract test
Save this as offline-client-test.mjs and run node offline-client-test.mjs. It needs a current Node.js release and no package install. The server chooses an unused port and binds only to 127.0.0.1. It has no provider URL, network forwarding or real credential.
import assert from "node:assert/strict";
import { createServer } from "node:http";
const send = (res, status, payload, extraHeaders = {}) => {
res.writeHead(status, {
"content-type": "application/json",
...extraHeaders,
});
res.end(JSON.stringify(payload));
};
const server = createServer(async (req, res) => {
if (req.method !== "POST" || req.url !== "/v1/chat/completions") {
return send(res, 404, { error: { message: "No mock route" } });
}
let raw = "";
try {
for await (const part of req) raw += part;
} catch {
return send(res, 400, { error: { message: "Could not read request" } });
}
if (req.headers.authorization !== "Bearer local-test-key") {
return send(res, 401, { error: { message: "Wrong mock key" } });
}
let body;
try {
body = JSON.parse(raw);
} catch {
return send(res, 400, { error: { message: "Body must be JSON" } });
}
if (typeof body.model !== "string" || !Array.isArray(body.messages)) {
return send(res, 400, { error: { message: "Expected model and messages" } });
}
if (req.headers["x-test-scenario"] === "429") {
return send(res, 429, { error: { message: "Synthetic rate limit" } }, { "retry-after": "2" });
}
return send(res, 200, {
id: "chatcmpl-local-test",
object: "chat.completion",
created: 0,
model: body.model,
choices: [
{
index: 0,
message: { role: "assistant", content: "MOCK OK" },
finish_reason: "stop",
},
],
usage: { prompt_tokens: 3, completion_tokens: 2, total_tokens: 5 },
});
});
await new Promise((resolve, reject) => {
server.once("error", reject);
server.listen(0, "127.0.0.1", resolve);
});
const address = server.address();
const endpoint = "http://127.0.0.1:" + address.port + "/v1/chat/completions";
async function callMock(options = {}) {
const key = options.key ?? "local-test-key";
const model = Object.prototype.hasOwnProperty.call(options, "model") ? options.model : "fixture-model";
const headers = {
"content-type": "application/json",
authorization: "Bearer " + key,
};
if (options.scenario) headers["x-test-scenario"] = options.scenario;
return fetch(endpoint, {
method: "POST",
headers,
body: JSON.stringify({
model,
messages: [{ role: "user", content: "Return a short test reply." }],
}),
});
}
try {
const success = await callMock();
const successBody = await success.json();
assert.equal(success.status, 200);
assert.equal(successBody.choices[0].message.content, "MOCK OK");
console.log("PASS success response");
const unauthorized = await callMock({ key: "wrong-test-key" });
assert.equal(unauthorized.status, 401);
console.log("PASS authorization error");
const badRequest = await callMock({ model: null });
assert.equal(badRequest.status, 400);
console.log("PASS invalid request");
const limited = await callMock({ scenario: "429" });
assert.equal(limited.status, 429);
assert.equal(limited.headers.get("retry-after"), "2");
console.log("PASS synthetic rate limit and Retry-After");
} finally {
const closed = new Promise((resolve) => server.close(resolve));
server.closeAllConnections?.();
await closed;
}
The same-process harness sends only to its ephemeral loopback port. The local-test-key, fixture-model, reply, request count and five-token usage are examples, not usable credentials or a provider's actual format or limits.
To exercise your app's SDK or wrapper, replace callMock() with your app's function and configure that client to use the printed local endpoint and dummy key. Keep the process pointed at 127.0.0.1. If your client has no configurable base URL or transport, do not assume this harness tested the SDK; add an HTTP stub at the boundary where your app sends requests.
Check the result without reaching a provider
| Local case | Expected result | What it checks |
|---|---|---|
| Valid request | 200 and MOCK OK | URL, JSON serialization and success parsing |
| Wrong dummy key | 401 JSON error | Authorization header and error branch |
| Missing model | 400 JSON error | Local request validation |
| Synthetic rate limit | 429 plus Retry-After: 2 | Whether your code surfaces or deliberately handles a limit response |
The 429 is fabricated by the test server. RFC 9110 defines how a real Retry-After field can contain a delay or date; a local response does not tell you what any provider will return. Keep retries bounded by your job deadline and provider instructions.
A passing loopback check means only that the tested client path handles these examples. It does not confirm live networking, TLS, free quota, key permissions, model availability, streaming, tool calls, JSON mode or a provider's other API surface. For those, use the provider's current official docs and a one-request smoke test with short synthetic input. Disable automatic retries for that live check and record the exact model, HTTP status and timestamp without logging the key or prompt.
Questions developers ask
Does a successful mock prove my free LLM API key works? No. The local server checks your client path; it never sees a provider key. A separate live request is required to test account and model access.
Can I use a Chat Completions mock to test the Responses API or streaming? No. This page covers one non-streaming Chat Completions request and four synthetic cases. Build a separate fixture for each endpoint or stream format your application uses.
Will the mock tell me whether a request consumes quota? No. It makes no provider request at all. Verify the provider's current usage and limit documentation before the single live smoke test.
Sources checked 2026-11-08
- Node.js HTTP API: local server and request handling.
- RFC 9110 ยง10.2.3: meaning of Retry-After.
- OpenAI Java SDK issue #956: a developer-requested offline SDK test client; this issue is a demand signal, not provider behavior.