> ## Documentation Index
> Fetch the complete documentation index at: https://docs.llm.kiwi/llms.txt
> Use this file to discover all available pages before exploring further.

# Code examples

> Copy-paste LLM.kiwi code examples in cURL, Python, and Node.js for chat completions, streaming responses, system prompts, and structured JSON output.

The examples on this page use `https://api.llm.kiwi/v1` and the OpenAI-compatible chat completions interface.

<Warning>
  Keep API keys on a trusted server. Never place a private key in public Git repositories or browser-side JavaScript.
</Warning>

## Set your API key

<Tabs>
  <Tab title="macOS and Linux">
    ```bash theme={null}
    export LLM_KIWI_API_KEY="sk_kiwi_..."
    ```
  </Tab>

  <Tab title="Windows PowerShell">
    ```powershell theme={null}
    $env:LLM_KIWI_API_KEY="sk_kiwi_..."
    ```
  </Tab>
</Tabs>

## Basic chat completion

<Tabs>
  <Tab title="cURL">
    ```bash theme={null}
    curl "https://api.llm.kiwi/v1/chat/completions" \
      -H "Authorization: Bearer $LLM_KIWI_API_KEY" \
      -H "Content-Type: application/json" \
      -d '{
        "model": "auto",
        "messages": [
          {"role": "user", "content": "Explain what an API is in one sentence."}
        ]
      }'
    ```
  </Tab>

  <Tab title="Python">
    Install the package:

    ```bash theme={null}
    python -m pip install openai
    ```

    ```python theme={null}
    import os
    from openai import OpenAI

    client = OpenAI(
        base_url="https://api.llm.kiwi/v1",
        api_key=os.environ["LLM_KIWI_API_KEY"],
    )

    response = client.chat.completions.create(
        model="auto",
        messages=[
            {"role": "user", "content": "Explain what an API is in one sentence."}
        ],
    )

    print(response.choices[0].message.content)
    ```
  </Tab>

  <Tab title="Node.js">
    Install the package:

    ```bash theme={null}
    npm install openai
    ```

    Save this as `chat.mjs`:

    ```javascript theme={null}
    import OpenAI from "openai";

    const client = new OpenAI({
      baseURL: "https://api.llm.kiwi/v1",
      apiKey: process.env.LLM_KIWI_API_KEY,
    });

    const response = await client.chat.completions.create({
      model: "auto",
      messages: [
        { role: "user", content: "Explain what an API is in one sentence." },
      ],
    });

    console.log(response.choices[0].message.content);
    ```

    ```bash theme={null}
    node chat.mjs
    ```
  </Tab>
</Tabs>

## System instructions

Use a `system` message to define the assistant's role, output style, or constraints.

```python theme={null}
response = client.chat.completions.create(
    model="auto",
    messages=[
        {
            "role": "system",
            "content": "You are a patient tutor. Use short sentences and one example.",
        },
        {"role": "user", "content": "Explain recursion."},
    ],
)
```

Keep system instructions focused. If your application needs a strict format, validate the returned content instead of assuming the model always follows instructions perfectly.

## Continue a conversation

The API is stateless: include relevant earlier messages on every request.

```python theme={null}
messages = [
    {"role": "user", "content": "My favorite color is green."},
    {"role": "assistant", "content": "Got it."},
    {"role": "user", "content": "What is my favorite color?"},
]

response = client.chat.completions.create(
    model="auto",
    messages=messages,
)
```

## Stream a response

Streaming lets your application display text as it is generated.

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    stream = client.chat.completions.create(
        model="auto",
        messages=[{"role": "user", "content": "Write a short story."}],
        stream=True,
    )

    for chunk in stream:
        text = chunk.choices[0].delta.content
        if text:
            print(text, end="", flush=True)
    ```
  </Tab>

  <Tab title="Node.js">
    ```javascript theme={null}
    const stream = await client.chat.completions.create({
      model: "auto",
      messages: [{ role: "user", content: "Write a short story." }],
      stream: true,
    });

    for await (const chunk of stream) {
      process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
    }
    ```
  </Tab>
</Tabs>

## Croatian response

Select `hrllm` and write the request in Croatian:

```python theme={null}
response = client.chat.completions.create(
    model="hrllm",
    messages=[
        {"role": "system", "content": "Odgovaraj jasno i samo na hrvatskom jeziku."},
        {"role": "user", "content": "Objasni što je strojno učenje."},
    ],
)
```

## Handle errors

```python theme={null}
import time
from openai import APIConnectionError, APIStatusError, RateLimitError

for attempt in range(3):
    try:
        response = client.chat.completions.create(
            model="auto",
            messages=[{"role": "user", "content": "Hello"}],
            timeout=30,
        )
        print(response.choices[0].message.content)
        break
    except RateLimitError:
        if attempt == 2:
            raise
        time.sleep(2 ** attempt)
    except APIConnectionError as error:
        print(f"Could not reach LLM.kiwi: {error}")
        break
    except APIStatusError as error:
        print(f"API returned {error.status_code}: {error.message}")
        break
```

Retry rate limits and temporary server errors with a delay. Do not automatically retry invalid requests or authentication errors.

<CardGroup cols={2}>
  <Card title="SDK setup" icon="cubes" href="/sdks">
    Install and configure supported client libraries.
  </Card>

  <Card title="Troubleshooting" icon="wrench" href="/troubleshooting">
    Fix authentication, request, and connection problems.
  </Card>
</CardGroup>
