Skip to navigation

Multi-turn conversations

Continue a conversation by sending it back, or let Gateway or Anthropic hold it for you

A conversation is multi-turn when the model needs what was already said. Send prior turns in input on every request, have Gateway store a turn and continue from its ID, or, for Claude on /v1/messages, use Anthropic message threads.

Send the conversation in input

This works on every model and route, needs no setup, and Gateway keeps nothing, so it’s the right default.

cURL
curl https://api-gateway.merge.dev/v1/responses \
-H "Authorization: Bearer $MERGE_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-4o",
"input": [
{"type": "message", "role": "user", "content": "Say hello in one word."},
{"type": "message", "role": "assistant", "content": "Hello"},
{"type": "message", "role": "user", "content": "What word did you just say?"}
]
}'

On most routes the repeated prefix is cached and billed at a lower rate.

Let Gateway hold the conversation

Send store: true and Gateway saves the turn under the response id. Next time, pass that ID as previous_response_id with only the new input; Gateway replays the stored conversation ahead of it.

cURL
curl https://api-gateway.merge.dev/v1/responses \
-H "Authorization: Bearer $MERGE_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-4o",
"previous_response_id": "resp_9754aec2adc6059b",
"input": [{"type": "message", "role": "user", "content": "What word did you just say?"}],
"store": true
}'

Send store: true on every turn you want to resume from; a resumed turn without it runs but isn’t saved.

Storing is off unless you ask

Omitting store stores nothing, unlike the OpenAI API, which defaults to true. A client that relies on the OpenAI default gets a 404 on previous_response_id until it sends store: true.

The store is best-effort (records can expire or fail to be stored), so keep your own transcript as the fallback. It doesn’t lower token cost: the vendor still receives the full conversation. Only text and tool calls replay. Reasoning blocks, vendor-run tool results such as web search, and generated images are dropped, since the next turn may route to another vendor. /v1/openai/responses and /v1/ai-sdk accept store and previous_response_id too.

Read or delete a stored response

cURL
curl https://api-gateway.merge.dev/v1/responses/resp_9754aec2adc6059b \
-H "Authorization: Bearer $MERGE_GATEWAY_API_KEY"
curl -X DELETE https://api-gateway.merge.dev/v1/responses/resp_9754aec2adc6059b \
-H "Authorization: Bearer $MERGE_GATEWAY_API_KEY"

GET returns an object with object: "response.stored" and the stored input, output, previous_response_id, and vendor. DELETE returns {"id": "...", "object": "response.deleted", "deleted": true} and ends the chain.

Reference

BehaviorValue
Expiry30 days after the turn is stored, matching OpenAI. DELETE /v1/responses/{id} removes it sooner.
Maximum stored snapshot8 MB. A larger turn isn’t stored, and the chain ends there.
ScopeYour organization, plus the project or customer your key is scoped to. The match is exact, so an organization-wide key can’t read a turn stored by a project-scoped key.
Zero data retentionNothing is stored, and store: true has no effect
Batch requestsprevious_response_id isn’t accepted
ErrorCause
404 not_found_error, code: previous_response_not_foundThe ID expired, was never stored, or belongs to another scope. Send the full conversation in input.
400 previous_response_id_unsupportedYour organization has zero data retention on
500 response_store_unavailableThe store didn’t answer in time. Retry, or send the full conversation.
400 conversation_unsupportedThe request sent an OpenAI conversation object, which Gateway doesn’t host
404 response_not_foundGET or DELETE on an unknown ID

Anthropic message threads

On POST /v1/messages, Gateway supports Anthropic’s message threads (beta). Send the full conversation once with thread: {"type": "create"}, then send each later turn with only the new message and the previous response’s id. Anthropic holds the conversation between turns, so a long agent loop stops resending a growing transcript. Gateway adds Anthropic’s beta header whenever the body has thread, so you don’t need to.

cURL
curl https://api-gateway.merge.dev/v1/messages \
-H "Authorization: Bearer $MERGE_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-5",
"max_tokens": 1024,
"system": "You are a concise travel assistant.",
"messages": [{"role": "user", "content": "What is the capital of France?"}],
"thread": {"type": "create"}
}'
# Continue from the id the first response returned
curl https://api-gateway.merge.dev/v1/messages \
-H "Authorization: Bearer $MERGE_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-5",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "And what is its population?"}],
"thread": {"type": "continue", "previous_message_id": "msg_resp_9754aec2adc6059b"}
}'
Anthropic SDK
import os
from anthropic import Anthropic
client = Anthropic(
api_key=os.environ["MERGE_GATEWAY_API_KEY"],
base_url="https://api-gateway.merge.dev",
)
first = client.messages.create(
model="anthropic/claude-sonnet-5",
max_tokens=1024,
system="You are a concise travel assistant.",
messages=[{"role": "user", "content": "What is the capital of France?"}],
extra_body={"thread": {"type": "create"}},
)
second = client.messages.create(
model="anthropic/claude-sonnet-5",
max_tokens=1024,
messages=[{"role": "user", "content": "And what is its population?"}],
extra_body={"thread": {"type": "continue", "previous_message_id": first.id}},
)

A continue sends only the new turn. It can’t carry system or tools, even as null, because the thread keeps the ones from the request that created it. Like the response store, a thread is best-effort, so keep your own transcript as the fallback.

BehaviorValue
ExpirySet by Anthropic. An idle thread expires after a few minutes.
ModelsModels Gateway can serve through Anthropic’s own API for your key. Bedrock and other vendors don’t support threads.
RoutingA continue goes to the same model and vendor as the turn before it and never fails over, since no other vendor has the conversation
ScopeYour organization, plus the project or customer your key is scoped to. An ID from another scope reads as not found.
Zero data retentionThreads are unavailable, because Anthropic holds the conversation between turns
Context compressionSkipped, since it would change the conversation Anthropic stored
ErrorCause
404 not_found_error, details.error_code: thread_not_foundThe thread expired or can’t be continued for any other reason. Send the full conversation again with thread: {"type": "create"}.
400 invalid_threadprevious_message_id is missing or isn’t a Gateway response ID, or a continue sent system or tools
400 thread_unsupportedThe model can’t be served through Anthropic for your key, or your organization has zero data retention on
thread_fingerprint_mismatch, thread_already_continued, thread_continue_in_flight, thread_unsupported_requestAnthropic’s own thread errors, passed through. None of them fail over.

The 404 uses Anthropic’s error shape, so branch on error.details.error_code:

{
"type": "error",
"error": {
"type": "not_found_error",
"message": "The thread can't be continued. Send the full conversation in a new thread.",
"details": { "error_code": "thread_not_found" }
}
}

Next steps