Overview
Multi-turn conversations allow you to maintain context across multiple exchanges by including conversation history in your API requests. This enables the model to understand and reference previous messages, creating natural, coherent dialogues.Why use multi-turn conversations?
With conversation history, you can:- Ask follow-up questions - Ask “What’s the population?” and the model knows you’re referring to Paris from the previous message
- Build on previous responses - Request clarifications, expansions, or corrections without repeating context
- Create interactive experiences - Build chatbots, assistants, and conversational interfaces that feel natural
How it works
Each API request includes an array of previous messages. The model uses this history to understand context and maintain coherence across the conversation:Getting started
Maintain context by including conversation history:- Python
- JavaScript
Message types
When building conversation history, you can use the following message types:- user - Messages from the user
- assistant - Messages from the AI model
- system - System instructions or context
API reference
Request
The LLM Gateway accepts POST requests tohttps://llm-gateway.assemblyai.com/v1/chat/completions with the following parameters:
Request parameters
| Key | Type | Required? | Description |
|---|---|---|---|
model | string | Yes | The model to use for completion. See Available models section for supported values. |
messages | array | Yes | An array of message objects representing the conversation history. |
max_tokens | number | No | The maximum number of tokens to generate. Default: 1000. Range: [1, context_length). |
temperature | number | No | Controls randomness in the output. Higher values make output more random. Range: [0, 2]. |
Message object
| Key | Type | Required? | Description |
|---|---|---|---|
role | string | Yes | The role of the message sender. Valid values: "user", "assistant", "system", or "tool". |
content | string or array | Yes | The message content. Can be a string or an array of content parts for the "user" role. |
name | string | No | An optional name for the message sender. For non-OpenAI models, this will be prepended as {name}: {content}. |
Content part object
| Key | Type | Required? | Description |
|---|---|---|---|
type | string | Yes | The type of content. Currently only "text" is supported. |
text | string | Yes | The text content. |
Response
The API returns a JSON response with the model’s completion:Response fields
| Key | Type | Description |
|---|---|---|
request_id | string | A unique identifier for the request. |
choices | array | An array of completion choices. Typically contains one choice. |
choices[i].message | object | The message object containing the model’s response. |
choices[i].message.role | string | The role of the message, typically "assistant". |
choices[i].message.content | string | The text content of the model’s response. |
choices[i].finish_reason | string | The reason the model stopped generating. Common values: "stop", "length". |
request | object | Echo of the request parameters (excluding messages). |
usage | object | Token usage statistics for the request. |
usage.input_tokens | number | Number of tokens in the prompt. |
usage.output_tokens | number | Number of tokens in the completion. |
usage.total_tokens | number | Total tokens used (prompt + completion). |
Error response
If an error occurs, the API returns an error response:| Key | Type | Description |
|---|---|---|
error | object | Container for error information. |
error.code | number | HTTP status code for the error. |
error.message | string | A human-readable description of the error. |
error.metadata | object | Optional additional error context. |
Common error codes
| Code | Description |
|---|---|
| 400 | Bad Request - Invalid request parameters |
| 401 | Unauthorized - Invalid or missing API key |
| 403 | Forbidden - Insufficient permissions |
| 404 | Not Found - Invalid endpoint or model |
| 429 | Too Many Requests - Rate limit exceeded |
| 500 | Internal Server Error - Server-side error |