Blog · 2026-07-11

How to Call a Free LLM API in Python (OpenAI SDK)

If you want a free LLM API Python developers can use in minutes, this guide walks you through it end to end. DreamPrompting gives you an OpenAI-compatible gateway at https://dreamprompting.com/api/v1 with automatic failover across providers and a free API key you can create in seconds at /account. You point the official OpenAI SDK at a different base URL, pass that key, send a chat request, and read the response. That is the entire setup.

Because the gateway speaks the OpenAI protocol, you can reuse code and habits you already know. The same call that hits chat.completions.create works here, streaming works with stream=True, and if you would rather avoid the SDK entirely, a raw requests call to /api/v1/chat/completions does the job. Let us get a working example running.

Install the OpenAI package

Start by installing the official Python client. This is the only dependency you need for the SDK examples below.

pip install openai

You do not need a billing setup or a credit card, only a free API key. Sign in with Google at /account, copy the key, and pass it wherever the SDK asks for an API key. Rate limits are 100 requests per minute plus a rolling 24 hour account quota of 5,000 requests and 500,000 tokens, which is plenty for prototyping, scripts, and small apps.

Your first free LLM API Python call

Here is a full, copy-paste ready example. The two lines that matter are base_url and api_key. Everything else is standard OpenAI SDK usage.

from openai import OpenAI

client = OpenAI(
    base_url="https://dreamprompting.com/api/v1",
    api_key="YOUR_API_KEY",
)

response = client.chat.completions.create(
    model="auto",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Give me three ideas for a weekend project."},
    ],
)

print(response.choices[0].message.content)

Setting model to "auto" lets the gateway pick an available model and fail over automatically if one provider is down. If you want a specific model, pass a provider/model string such as groq/openai/gpt-oss-120b instead. You can browse the full list on the models page.

Stream the response with stream=True

Streaming lets you print tokens as they arrive instead of waiting for the whole reply. Set stream=True and iterate over the chunks. This is the same pattern you would use against OpenAI directly, so it works without changes here.

from openai import OpenAI

client = OpenAI(
    base_url="https://dreamprompting.com/api/v1",
    api_key="YOUR_API_KEY",
)

stream = client.chat.completions.create(
    model="auto",
    messages=[{"role": "user", "content": "Write a short haiku about Python."}],
    stream=True,
)

for chunk in stream:
    delta = chunk.choices[0].delta.content
    if delta:
        print(delta, end="", flush=True)
print()

Each chunk carries a small piece of text in choices[0].delta.content. Guarding against None avoids errors on the final chunk, which often has no content. This approach feels responsive in a terminal or a chat interface, and you can try the hosted version on the chat playground.

Raw requests example (no SDK)

If you prefer to skip the SDK, you can call the endpoint directly with the requests library. This is handy for minimal scripts, serverless functions, or environments where you cannot add the openai package. The httpx library works the same way if that is your preference.

import requests

API_KEY = "YOUR_API_KEY"

resp = requests.post(
    "https://dreamprompting.com/api/v1/chat/completions",
    headers={"Authorization": f"Bearer {API_KEY}",
             "Content-Type": "application/json"},
    json={
        "model": "auto",
        "messages": [
            {"role": "user", "content": "Explain recursion in one sentence."}
        ],
    },
    timeout=30,
)

data = resp.json()
print(data["choices"][0]["message"]["content"])

The Authorization header is required on every request, so send your key as "Authorization": "Bearer YOUR_API_KEY". Leaving it out returns a 401. The response shape matches the OpenAI format, so choices[0].message.content holds the reply. You can also hit GET /api/v1/models to list available models and GET /api/v1/quota to check your rate limit status. Full details live in the API docs.

A few practical notes make the raw approach smoother in production. Always set a request timeout so a slow provider does not hang your script, and wrap the call in a try or except block to catch network errors. If a request fails, retrying after a short pause is usually enough, since the gateway already fails over between providers on its side. When you build an agent or a longer chat loop, keep the full message history in a list and append each turn so the model has context. That same message list format works identically across the SDK example, the streaming example, and this raw version, which keeps your code portable as your project grows.

Frequently asked questions

Do I need an API key? Yes, and it is free. Sign in with Google at /account, no credit card, and it takes seconds. Pass that key wherever a key is expected. Requests are limited to 100 per minute plus a rolling 24 hour account quota of 5,000 requests and 500,000 tokens.

Does streaming work? Yes. Set stream=True in the OpenAI SDK and iterate over the chunks exactly as shown above. The gateway streams server-sent events in the OpenAI format.

Which models can I use? Use "auto" to let the gateway choose and fail over, or name a specific provider/model like groq/openai/gpt-oss-120b. See the current lineup on the models page.

Next steps

You now have a free LLM API Python setup that runs with the OpenAI SDK, streams responses, and works with plain requests. To go further, read the API docs for every endpoint and parameter, and check the models page to pick the right model for your use case.