Skip to content
Inference

Inference quickstart

Use your River API key with https://api.river.ai/v1 and the OpenAI SDK or HTTP.

Endpoints

EndpointUse
POST /v1/chat/completionsChat, images, tools, and streaming
POST /v1/responsesResponses API
GET /v1/modelsList model IDs available through the API

Connect

Create an API key in the River Console. See Models and pricing for available models.

pip install openai
export RIVER_API_KEY="rv_..."

Keep your API key on your server, outside browser code and source control.

Stream a response

Save this as inference.py and run python inference.py:

import os

from openai import OpenAI

with OpenAI(
    api_key=os.environ["RIVER_API_KEY"],
    base_url="https://api.river.ai/v1",
) as client:
    with client.chat.completions.create(
        model="deepseek-ai/DeepSeek-V4.1-Flash",
        messages=[{"role": "user", "content": "Say hello in one short sentence."}],
        max_tokens=1024,
        stream=True,
    ) as stream:
        for chunk in stream:
            if chunk.choices:
                print(chunk.choices[0].delta.content or "", end="", flush=True)
    print()

Send conversation history with each request. Unchanged prefixes can reuse the cache when available.

Call with curl

This returns a complete JSON response, with the answer in choices[0].message.content and token counts in usage:

curl --fail-with-body https://api.river.ai/v1/chat/completions \
  -H "Authorization: Bearer $RIVER_API_KEY" \
  -H "Content-Type: application/json" \
  -d @- <<'JSON'
  {
    "model": "deepseek-ai/DeepSeek-V4.1-Flash",
    "messages": [{"role": "user", "content": "Say hello in one short sentence."}],
    "max_tokens": 1024
  }
JSON

For HTTP streaming, add "stream": true to the body and -N to curl. The response uses server-sent events and ends with data: [DONE].

Chat with a LoRA checkpoint

Set model to a LoRA checkpoint path that you saved, or one that is public, to chat with it through the same endpoint:

response = client.chat.completions.create(
    model="river://YOUR_RUN_ID/sampler_weights/YOUR_CHECKPOINT",
    messages=[{"role": "user", "content": "Say hello in one short sentence."}],
    max_tokens=1024,
    timeout=1800,
)
print(response.choices[0].message.content)

This runs exactly like Client.chat_complete_from_checkpoint in the Python client: the request waits in the same inference queue and is billed the same way. The response arrives when the completion finishes, up to 30 minutes, so set a client timeout of at least that long. The x-river-request-id response header identifies the request. Full-weight checkpoints are not supported here. For low-latency serving of a checkpoint, use a deployment.

With stream=True, the request runs the same way and the whole completion arrives when it finishes, in one chunk, or in a few consecutive chunks when it is very large; tokens are not streamed as they are generated. While the request waits, the stream carries keep-alive comments, which keeps long waits from being cut off by idle connections. Errors found before the request is queued, such as an unknown checkpoint, are ordinary HTTP errors; later failures arrive as an error event in the stream. With stream_options={"include_usage": True}, a last chunk with empty choices carries usage:

stream = client.chat.completions.create(
    model="river://YOUR_RUN_ID/sampler_weights/YOUR_CHECKPOINT",
    messages=[{"role": "user", "content": "Say hello in one short sentence."}],
    max_tokens=1024,
    stream=True,
    stream_options={"include_usage": True},
    timeout=1800,
)
for chunk in stream:
    if chunk.choices:
        print(chunk.choices[0].delta.content or "", end="")
    if chunk.usage:
        print("\n", chunk.usage.total_tokens, "tokens")

For reasoning and structured output, see API controls. For errors, retries, and timeouts, see Limits and support.