← Blog

Claude API Conversation History Management Guide

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

Claude's API is stateless — every request you send is evaluated independently, with no memory of what was said before. If you want multi-turn conversations, you are responsible for storing, formatting, and sending the full message history yourself on every call. Claude API conversation history management is the practice of keeping that history accurate, within context limits, and cheap to run.

This matters because two things get worse as conversations grow: cost and reliability. Longer histories mean more input tokens billed on every single request, and past a certain point, excess context can dilute the model's attention on what actually matters in the current turn. The goal of good history management is to send the right context, not just all context.

How Claude Stores (and Doesn't Store) Conversations

The Messages API expects a messages array of alternating user and assistant turns. There is no conversation ID, no server-side thread, no "continue this chat" flag. Every request is a fresh document:

{
  "model": "claude-sonnet-4-5",
  "max_tokens": 1024,
  "messages": [
    {"role": "user", "content": "What's a good name for a coffee shop?"},
    {"role": "assistant", "content": "How about 'Grounded'?"},
    {"role": "user", "content": "I like that. Any taglines?"}
  ]
}

If you omit the earlier turns, Claude has no idea a coffee shop was ever mentioned. Your application layer — not the API — owns conversation state. That's true whether you call Anthropic directly or through a proxy like SubToAPI, which forwards the same message format over a standard HTTPS endpoint (see /docs/messages).

Core Strategies for Managing History

1. Store the raw transcript, trim before sending

Keep a complete, append-only log of every turn in your own database (Postgres, Redis, whatever fits). Never mutate it. When building a request, construct a view of that history that fits your budget — this separates "what happened" from "what we send the model," which makes debugging and auditing much easier.

function buildContextWindow(fullHistory, maxTurns = 20) {
  // Keep system context + the most recent N turns
  return fullHistory.slice(-maxTurns);
}

2. Truncate by token budget, not turn count

Turn count is a rough proxy. A better approach tracks estimated tokens per turn and trims from the oldest end until you're under budget, reserving headroom for the model's response:

function fitToBudget(history, maxInputTokens) {
  let tokens = 0;
  const kept = [];
  for (let i = history.length - 1; i >= 0; i--) {
    const t = estimateTokens(history[i].content);
    if (tokens + t > maxInputTokens) break;
    tokens += t;
    kept.unshift(history[i]);
  }
  return kept;
}

A rough estimate (roughly 4 characters per token in English) is fine for budgeting; you don't need perfect precision to avoid blowing the context window.

3. Summarize old turns instead of dropping them

Dropping old messages loses information. A common pattern: once history exceeds a threshold, send the oldest chunk to Claude with a prompt like "Summarize this conversation segment in 2-3 sentences, preserving names, decisions, and open questions," then replace that chunk with the summary in your stored history going forward.

async function summarizeOldTurns(oldMessages) {
  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: 300,
      messages: [
        { role: "user", content: `Summarize this conversation, preserving key facts:\n\n${JSON.stringify(oldMessages)}` }
      ]
    })
  });
  const data = await res.json();
  return data.content[0].text;
}

Then prepend the summary as a synthetic early user/assistant pair, or fold it into your system prompt. This keeps the model grounded without re-sending every raw message.

4. Use the system prompt for persistent facts

Things that should always be true — user preferences, account details, tone instructions — belong in the system parameter, not buried in old turns where they can get trimmed. The system prompt isn't part of the turn-counting logic, so it survives truncation by construction.

5. Separate "display history" from "model history"

What a user sees in the UI (full chat log, including errors, retries, tool call noise) doesn't have to match what you send to Claude. Strip tool-call scaffolding, redundant confirmations, or debug content before constructing the API payload — see /docs/tools if you're mixing conversation history with tool use, since tool results also count as turns and can bloat context quickly.

6. Expire or archive stale conversations

If your product has long-lived sessions (support tickets, ongoing projects), set a policy: after N days of inactivity, archive the conversation and start fresh context, optionally seeded with a summary. This caps worst-case token usage and keeps latency predictable.

Practical Checklist

If you're building this on top of SubToAPI, usage metadata returned with every response makes it straightforward to track input tokens per conversation and trigger summarization automatically once a thread crosses your threshold — check /docs/streaming if you're also managing history for long-running streamed sessions, since partial responses need to be reassembled before they're stored as a completed turn.

questions

Does Claude remember previous conversations automatically? No. The API is stateless — each request only knows what's in the messages array you send. Your application must store and resend history.

What happens if I exceed the context window? Requests that exceed the model's context limit will fail with an error. You need to truncate or summarize history client-side before the request is sent, not rely on the API to handle overflow.

Should I send the entire conversation on every request? Only if it's short. For longer conversations, trim to a token budget and consider summarizing older turns — this keeps costs down and avoids diluting the model's focus on the current question.

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 →