diagnostics.previous_message_id : must be the id from a prior /v1/messages
The request named a previous message that is not a msg_ id the API issued. In Claude Code it follows a connection or rate-limit error; run /clear or start a new session.
Summary
- previous_message_id must be the id of an earlier Messages API response, which starts with msg_, or null on the first turn.
- Claude Code can save a failed turn with an internal UUID instead of a msg_ id and then send that UUID on the next request.
- In the reported cases /clear stopped the error repeating; a resumed session can need the bad entry removed from its transcript.
- Both Claude Code reports are open with no maintainer reply, so the fix for now is on your side.
On this page
The id is not one the API issued
API Error: 400 diagnostics.previous_message_id: must be the `id` from a prior /v1/messages response (starts with `msg_`)
diagnostics is the Claude API's cache diagnostics object. Each turn passes the id of the previous response as previous_message_id, and the API reports where the new request's prompt prefix diverged from the old one, which explains cache misses. On the first turn the value is null. Anything else that does not look like a response id (they start with msg_) is rejected with this 400.
When Claude Code prints it
Two open reports in Claude Code's issue tracker show the same chain of events:
- A turn fails: a connection error in issue #96033 (Claude Code 2.1.131), a rate limit hit mid-response in issue #107331.
- Claude Code writes that failed turn into the session's transcript as an assistant entry with
"isApiErrorMessage": trueand an internal UUID as its id, not amsg_id. In #107331 the entry also has"model": "<synthetic>". - The next request, or
claude --resume, takes the most recent assistant entry's id asprevious_message_idand sends the UUID. The API answers with the 400.
To get past it:
- In a running session,
/clear. The #96033 reporter found it stops the error repeating on every prompt. - To keep a resumed conversation, take the bad entry out of its transcript. Transcripts live under
~/.claude/projects. Back the file up, remove the trailing assistant entry marked"isApiErrorMessage": trueand the bookkeeping lines written after it, then resume. The #107331 reporter used this repair and describes it as manual. - If neither fits, start a new session; the stale id lives only in the old transcript.
Both reports suggest the real fix: skip entries whose id does not start with msg_ when choosing previous_message_id. Neither has a maintainer reply yet, so check claude update for a release that mentions it.
In your own code
Pass the id from the response you just received, unchanged:
r1 = client.beta.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Summarize section 1."}],
diagnostics={"previous_message_id": None},
)
r2 = client.beta.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
messages=[
{"role": "user", "content": "Summarize section 1."},
{"role": "assistant", "content": r1.content},
{"role": "user", "content": "Now summarize section 2."},
],
diagnostics={"previous_message_id": r1.id},
)
Do not substitute your own request ids, trace ids or database keys, and do not pass the id of a response that failed: a failed request has no msg_ id to reuse. A wrong but well-formed id is handled differently. The API returns previous_message_not_found inside the response's diagnostics, not a 400, when it has no stored fingerprint for that id, for example because the earlier request skipped the diagnostics object, ran in another workspace or was sent too long ago.
Cache diagnostics exist only on the Claude API, not on Amazon Bedrock or Claude models on Google Cloud (Google managed). If Claude Code itself sends a field that a provider or gateway does not accept, see context_management: Extra inputs are not permitted. A turn cut off by a broken stream is covered on the Response stalled mid-stream page, and other 400s are on our Claude Code API Error 400 page.
Sources and checks
- Cache diagnostics (Claude API)
- anthropics/claude-code #96033: Session resume sends invalid previous_message_id (UUID) when prior session ended with connection error
- anthropics/claude-code #107331: claude --resume fails after mid-stream rate-limit: synthetic assistant entries poison previous_message_id chain