Claude API Conversation History Storage Design
Claude's API is stateless: every request must include the full message history you want the model to see. There's no server-side session you can reference by ID, so conversation history storage design is entirely your application's responsibility. The core problem is deciding how to persist messages, how much of that history to replay on each request, and how to keep token usage and latency under control as conversations grow.
The short answer: store raw messages in a database keyed by conversation ID, keep a separate "active context" that's a trimmed or summarized subset of that history, and send only the active context to the API on each call. The rest of this article covers the schema, trimming strategies, and common pitfalls.
Why stateless APIs need their own storage layer
Unlike some chat platforms, the Claude Messages API doesn't remember previous turns. Each call to /v1/messages takes a messages array of {role, content} objects, and the model only "knows" what's in that array. If you want multi-turn conversations, you must:
- Persist every user and assistant message somewhere durable.
- Reconstruct the
messagesarray on each new request. - Manage growth — conversations that run long will eventually exceed context limits or get expensive to resend.
This is true whether you call the Anthropic API directly or through a proxy like SubToAPI, which forwards the same messages format over HTTPS with an application API key (sub_live_...). Storage design lives entirely in your app layer either way.
A minimal schema
A relational schema works well for most products:
CREATE TABLE conversations (
id UUID PRIMARY KEY,
user_id UUID NOT NULL,
title TEXT,
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE messages (
id UUID PRIMARY KEY,
conversation_id UUID REFERENCES conversations(id),
role TEXT NOT NULL CHECK (role IN ('user', 'assistant')),
content JSONB NOT NULL,
token_count INTEGER,
created_at TIMESTAMPTZ DEFAULT now()
);
Key decisions baked into this schema:
contentas JSONB, not plain text. Claude messages can include text blocks, tool-use blocks, and tool-result blocks. Storing the raw structured content lets you replay tool calls accurately instead of flattening everything to strings.token_countper message. Estimate and store this at write time (even a rough tokenizer estimate) so you can trim history by token budget without re-tokenizing on every read.- No separate "system prompt" table needed — store system prompts at the conversation level if they're static, since they don't belong in the
messagesarray.
A document store (MongoDB, DynamoDB) works equally well if you prefer storing a conversation as one document with an array of messages — it simplifies reads at the cost of harder partial updates on long conversations.
Deciding how much history to replay
Sending the entire history on every call is simple but doesn't scale: costs grow linearly with conversation length, and eventually you hit context window limits. Three common strategies:
1. Sliding window by message count. Keep the last N messages. Simple, but loses early context (e.g., instructions given at the start).
2. Sliding window by token budget. Walk backward through stored messages, summing token_count, and stop once you hit a budget (e.g., 6,000 tokens of history, leaving room for the new prompt and response). This is the most practical default for most apps.
function buildContext(messages, tokenBudget) {
const selected = [];
let total = 0;
for (let i = messages.length - 1; i >= 0; i--) {
total += messages[i].token_count;
if (total > tokenBudget) break;
selected.unshift(messages[i]);
}
return selected;
}
3. Summarize-and-compact. Once a conversation exceeds a threshold, generate a summary of the older portion (a separate Claude call) and store it as a synthetic "system" or "user" message prepended to the active window. This preserves long-term context at a fraction of the token cost but adds complexity and an extra API call.
Most production apps combine #2 as the default and fall back to #3 for conversations that get unusually long (support threads, long-running agents).
Handling tool use and multi-step turns
If your app uses tool calling, a single "turn" from the user's perspective can involve multiple Claude responses (assistant requests a tool, you run it, you send the result back, Claude responds again). Store each of these as separate rows in the messages table in the order they occurred — the Messages API expects alternating roles with tool-result content blocks nested correctly. Don't try to collapse a tool-use exchange into one row; it breaks replay and makes debugging failed tool calls much harder. See /docs/tools for the exact content block shapes.
Pagination and listing conversations
For a chat UI, you'll also need efficient pagination of conversation lists and message history within a conversation. Index on (conversation_id, created_at) and paginate with a cursor on created_at rather than offset-based pagination — offsets get slow and inconsistent once conversations have thousands of messages (rare, but it happens with long-running agents).
Where SubToAPI fits
If you're already managing storage and context-window logic yourself, SubToAPI doesn't replace that — it sits underneath as the transport layer. You get an application API key, send the same messages array you've built from your own history store to https://api.subtoapi.app/v1/messages, and get streaming, usage metadata, and per-key spend tracking without managing separate Anthropic credentials per environment or teammate. It's useful specifically because conversation history design is your job regardless of which endpoint you call — SubToAPI just makes the calling side (keys, streaming, team access) easier to manage. See /docs/quickstart and /docs/messages for the request format, and /docs/streaming if you're streaming long assistant responses into your stored messages table.
questions
Does Claude's API remember previous conversations automatically? No. The API is stateless — you must store messages yourself and send the relevant history back in the messages array on every request.
Should I store full message content or just summaries? Store full raw content (including tool-use blocks) as your source of truth, and generate summaries only as a secondary, derived layer for trimming long conversations before sending them to the model.
How do I stop old conversations from costing too much in tokens? Use a token-budget sliding window to select recent messages, and summarize or archive anything older than that window so it isn't resent on every call.