MarsAI
Sign in Get Started

MarsAI developer guide

Keep context across routed model calls

Conversation history belongs in the client. Route affinity keeps one session on one healthy target, but it cannot recreate messages that were omitted.

Reviewed . Checked against the current MarsAI /v1 behavior and published catalog.

Chat history lives in the client

The Chat Completions endpoint is stateless. Every call must include the system message, earlier user messages, assistant replies and tool exchanges that the model needs. MarsAI routes the JSON it receives; it does not rebuild turns that the client left out.

Sending only the newest user message starts a new model context even when the API key and public model ID are unchanged.

Keep one session on one route

Send a stable X-Session-Affinity or X-Session-Id value for the life of a conversation. MarsAI hashes the value and uses weighted session routing, so the same session and public model alias stay on one eligible target. Different sessions still spread across the configured routes.

curl
curl https://marsai.click/v1/chat/completions \
  -H "Authorization: Bearer $MARSAI_API_KEY" \
  -H "Content-Type: application/json" \
  -H "X-Session-Affinity: project-42-chat-7" \
  -d '{
    "model": "YOUR_PUBLIC_MODEL_ID",
    "messages": [
      {"role":"user","content":"Remember the codeword ORBITAL."},
      {"role":"assistant","content":"I will remember it."},
      {"role":"user","content":"What was the codeword?"}
    ]
  }'

MarsCLI already sends session affinity headers. A client with no session header gets a temporary API key and model pin after a successful call, but that pin still stores no conversation text.

Responses calls require full input history

MarsAI returns Responses compatible framing IDs but does not store server side response history. Responses objects report store: false and previous_response_id: null. A nonnull previous_response_id returns HTTP 400 with code previous_response_not_found.

When a client receives that code, it should remove the continuation ID and resend its canonical full input history. Server managed conversation and item_reference fields are rejected rather than ignored.

Affinity is consistency, not memory

Affinity prevents an avoidable model or provider switch during a healthy session. If the selected target becomes unavailable before output starts, failover may choose another eligible target. Once a stream has emitted text, the gateway cannot splice a second model's continuation onto that partial answer.

Client checklist

  • Persist the canonical conversation in your application.
  • Replay every required message and tool result on each Chat Completions call.
  • Reuse one stable session header value within a conversation, not across unrelated users.
  • Treat partial streamed output as incomplete.
  • On previous_response_not_found, retry once with full Responses input history.