← Blog

Claude API Multi-Turn Conversation State Guide

2026-11-09 · 5 min read · SubToAPI Team

Managing conversation state with the Claude API

The Claude API is stateless: every request you send contains the entire conversation history, not just the latest message. There is no server-side session or conversation ID that Anthropic maintains for you between calls. If you want a chatbot, assistant, or agent that remembers what was said three turns ago, your application is responsible for storing the message history and re-sending it on every request.

This matters because it changes how you design your backend. Instead of calling an endpoint with "continue conversation 123," you build an array of {role, content} objects, append each new user message and each assistant reply to it, and send the growing array with every call. Get this pattern right and multi-turn conversations feel seamless. Get it wrong and you'll see the assistant forgetting context, repeating itself, or blowing past token limits.

The basic message array pattern

Every Claude request takes a messages array alternating between user and assistant roles:

{
  "model": "claude-sonnet-4-5",
  "max_tokens": 1024,
  "messages": [
    { "role": "user", "content": "What's the capital of Portugal?" },
    { "role": "assistant", "content": "Lisbon." },
    { "role": "user", "content": "What's its population?" }
  ]
}

To continue the conversation, you don't send a "conversation ID" anywhere — you take the assistant's previous reply, append it as an assistant message, append the new user turn, and send the full array again. This is true whether you call Anthropic's API directly or through a proxy layer.

If you're using SubToAPI to expose your Claude access as an HTTPS API, the request shape is identical — you POST the same messages array to https://api.subtoapi.app/v1/messages with your sub_live_... key. State management happens entirely on your side; SubToAPI just handles auth, streaming, and usage metadata on top of the same stateless model. See /docs/messages for the request reference.

curl https://api.subtoapi.app/v1/messages \
  -H "Authorization: Bearer $SUBTOAPI_KEY" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-sonnet-4-5",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "What is the capital of Portugal?"},
      {"role": "assistant", "content": "Lisbon."},
      {"role": "user", "content": "What is its population?"}
    ]
  }'

Where to store conversation history

You need somewhere to persist the message array between requests. Common patterns, from simplest to most robust:

A minimal Node.js example using an in-memory store per session:

const conversations = new Map();

async function sendTurn(sessionId, userText) {
  const history = conversations.get(sessionId) || [];
  history.push({ role: "user", content: userText });

  const res = await fetch("https://api.subtoapi.app/v1/messages", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.SUBTOAPI_KEY}`,
      "content-type": "application/json",
    },
    body: JSON.stringify({
      model: "claude-sonnet-4-5",
      max_tokens: 1024,
      messages: history,
    }),
  });

  const data = await res.json();
  const reply = data.content[0].text;
  history.push({ role: "assistant", content: reply });
  conversations.set(sessionId, history);
  return reply;
}

Handling growing context windows

The longer a conversation runs, the more tokens you resend on every turn — and that cost adds up, since input tokens are billed on every call even if the content hasn't changed. Three practical strategies:

  1. Truncate old turns. Keep only the last N exchanges and drop the rest. Simple, but loses early context.
  2. Summarize and compress. Periodically ask Claude to summarize the conversation so far, replace the old turns with that summary as a single system or user message, and continue from there. This keeps relevant context while cutting token count significantly.
  3. Track token usage per request. Claude responses include usage metadata (input and output token counts) so you can monitor when a conversation is approaching your model's context limit and trigger summarization proactively rather than reactively. If you're on SubToAPI, this usage data is surfaced per request so you can log it alongside your conversation store — useful for deciding when to prune.

A simple rule of thumb: if your average conversation runs past 15–20 turns, build summarization in from day one rather than retrofitting it after users start seeing degraded responses or rising latency.

System prompts and state

The system field is not part of the messages array and doesn't need to be resent as a message — pass it once per request, every request, since the API has no memory of previous system prompts either. If your assistant's persona or instructions change mid-conversation (e.g., switching modes in a multi-purpose tool), just update the system field on the next call; you don't need to touch the stored history.

Multi-user and multi-device conversations

If the same conversation needs to be accessible from multiple devices or team members, store history server-side keyed by a conversation ID rather than a session ID tied to one browser or device. This is also where team-based access control matters: if multiple people on a team are triggering completions against the same underlying Claude access, you want per-user API keys and usage visibility rather than one shared credential. SubToAPI's Team and Scale plans support per-seat sub_live_... keys so each team member's conversations and usage are attributable without sharing a single key across your organization.

Getting started

If you're setting up conversation handling for the first time, start with the quickstart guide to get an API key, then build your message-array loop against /docs/messages. For chat UIs where you want tokens to appear as they're generated rather than waiting for the full reply, pair this with streaming. You can test the full flow on a free trial at /signup before committing to a plan.

questions

Does Claude API have a conversation or session ID I can use instead of resending messages? No. The API is stateless — there's no built-in session concept. You must store and resend the full (or summarized) message history yourself on every call.

How many turns can I keep in a conversation before hitting limits? It depends on the model's context window and your max_tokens setting. Rather than counting turns, track cumulative input tokens via the usage data returned on each response and summarize or truncate before you approach the limit.

Can I change the system prompt mid-conversation without losing history? Yes. The system field is sent separately from messages on each request, so you can change it any time without touching the stored conversation array.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →