Skip to content

Create a completion ​

Call a completion model directly when the application owns everything before and after the model request.

Before you start ​

Complete Install and setup and create a completion model.

Send a request ​

ts
import { createCompletion } from '@anvia/core'
import { OpenAIClient } from '@anvia/openai'

const openai = new OpenAIClient({
  apiKey: process.env.OPENAI_API_KEY,
})

const model = openai.completionModel('gpt-5')

const result = await createCompletion(model, {
  instructions: 'Answer clearly and concisely.',
  input: 'Summarize Anvia in one sentence.',
  maxTokens: 120,
})

console.log(result.text)

Configure the request ​

OptionPurpose
inputA string, one message, or several messages appended to the transcript.
messagesExisting provider-neutral message history.
instructionsStable behavior for this request.
documentsSmall text documents that should accompany the request.
temperatureProvider-supported output randomness.
maxTokensMaximum generated tokens.
paramsProvider-specific options.

At least one of input or messages is required.

Continue an existing transcript ​

ts
const result = await createCompletion(model, {
  messages: previousMessages,
  input: 'Rewrite the last answer as three bullets.',
})

This sends the supplied transcript but does not persist it. Your application owns the history; use memory and an agent session when Anvia should load and append messages automatically.

Handle failures ​

The call rejects for empty input, unsupported model features, authentication failures, transport errors, or provider validation errors. Map those errors at the application boundary instead of returning raw provider details to users.

Built for Anvia.