DEV Community

shiva prasad
shiva prasad

Posted on Originally published at freellmapi.link

Free OpenAI-compatible LLM APIs in 2026: how to switch `base_url` in Python and JS

Free OpenAI-compatible LLM APIs in 2026: how to switch base_url in Python and JS

You do not need a new SDK for every free LLM API. If a provider documents an OpenAI-compatible endpoint, the official Python and JavaScript openai clients will talk to it after you change two things: the base URL and the API key. The model id is a third value, and it has to come from that same provider's docs or dashboard.

This walkthrough uses only figures and endpoints that were published on the providers' own pages, as recorded in the 12 November 2026 snapshot on Free LLM API Tokens. When a quota is not published, I will say so rather than invent one. Always re-check the official page before you plan work around a daily cap; these numbers change.

What "OpenAI compatible" actually promises

Compatibility means the server accepts the same style of HTTP request that OpenAI's API uses — typically a chat.completions call with a model id and a list of messages. It is not a promise that streaming, tool use, vision, batch jobs or embeddings all exist, or that they use the same names.

Trackers that are careful about this will mark a provider as yes, partial, or not published. "Yes" means they document a compatible endpoint. "Partial" means only some of the surface works that way. "Not published" means there is no clear public statement, not that the API is incompatible.

In the 12 November 2026 snapshot, the tracker listed 112 providers. Of those, 105 were marked OpenAI compatible, two were partial, one was not compatible, and four had no public statement recorded. That is a snapshot, not a ranking, and it is not a reason to skip the provider's own docs.

Pick a provider before you write any code

Start with an official provider if you are still learning, or if the prompt might include anything you would not post in public. Official, in the tracker's terms, means a model maker or a cloud that offers the API under its own name. Groq, Google's Gemini API, Cloudflare Workers AI, Mistral, SambaNova and Cerebras are examples in the current list.

A gateway sits in front of one or more models and gives you a single key and a single request format. Some gateways are well documented. Some are unofficial relays. A relay warning on a card means you should not send private data through that site, however large the advertised token cap looks. The 12 November snapshot flagged 47 of 112 entries with that warning, and classified 37 as official and 75 as gateways.

Use the filters for "OpenAI compatible" and "No card needed" on the provider list, then open the official pricing or rate-limit page from the card. If tokens per day are marked "Not published", do not assume a number.

A few published official figures, as of that snapshot:

  • Groq publishes 200,000 tokens per day and 1,000 requests per day on listed models, with no card required. The documented base URL is https://api.groq.com/openai/v1. Limits vary by model.
  • Gemini API (Google AI Studio) has a free tier on selected Gemini models, with per-model RPM and RPD on Google's rate-limit page, and no card required. The documented compatible base URL is https://generativelanguage.googleapis.com/v1beta/openai/. The tracker's notes also record that free-tier prompts may be used to improve Google products.
  • Cloudflare Workers AI publishes 10,000 Neurons per day — that unit is not tokens — and its base URL includes an {ACCOUNT_ID} placeholder that you must replace. Do not treat the placeholder as a hostname.
  • SambaNova Cloud published 200,000 tokens per day per model as of 3 October, with 20 requests per day, and a card required.
  • Cerebras publishes 1,000,000 tokens per day during a $5, 30-day trial, with a card required. That is a trial, not an ongoing no-card tier.

Keep the key out of the source

Store the key in an environment variable and read it at runtime. Do not paste it into a chat, a screenshot, a ticket, or a Git repository. If a key leaks, revoke it on the provider's dashboard and create a new one.

# current shell session only
export API_KEY="paste-the-provider-key-here"
Enter fullscreen mode Exit fullscreen mode

On Windows PowerShell the equivalent is $env:API_KEY = "paste-the-provider-key-here". A .env file is fine only if that file is listed in .gitignore.

Python: construct the official client with base_url

Install the official openai package, then pass base_url and api_key. Replace the base URL with the exact string from the provider's docs (or from the tracker card, if it repeats a published URL). Replace MODEL_ID with a model id copied from that same provider — not from this article, and not from a different catalogue.

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["API_KEY"],
    base_url="https://api.example.com/v1",
)

resp = client.chat.completions.create(
    model="MODEL_ID",
    messages=[{"role": "user", "content": "Reply with the word ok."}],
)
print(resp.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

To point this at Groq, for example, you would set base_url to the published value https://api.groq.com/openai/v1 and copy a model id from Groq's docs. Some base URLs already end in /v1 or /openai/v1. Copy them as published. Do not add a second /v1 unless the official docs say to.

JavaScript: the same two fields, on a server

The official Node SDK uses baseURL (camel case) and apiKey. Do not embed a secret key in a browser bundle; call the provider from a server or a function you control.

import OpenAI from "openai";

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

const resp = await client.chat.completions.create({
  model: "MODEL_ID",
  messages: [{ role: "user", content: "Reply with the word ok." }],
});
console.log(resp.choices[0].message.content);
Enter fullscreen mode Exit fullscreen mode

If you already have application code written for OpenAI, this is often the only change you need, besides picking a model id that exists on the new host.

A raw HTTP request, if you would rather skip the SDK

A compatible server usually accepts a JSON POST to {base URL}/chat/completions with a bearer token. If the published base URL already includes a version suffix, you still append /chat/completions unless the official docs say otherwise.

curl https://api.example.com/v1/chat/completions \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"MODEL_ID","messages":[{"role":"user","content":"Reply with the word ok."}]}'
Enter fullscreen mode Exit fullscreen mode

If this returns an authentication error, the key or the header name may be wrong. If it returns a model error, the model id is usually wrong or not on the free tier. If it returns a rate-limit error, you have hit a published or unpublished cap.

Why this article will not fill in a model id

Model catalogues change. A name that worked last month may be paid-only, renamed, or removed from a free tier. A tracker can record allowances, compatibility and base URLs without pretending to be a live model directory. Official docs and the provider dashboard are the right source for the string you put in the model field.

The longer, step-by-step version of this setup lives in the site's guide on how to use free OpenAI-compatible APIs.

Test without burning the day's allowance

Start with a one-line prompt and a low max_tokens value if the API supports it. Read the dashboard, or any remaining-quota headers, before you run a loop. Free tiers often count both input and output tokens, and a retry loop can exhaust a daily cap in minutes.

The common failures are practical: a typo in the base URL, a second /v1 on a URL that already has one, an unset environment variable, a model id copied from a different provider, or an offer that has ended since the last snapshot.

Once a test request works, keep client construction in one module, log the provider name and model id (never the key), and add basic backoff on HTTP 429. If you outgrow a free tier, switch the base URL, the key and the model id rather than rewriting the rest of the app — that is the point of compatibility.

Top comments (0)