Claude API Conversation Memory Management Guide
The Claude API is stateless — it has no memory of previous requests unless you send the conversation history back with every call. "Conversation memory management" is the practice of deciding what history to keep, what to drop, and how to store it between requests so your app feels continuous without blowing through context limits or racking up unnecessary token costs.
There are three core problems to solve: storage (where conversation turns live between requests), context window limits (how much history you can realistically send), and relevance (which parts of the history actually matter for the next response). This article covers practical patterns for all three, with code you can drop into a Node.js or curl-based project.
Why Claude doesn't "remember" anything by default
Every call to the Messages API is independent. Claude generates a response based solely on the messages array you send — there's no server-side session, no hidden state, no "resume conversation" flag. If you want multi-turn behavior, you build the array yourself:
{
"model": "claude-sonnet-4-5",
"messages": [
{"role": "user", "content": "I'm building a REST API in Go."},
{"role": "assistant", "content": "Got it. What does the API need to do?"},
{"role": "user", "content": "Handle file uploads up to 50MB."}
]
}
That third user message only makes sense because the first two turns are included. Drop them, and Claude has no idea what "it" refers to. This is true whether you call Claude directly or through a proxy like SubToAPI — the memory logic lives in your application, not the API.
Pattern 1: Full history replay (simple, limited)
The naive approach: store every message in a database, and on each new request, send the entire history back.
const history = await db.getMessages(conversationId);
history.push({ role: "user", content: userInput });
const response = 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
})
});
This works fine for short-lived conversations (support chats, quick Q&A). The problem: every long-running conversation eventually hits the model's context window, and even before that, you're paying input-token cost for the same old messages on every single call. See /docs/messages for request formatting details.
Pattern 2: Sliding window
Keep only the last N turns (or last N tokens) and drop older ones. This caps cost and avoids context overflow, at the expense of losing early context.
function slidingWindow(history, maxTurns = 20) {
return history.length > maxTurns
? history.slice(history.length - maxTurns)
: history;
}
A sliding window is good for casual assistants where old context rarely matters (e.g., "what's the weather" style bots). It's a bad fit when users reference something from turn 3 in turn 40 — the model will have no idea what they mean.
Pattern 3: Summarize-and-compress
The most common production pattern: periodically collapse older turns into a short summary, and keep only recent turns verbatim plus the summary.
async function compressHistory(history, callClaude) {
const [recent, older] = splitAt(history, history.length - 10);
if (older.length === 0) return history;
const summaryPrompt = [
...older,
{ role: "user", content: "Summarize this conversation so far in 3-5 sentences, preserving key facts, decisions, and open questions." }
];
const summary = await callClaude(summaryPrompt);
return [
{ role: "user", content: `[Earlier conversation summary]: ${summary}` },
...recent
];
}
Run this compression step whenever the stored history exceeds a token threshold (e.g., 6,000 tokens), rather than on every message — recomputing a summary every turn wastes calls. This pattern scales well for long-running assistants, onboarding flows, or multi-session support tickets.
Pattern 4: Structured memory (facts, not transcripts)
For apps where specific facts matter more than exact wording — a CRM assistant that needs to remember a customer's plan tier, or a coding assistant that needs to remember project structure — store structured key-value memory instead of raw transcript:
{
"customer_name": "Priya Shah",
"plan": "Team",
"open_issue": "Billing seat count mismatch",
"last_contacted": "2024-02-01"
}
Inject this as a short system-style message at the start of each conversation instead of replaying dozens of old turns. This is usually the cheapest and most reliable approach when the conversation has a clear schema (support tickets, onboarding, order tracking).
Tracking token usage to decide when to compress
Claude's response includes usage metadata so you can monitor how close you are to context limits and decide when to trigger summarization:
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": "Summarize our chat so far."}]
}'
The response's usage.input_tokens and usage.output_tokens tell you exactly how much context you're consuming per call. SubToAPI surfaces this same usage data in request logs, which makes it easier to spot conversations that are growing too large before they hit a hard limit — useful if you're running this across a team with shared API access (/pricing). For a baseline implementation, /docs/quickstart walks through sending your first multi-turn request, and /docs/streaming covers handling long responses without blocking on memory-heavy conversations.
Picking the right pattern
- Short sessions, low stakes: full history replay
- Casual bots, recency matters more than history: sliding window
- Long-running assistants, support, tutoring: summarize-and-compress
- Transactional apps with clear data fields: structured memory
Most production systems end up combining two: structured memory for hard facts, plus a sliding window or summary for recent conversational nuance.
questions
Does the Claude API store conversation history for me? No. The API is stateless — you must send the full message history (or a compressed/summarized version of it) with every request. Storage and retrieval are your application's responsibility.
How do I know when a conversation is too long for the context window? Check usage.input_tokens in each response and track cumulative size. When you approach your model's context limit, trigger summarization or drop older turns before the next call fails.
Is summarizing conversation history expensive? It adds one extra API call per compression cycle, but you only need to run it occasionally (e.g., every 10–20 turns), not on every message. The token savings on subsequent calls usually outweigh the summarization cost.