Claude Code Context Deep Dive (2): Three Rules for the Message Array
Editor's note (November 2026): This article is an AI-assisted translation and adaptation of an older research note about Claude Code's message handling. Two statements in that note were too absolute: an application may t
Editor's note (November 2026): This article is an AI-assisted translation and adaptation of an older research note about Claude Code's message handling. Two statements in that note were too absolute: an application may transform or compact earlier messages rather than append forever, and the Anthropic Messages API does allow consecutive messages of the same role, combining them into one turn. The three βinvariantsβ below are therefore presented as practical design rules, with the public API contract separated from version-specific Claude Code implementation observations.
Part 1 described the agent loop: Claude Code assembles a request, receives a response, runs any requested client-side tools, and supplies results to a later model call. Now the message list itself is the subject. What does one message contain? How do tool requests and results relate? Which constraints are real API requirements, and which are merely useful implementation conventions?
What one message looks like
At the public Messages API, the essential shape is a role and content:
{
"role": "user",
"content": "Please investigate this bug."
}
content can also be a list of typed blocks, not just a string. An application such as Claude Code may maintain additional local metadataβIDs, timestamps, parent pointers, or flags for internal messages. Those are not automatically fields in the API request. The original research note illustrated an internal record with uuid and timestamp; that is a version-specific implementation detail, not the minimal public message schema.
As the loop proceeds, the list also contains assistant responses and user-role messages carrying client-tool results.
How the model learns which tools exist
The message list is not the whole request. A tool-capable request can also include system instructions and a tools parameter:
{
"system": "...",
"tools": ["...tool definitions..."],
"messages": ["...conversation..."],
"max_tokens": 1024
}
A client-tool definition typically supplies a name, description, and JSON input_schema:
{
"name": "get_deployment_status",
"description": "Return the current deployment for an environment.",
"input_schema": {
"type": "object",
"properties": {
"environment": { "type": "string" }
},
"required": ["environment"]
}
}
The model uses those definitions to select a tool and produce a tool_use block containing the name and arguments. Claude Code also has built-in tools such as Read, Edit, and Bash, while MCP can contribute more. Which definitions are available can vary by session, permissions, and deferred loading. For the public interface, see Anthropic's tool-use documentation.
Why a client keeps a message history
Model invocations do not carry an invisible private memory of earlier calls. To continue a conversation, the client supplies the relevant prior turns and new material. In an ordinary tool loop it adds the assistant's tool request and, after execution, a result the model can inspect.
A message array is a natural way to represent that history. It is not a promise that every previous entry will always be sent unchanged. Compaction, context editing, branching, and summarization can select or transform the request-visible history. The distinction is between the record the application stores and the messages it sends now.
One assistant response can request multiple tools
An assistant message can contain text and two tool calls:
{
"role": "assistant",
"content": [
{ "type": "text", "text": "I'll read both files." },
{ "type": "tool_use", "id": "toolu_A", "name": "Read", "input": { "file_path": "auth.py" } },
{ "type": "tool_use", "id": "toolu_B", "name": "Read", "input": { "file_path": "login.py" } }
]
}
For client-side tools, the application executes the requests and can send the results in a subsequent user-role message:
{
"role": "user",
"content": [
{ "type": "tool_result", "tool_use_id": "toolu_A", "content": "...auth.py..." },
{ "type": "tool_result", "tool_use_id": "toolu_B", "content": "...login.py..." }
]
}
The tool_use_id is the important correspondence. It tells the model which result answers which request; one must not infer identity from position. This becomes particularly important when tools are parallel or return at different times. The earlier note asserted a specific Claude Code completion-order policy, but that is an internal behavior we cannot verify from the public API, so the example does not rely on it.
Rule 1: Preserve a coherent history, but do not assume immutable append-only input
In the ordinary loop, new user messages, assistant responses, and tool results are appended to the working history. That simple rule makes causal order clear and enables stable prefixes for prompt caching.
The older note went further: βnever update, delete, or reorder an existing message,β including a claim about append-only JSONL storage and parent UUID chains. That may describe a particular local implementation or persistence path, but it is not a universal Messages API invariant. The request sent to the model can be constructed from a summary, a branch, or edited context. Even if an on-disk transcript remains append-only, the next request need not contain every earlier message.
The durable rule is narrower: preserve the meaning and ordering of the context you choose to send. When you compact or branch, make the resulting request coherent; do not silently treat the storage log and request payload as identical.
Rule 2: Match client-tool results to their requests
A tool_use block gives a client-tool call an ID. A tool_result block refers to that ID. When continuing the loop, send a result for each client-tool request, immediately after the assistant tool-use message. Put tool_result blocks first in the following user message, and do not fabricate an unrelated ID. A tool failure can be represented with is_error: true rather than leaving the model guessing.
assistant: tool_use id=toolu_A
β execute or fail
user: tool_result tool_use_id=toolu_A
This matching is the structural relationship that matters. An interruption, rewind, or compaction routine must avoid creating a request with dangling or misidentified tool results. The source note described a particular 400-error string for a malformed request; exact error text and repair behavior are version-specific, so this article does not present them as a stable API guarantee. See Handle tool calls for the current round trip.
Internal meta-context is not another API role
Claude Code may need to tell the model about project instructions, dates, changed files, tool availability, or an interrupted operation without pretending that the user typed the text. The research note described an internal isMeta: true flag and a <system-reminder>...</system-reminder> wrapper used by a version of Claude Code.
Conceptually:
Internal record:
role: user
isMeta: true
content: <system-reminder>...application context...</system-reminder>
Public API request:
role: user
content: ...selected text/content blocks...
isMeta is not a public Messages API role or field. It can help an application distinguish its own metadata from a real user message before serialization. The wrapper is text seen by the model, not an API-level proof of origin. Avoid building security policy on the model merely recognizing a tag. The original note speculated that a user could inject an identical tag; without testing a current implementation's escaping, precedence, and trust handling, that should remain a security question, not an established exploit.
For how supported project instruction files load, use the current Claude Code memory documentation rather than depending on one observed internal message shape.
Rule 3: Keep turns understandable; strict alternation is not an API requirement
The old note called βuser/assistant/user/assistant alternationβ a hard API invariant and said two consecutive user messages would be rejected. The current Messages API reference says otherwise: consecutive user or assistant messages in a request are combined into a single turn. An application may still choose to merge adjacent same-role content itselfβperhaps to control formatting or simplify internal handlingβbut that is not because the API categorically forbids adjacency.
There are still good structural reasons to be careful:
- Preserve the intended causal order of user input, assistant tool requests, and results.
- Keep tool results attached to the right request IDs.
- Separate application-supplied context from what the real user said in your own data model.
- Do not mistake a text convention such as
<system-reminder>for a trusted role boundary.
Thus the third rule is semantic clarity, not a false claim of enforced alternation.
The three rules together
| Rule | What to preserve | What not to overstate |
|---|---|---|
| Coherent history | Order and meaning of request-visible context | That all requests are immutable append-only transcripts |
| Tool correspondence |
tool_use.id β tool_result.tool_use_id
|
A particular internal repair strategy or error string |
| Origin clarity | Real user content versus application context | Strict same-role alternation as a public API constraint |
These rules explain why higher-level features need care. Compaction must produce a coherent replacement for older context. Rewind and branching select a different history. Interruption handling must account for outstanding tool work. Subagents run with their own context. Project instructions and reminders are application-supplied context, not a new public message role.
The next article examines prompt caching: how a tool-using loop can reuse stable request prefixes and which changes break that reuse. The message structure matters because caching operates on the request that is actually sent, not on a conceptual transcript in isolation.
References
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.