Skip to main content
The /v1/completions endpoint provides simple text completion capabilities. Given a prompt, it returns the predicted continuation.

Endpoint

Request Format

Required Parameters

string
required
Model identifier. Can be the model path, alias (set via --alias), or any string when using a single model.
string | array
required
The text prompt to complete. Can be:
  • A string: "Once upon a time"
  • An array of token IDs: [12, 34, 56]
  • An array of strings for batch completion: ["prompt1", "prompt2"]
  • Mixed tokens and strings: [12, 34, "string", 56]

Optional Parameters

number
default:"-1"
Maximum number of tokens to generate. -1 means unlimited.
number
default:"0.8"
Sampling temperature between 0 and 2. Higher values make output more random.
number
default:"0.95"
Nucleus sampling: only tokens with cumulative probability up to top_p are considered.
number
default:"40"
Limit token selection to the K most probable tokens. 0 = disabled.
number
default:"0.05"
Minimum probability threshold relative to the most likely token.
boolean
default:"false"
Stream partial completions as Server-Sent Events.
array
Array of sequences where generation should stop. Stop words are not included in the output.
number
default:"0.0"
Penalize tokens based on whether they appear in the text. Range: -2.0 to 2.0.
number
default:"0.0"
Penalize tokens based on their frequency. Range: -2.0 to 2.0.
number
default:"1.1"
Control repetition of token sequences.
number
default:"1"
Number of completions to generate for each prompt.
number
default:"-1"
Random seed for reproducible outputs. -1 = random seed.
number
Include the log probabilities on the most likely tokens. Maximum: 5.
boolean
default:"false"
Echo back the prompt in addition to the completion.
string
Text that comes after the completion. Useful for code infilling.

llama.cpp-Specific Parameters

number
default:"0"
Enable Mirostat sampling. 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0.
number
default:"5.0"
Mirostat target entropy (τ).
number
default:"0.1"
Mirostat learning rate (η).
string
BNF-like grammar to constrain generation.
object
JSON schema to constrain output to valid JSON matching the schema.
boolean
default:"true"
Reuse KV cache from previous requests for efficiency.

Request Examples

Response Format

Standard Response

string
Unique identifier for the completion.
string
Always "text_completion" for non-streaming responses.
number
Unix timestamp of creation time.
string
The model used for completion.
array
Array of completion choices. Each choice contains:
  • text (string) - The generated text
  • index (number) - Choice index
  • logprobs (object | null) - Log probabilities if requested
  • finish_reason (string) - Why generation stopped: stop, length, or null
object
Token usage statistics:
  • prompt_tokens (number) - Tokens in prompt
  • completion_tokens (number) - Tokens generated
  • total_tokens (number) - Sum of prompt and completion

Example Response

Streaming Responses

When stream: true, the server sends Server-Sent Events:

Log Probabilities

Request token probabilities with the logprobs parameter:
Response includes probability data:

Batch Completions

Generate multiple completions from different prompts:
Response contains completions for each prompt:

Code Infilling

Use the suffix parameter for code completion:
The model will generate code that fits between the prompt and suffix.
For more advanced code infilling, use the native /infill endpoint which supports repository-level context.

Grammar-Constrained Generation

Constrain output using BNF grammar:

JSON Schema Constraint

Force valid JSON output matching a schema:

Mirostat Sampling

Enable Mirostat for controlled perplexity:
Mirostat dynamically adjusts sampling to maintain target entropy (τ), useful for balancing coherence and creativity.

Performance Tips

  1. Prompt caching: Keep cache_prompt: true to reuse KV cache
  2. Batch processing: Send multiple prompts in one request for efficiency
  3. Streaming: Use stream: true for better perceived latency
  4. Stop sequences: Define clear stop conditions to avoid over-generation
  5. Token limits: Set appropriate max_tokens to prevent excessive computation

Error Responses

Common errors:
  • 400 - Missing or invalid parameters
  • 401 - Invalid API key
  • 503 - Server not ready (model loading)

Differences from Chat Completions