Category: Uncategorized

  • A Beginner’s Guide to Calling a Free LLM API with Python and LLM.kiwi

    You can send prompts to a hosted model from Python without running a local model or buying a GPU. A free LLM API with Python gives beginners a practical place to test ideas with a few lines of code.

    LLM.kiwi uses an OpenAI-compatible API, so familiar Python tools work with a base URL, an API key, and a short script. Current plans can change, so check the LLM.kiwi pricing page before you build beyond small tests.

    Key Takeaways

    • LLM.kiwi accepts OpenAI-style chat completion requests through https://api.llm.kiwi/v1.
    • Start with Python 3, the openai package, and an API key stored outside source code.
    • Use auto first because it selects a suitable hosted model for general tasks.
    • Test GET /v1/models before relying on a direct model name.
    • Respect rate limits, keep prompts short, and never expose API keys or private data.

    How to Call a Free LLM API with Python and LLM.kiwi

    The request flow is straightforward. Create an account, generate an API key, configure the OpenAI Python client with LLM.kiwi’s custom base URL, send a chat completion, and read the assistant message in the response.

    The two main endpoints are GET /v1/models, which returns the model catalog available to your account, and POST /v1/chat/completions, which accepts prompts. The documented base URL is https://api.llm.kiwi/v1, and requests authenticate with a Bearer token. LLM.kiwi API keys begin with sk_kiwi_.

    Beginner developer using a laptop beside a coffee cup and notebook.

    What You Need Before Writing Your First Script

    Install Python 3 and the official OpenAI Python package with pip install openai. You also need an LLM.kiwi account, an internet connection, and an API key.

    The current quickstart says you can begin without a local model, dedicated GPU, Docker setup, or credit card. Keep the key in an environment variable instead of typing it into a Python file. That habit prevents accidental exposure when you share code or push a project to GitHub.

    Create and Protect Your LLM.kiwi API Key

    Sign in, open the dashboard, and locate the API Keys area. Create a key, then copy it immediately. The key is shown only once, so save it in a password manager or your local environment configuration.

    On macOS or Linux, set it with export LLM_KIWI_API_KEY="sk_kiwi_your_key_here". Never commit that line to Git, paste it into a support request, or leave it visible in a screenshot.

    A laptop and notebook beside a lock icon with colorful connection lines.

    For dashboard access, billing, or key questions, use the LLM.kiwi Help Center.

    Set Up Python and Send Your First Chat Completion

    Create a file named first_request.py, then add these lines in order:

    1. import os
    2. from openai import OpenAI
    3. client = OpenAI(base_url="https://api.llm.kiwi/v1", api_key=os.environ["LLM_KIWI_API_KEY"])
    4. response = client.chat.completions.create(
    5. model="auto", messages=[{"role": "system", "content": "You are a concise programming tutor."}, {"role": "user", "content": "Explain Python lists in two short paragraphs."}]
    6. )
    7. print(response.choices[0].message.content)

    Run it with python first_request.py. The custom base_url matters because the client would otherwise send requests to OpenAI’s own API. The auto model is the recommended beginner default for general chat, coding, questions, and prototypes.

    Your prompt uses messages with roles. The system message sets behavior, while the user message states the task. The response is nested because one request can contain one or more choices. For a normal chat request, response.choices[0].message.content retrieves the first assistant reply.

    Choose Models and Read Responses Carefully

    free LLM API with Python works best when your code checks the current catalog rather than assuming every model remains available. Model access, routing, and plan rules can change.

    Check Available Models Before You Choose One

    Ask the compatible client for models with models = client.models.list(), then inspect the returned IDs. This request maps to GET /v1/models and is a quick way to confirm access before an application sends prompts.

    Start with auto unless you have a defined reason to choose another model. LLM.kiwi also documents hrllm for Croatian-language writing and conversation. Direct access to models such as qwen3kimik2, and gpt-oss-120b may be Pro-only, so confirm the current plan and catalog before hard-coding one.

    The LLM.kiwi model documentation lists recommended uses and reinforces that runtime model discovery is the reliable check.

    Understand the Response and Improve the Prompt

    Print the assistant content first, then inspect response.model and usage fields when the API returns them. Usage data helps you spot prompts that grow too large or loops that generate needless requests.

    Some responses may include more than plain text as API features expand. Therefore, write code that checks for empty content instead of assuming every response is a complete string.

    A strong prompt names the role, task, context, and desired output format. For example, ask for “three bullet points for a beginner, each under 20 words” instead of “tell me about Python.” Clear instructions often improve output more than switching models.

    Do not send passwords, customer records, health details, or unreleased business information while testing prompts.

    Handle Costs, Rate Limits, and Common Python Errors

    LLM.kiwi lists a EUR 0.00 free plan with baseline limits, while Pro has higher capacity. Free access is useful for learning and small tools, but it doesn’t mean every model or request volume is free.

    Limits can apply to requests, tokens, IP addresses, API keys, message size, and daily usage. Public API limits also differ from dashboard and tool endpoints. Keep test prompts short, avoid unnecessary loops, and inspect status codes whenever a request fails.

    Fix Authentication, Model, and Connection Problems

    A missing LLM_KIWI_API_KEY variable causes a local Python error before the request reaches the API. An invalid, revoked, or mistyped key usually triggers an authentication error. Check the full error message, but never print the key itself.

    Connection failures often come from an incorrect base URL. Use the exact documented /v1 path: https://api.llm.kiwi/v1. If Python cannot import openai, install or update the package in the same environment running your script.

    A model error usually means the ID is misspelled or unavailable to your key. Test authentication and model visibility with the models endpoint before debugging a larger prompt payload. The LLM.kiwi API documentation has the endpoint and authentication details in one place.

    Retry Rate-Limited Requests Without Overloading the API

    429 Too Many Requests response means your client must slow down. Don’t repeat the same request in a tight loop. That pattern can extend the wait and consume your remaining allowance.

    Use exponential backoff: pause briefly after the first 429, increase the delay after each failed retry, add a small random delay, and stop after a fixed number of attempts. When present, honor the Retry-After header. Also inspect X-RateLimit-Remaining and X-RateLimit-Reset when the response includes them.

    Free and Pro limits differ, and some IP-based safeguards stay fixed across plans. Log status codes and retry counts, but redact API keys and private prompts from logs.

    Turn the Test Script Into a Safe Python Tool

    After the first request succeeds, put the API call inside a function. The function should reject empty input, set a timeout, return useful errors, and keep configuration outside the source file.

    Test short prompts before you build a chatbot, report generator, or internal utility. Track token usage when it is available, then set input and output limits before other people use the script.

    The main tradeoff is clear. OpenAI compatibility and hosted access make setup simple, while network dependence, changing model access, and plan limits require regular checks.

    A Simple Project Structure for Future LLM Apps

    Keep the first project small. Use a Python entry file, a requirements.txt file, a README, and a local environment file or shell configuration for secrets. Add .env files to .gitignore before your first commit.

    Version prompts and non-secret settings separately from credentials. Production apps also need user authentication, input validation, redacted logging, retry controls, spending limits, and human review for high-impact decisions.

    Frequently Asked Questions

    Can I use the OpenAI Python package with LLM.kiwi?

    Yes. Configure the client with LLM.kiwi’s https://api.llm.kiwi/v1 base URL and your LLM.kiwi API key. The chat completion request shape remains familiar to developers who have used OpenAI-compatible clients.

    Do I need a GPU to call the API?

    No. LLM.kiwi hosts the models, so your Python script sends requests over the internet. Your computer only needs Python and a network connection.

    Is auto a real model name I can use in code?

    Yes. auto is the documented default route for general requests. It selects a suitable available model, which makes it a sensible starting point when you don’t need a particular direct model.

    Why does my API call work once and then return 429?

    You likely hit a rate limit for your plan, key, IP address, or endpoint. Add backoff, reduce request frequency, and check the plan details before increasing traffic.

    Can I put an API key in a .env file?

    Yes, if the .env file stays local and appears in .gitignore. Load it into an environment variable, then read it through os.environ rather than placing the key in source code.

    Start Small, Then Build Carefully

    The shortest path to a working request is simple: install the client, store the API key safely, set the LLM.kiwi base URL, choose auto, and print the returned message.

    Start with small prompts, respect rate limits, and check current model access before building a larger application. Once the script is reliable, turn it into a small command-line chatbot or test structured prompts for a focused task.