Guides

Chat Completions

Send stateless text-generation requests through POST /v1/chat/completions using familiar OpenAI SDK methods.

文

This guide has an independently written Vietnamese edition. Đọc bằng Tiếng Việt

SDK documentation · API v1

Create a completion

Send POST /v1/chat/completions with model and at least one message. Use auto to route according to the application’s policy, or choose a visible concrete model. Every generation request needs an Idempotency-Key in the default strict configuration.

import os
from uuid import uuid4

from openai import OpenAI

client = OpenAI(
    api_key=os.environ["TASKWAY_API_KEY"],
    base_url=os.environ["TASKWAY_BASE_URL"],
)

completion = client.chat.completions.create(
    model="auto",
    messages=[{"role": "user", "content": "Summarize this paragraph."}],
    extra_headers={"Idempotency-Key": str(uuid4())},
)
print(completion.choices[0].message.content)
import { randomUUID } from "node:crypto";
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.TASKWAY_API_KEY,
  baseURL: process.env.TASKWAY_BASE_URL,
});

const completion = await client.chat.completions.create(
  {
    model: "auto",
    messages: [{ role: "user", content: "Summarize this paragraph." }],
  },
  { headers: { "Idempotency-Key": randomUUID() } },
);
console.log(completion.choices[0]?.message.content);

The full Python and Node examples appear in the quickstart.

Messages and options

The request uses an ordered messages array. The gateway processes each request independently; include the prior user and assistant messages yourself when continuing a chat. Provider/model eligibility affects whether sampling values or other optional parameters can be used.

The checked profile includes temperature, top_p, stop, seed, frequency/presence penalties, max_tokens or max_completion_tokens (choose one), function tools and history, parallel_tool_calls, n from 1 to 4, JSON object, and a supported structured JSON Schema subset. Unsupported settings return a validation error. See compatibility for provider-specific caveats.

The gateway’s max_completion_tokens cap includes reasoning and visible completion output. Application limits can impose a lower cap. With n > 1, each choice has the requested cap.

Gateway-specific request constraints can be supplied through the Chat gateway object. The currently modeled values include max_latency_ms, allowed/blocked providers and models, external_provider_allowed, and allow_partial_response; use them only when your application expects these controls.

Keep conversation history

Chat Completions does not provide stored conversation state. Persist the messages in your application and send the relevant history with the next request. If the message contains a tool call, keep the assistant’s tool-call data and call IDs, run the function in your application, then include the matching tool result in a later request.

The gateway never runs your function code. See function tools and streaming for the request loop.

Output and usage

Read the assistant message from choices. usage describes the public output producer or producers. A gateway receipt also records work across the internal pipeline. Its usage.pipeline includes execution usage: do not add the two scopes together. Missing provider counters remain unknown rather than being reported as zero.