Inference

Create a chat completion

Generate an answer using the OpenAI Chat Completions protocol.

POST/v1/chat/completions

The SDK appends /chat/completions to the OpenAI-compatible base URL. Configure the connection before using the example. Send the conversation needed for the next answer.

Headers

  • Name
    Authorization
    Type
    string
    Required
    required
    Description

    Bearer followed by the issued inference key, in the proposed gateway contract. Keep the key on the server.

  • Name
    Content-Type
    Type
    string
    Required
    required
    Description

    application/json.

Body parameters

These fields describe the compatibility shape. Model-specific options need validation; this is not a complete OpenAI feature guarantee.

  • Name
    model
    Type
    string
    Required
    required
    Description

    The approved provider model ID. The SDK package does not select an OpenAI model.

  • Name
    messages
    Type
    object[]
    Required
    required
    Description

    Conversation messages with role and content. The text example uses system and user roles. Include earlier turns when needed; do not assume server-side conversation storage.

  • Name
    stream
    Type
    boolean
    Description

    Use false for a complete JSON response or true for server-sent events, where the model and endpoint support streaming.

  • Name
    tools
    Type
    object[]
    Description

    Optional function definitions where tool calling is validated. Your app executes tools and sends their results; the endpoint does not run the agent loop.

Response

The example returns a 200 Chat Completions JSON object for a completed request.

  • Name
    id
    Type
    string
    Description

    The completion identifier.

  • Name
    model
    Type
    string
    Description

    The model used for the response.

  • Name
    choices
    Type
    object[]
    Description

    Read answer text from choices[0].message.content. A tool-call response can contain tool requests instead of text.

  • Name
    choices[].finish_reason
    Type
    string
    Description

    The completion reason. The example uses stop; other reasons depend on the request and model.

  • Name
    usage
    Type
    object
    Description

    Example token counts: prompt_tokens, completion_tokens, and total_tokens. The sample counts are illustrative; production accounting needs confirmation.

Streaming

Use the same endpoint with stream: true. Read choices[].delta, not the complete message. See the streaming guide for events, completion markers, and SDK examples.

Errors and ad delivery

Handle request and stream failures at the application boundary. A completion response does not establish an ad view. The separate ad-ID flow returns an ID rather than an ad payload when Koah requests an ad. Not every response includes an ID. Only when the proposed X-Koah-Ad-Id response header is present, forward its ID to the client and call window.koah.displayAd(adId, { target }). If the header is absent, show the content without an ad. The quickstart shows the candidate response header; it is not part of the standard completion body below.

The compatibility shape follows the Chat Completions reference.

import os

from openai import OpenAI

with OpenAI(
    base_url="https://inference.koah.ai/v1",
    api_key=os.environ["KOAH_INFERENCE_API_KEY"],
    timeout=60.0,
    max_retries=0,
) as client:
    completion = client.chat.completions.create(
        model=os.environ["KOAH_INFERENCE_MODEL"],
        messages=[
            {"role": "system", "content": "Suggest short, memorable product names."},
            {"role": "user", "content": "Name a community garden app. Give three options."},
        ],
    )
    print(completion.choices[0].message.content or "")

Response

{
  "id": "chatcmpl-example",
  "object": "chat.completion",
  "created": 1792584000,
  "model": "sail/DeepSeek-V4.1-Flash-1017-free",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "1. Garden Circle\n2. Shared Soil\n3. Sprout Together"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 32,
    "completion_tokens": 18,
    "total_tokens": 50
  }
}
Was this helpful?