This is a beta feature according to Algolia’s Terms of Service (“Beta Services”).
When to use per-turn context
UseturnContext when:
- The client UI already has relevant information for the current turn, such as URL, locale, product ID, or the selected category.
- You don’t want that information to appear in the conversation transcript.
- The information applies only to this turn and shouldn’t persist across the conversation.
- Conversation-wide state (agent instructions, user preferences). Use the agent prompt instead.
- Tool results or extra messages. Those belong in the normal message pipeline.
- Trusted server signals. Agent Studio treats
turnContextas untrusted client input.
How per-turn context works
- Your client attaches
turnContextto the latest user message in a completion request. - Agent Studio validates the payload, renders it as a preamble and a JSON block, and prepends it to the latest user message before calling the LLM.
- If conversation persistence is enabled, Agent Studio stores the context on the user message.
- The model uses the context silently to inform its answer. It doesn’t quote or cite the block.
- When you retrieve conversation history, the stored context is echoed as a
turnContextfield on the user message.
Attach context to a request
The request shape depends on which AI SDK client protocol you use.metadata.turnContext on the user message.
Unknown sibling keys inside metadata are preserved and ignored.
For AI SDK v4, attach it as an entry in the annotations array with type set to turn-context.
Other annotation types pass through untouched, but attaching more than one turn-context annotation to the same message is rejected.
The Chat widget sends its
context option as metadata.turnContext for you.
You don’t need to construct the payload manually when you use the widget.Validation rules
turnContext must be a flat object that maps strings to strings.
Agent Studio rejects malformed payloads.
| Rule | Constraint |
|---|---|
| Type | Flat Record<string, string>. No nested objects, no non-string values. |
| Total size | Up to 4,096 bytes (UTF-8) across all keys and values. |
| Key count | Up to 32 keys. |
| Key charset | ^[A-Za-z0-9_.-]{1,64}$. Both snake_case and camelCase are accepted. |
| Value | Non-empty, non-whitespace, up to 1,024 bytes (UTF-8). |
422 with a stable error code:
| Error code | When |
|---|---|
turn_context_too_many_keys | More than 32 keys. |
turn_context_invalid_key | A key fails the charset or length rule. |
turn_context_empty_value | A value is empty or contains only whitespace. |
turn_context_value_too_long | A value exceeds 1,024 bytes. |
turn_context_oversize | The total serialized size exceeds 4,096 bytes. |
turn_context_duplicate | AI SDK v4 only: multiple turn-context annotations. |
turn_context_invalid_shape | A type mismatch, such as a nested object or non-string value. |
too_many_keys → invalid_key → empty_value → value_too_long → oversize.
What the model sees
Agent Studio prepends a preamble and a<turn_context> JSON block to the latest user message before calling the LLM:
</ sequence inside a value is escaped so a value can’t forge a closing tag.
Retrieve persisted context
When conversation persistence is enabled, the history endpoints echo the stored context as a flatturnContext field on user messages, regardless of which AI SDK version the client used:
null, because per-turn context is a user-turn signal.
For agents with retention turned off, turnContext is null on every user message, because nothing is persisted.
Caching
Per-turn context participates in response caching:- When context is attached, the cache key includes a hash of the context. Two requests with identical messages but different contexts produce distinct cache entries—different pages can warrant different answers.
- When no context is attached, requests share cache entries as before. Enabling per-turn context doesn’t invalidate your existing cache.
Privacy and retention
- Agent Studio treats
turnContextas untrusted client input. Keys and values don’t appear in logs, traces, metrics, or error messages. - Values are stored on the user message under the same retention policy as message text. Agents with retention turned off never store context.
- Authenticated tenants read their own persisted context through the history endpoints—the same trust boundary as message text.
See also
- Send context from the UI library, see the Chat widget’s
contextoption for JavaScript and React - Persist and retrieve conversations, see Conversations
- Cache agent responses, see Caching