Chat Completions Guide¶
Chat completions are the primary way to interact with models on OpenRouter. This library provides multiple ways to create and handle these requests.
Non-Streaming Requests¶
To get a complete response at once, use the client.chat { } DSL.
val response = client.chat {
model = "openai/gpt-4o-mini"
userMessage("What is the capital of France?")
temperature = 0.7
}
println(response.choices.first().message?.content)
Request Parameters¶
You can configure several parameters in the request:
model: The model ID (e.g.,openai/gpt-4o-mini,anthropic/claude-3-haiku).messages: A list of messages (use builder methods below).temperature: Controls randomness (0.0 to 2.0).maxTokens: The maximum number of tokens to generate.topP: Nucleus sampling parameter.topK: Top-K sampling parameter.seed: Random seed for deterministic output.
Message Types¶
OpenRouter supports several message types, which you can add easily using the DSL.
System Message¶
Used to set the behavior and persona of the assistant.
User Message¶
The input from the user.
Assistant Message¶
A message previously generated by the model.
Tool Message¶
The result of a tool call.
Response Structure¶
The ChatCompletionResponse object contains the following:
id: Unique identifier for the completion.choices: A list of generated choices (usually one).message: The generated message.finishReason: Why the generation stopped (e.g.,stop,length).
usage: Token usage information for the request.promptTokens: Tokens used in the prompt.completionTokens: Tokens generated.totalTokens: Sum of prompt and completion tokens.
Using l1 Types Directly¶
If you need more control or want to reuse request objects, you can use the low-level l1 data models.