Marking Relayed Mid-Turn Messages Synthetic in Claude Code¶
Without fetched feature flags, Claude Code 2.1.332 counts Opus 4.8+ SDK and
claude -pmid-turn messages as user input; mark relayed textisSynthetic: true.
When an Agent SDK or claude -p harness injects non-human text as a user message mid-turn, send it as its own message with isSynthetic: true, origin unset, and client_composed: true (TypeScript SDK reference). Claude Code 2.1.332 (November 25, 2026) "Changed SDK and claude -p mid-turn messages to count as the user’s where feature flags aren’t fetched, as on clouds or with telemetry off, from Opus 4.8 on; mark relayed text isSynthetic: true" (changelog 2.1.332). Apply this contract to relayed non-human text only; direct human prompts and host-declared scheduled runs use separate origin fields.
When feature flags are not fetched¶
Claude Code skips fetching feature flags from Anthropic in three session classes (environment variables reference):
- Sessions disabling fetching through
CLAUDE_CODE_DISABLE_FEATURE_FLAGS,DISABLE_TELEMETRY,DO_NOT_TRACK, orCLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC, using the documented values for each variable - Third-party provider sessions on Amazon Bedrock, Claude Platform on AWS, Google Cloud's Agent Platform, or Microsoft Foundry, unless a host sets
CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST - Claude apps gateway sessions
Two other setups start without fetched flags on every run: clean CI containers without flags saved by an earlier session, and sessions authenticating with ANTHROPIC_AUTH_TOKEN to a non-Anthropic ANTHROPIC_BASE_URL with no API key (environment variables reference).
For DISABLE_TELEMETRY and CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC, "any non-empty value including 0 turns the behavior on" (environment variables reference). Setting DISABLE_TELEMETRY=0 or false still disables flag fetching; unset the variable to remove that opt-out. DO_NOT_TRACK=0 leaves telemetry on (variables reference).
The three-field contract for relayed text¶
For text the user did not write (such as a fetched page or another agent's output), each field enforces one part of the provenance boundary:
isSynthetic: truemarks the message as non-user input, and Claude Code assigns it theunclassifiedorigin kind on v2.1.223 and later (TypeScript SDK reference).- Leaving
originunset avoids a silent fallback to user authority. Callers may set only{ kind: "human" }or the declared scheduled-runtask-notificationkind, and must leave every other kind (such aspeerorchannel) to Claude Code and Anthropic servers: "If you set one of those kinds, Claude reads the message as the user's by default" (TypeScript SDK reference). client_composed: truedelivers relayed text verbatim. "Claude Code then doesn't expand@pathmentions, and doesn't run text that starts with/as a command" (TypeScript SDK reference). This field requires TypeScript Agent SDK v0.3.280 or later and Claude Code v2.1.248 or later.
Each caller message class carries a distinct field combination (TypeScript SDK reference):
| Message class | isSynthetic |
origin |
client_composed |
|---|---|---|---|
| Relayed non-human text | true |
Unset | true |
| Direct human prompt | Unset | { kind: "human" } |
Unset |
| Host-declared scheduled run | Unset | { kind: "task-notification", subkind: "scheduled-trigger", fireReason: "scheduled" } |
Not specified |
Declared scheduled runs also require CLAUDE_CODE_HOST_SCHEDULED_RUN=1 in env, with neither CLAUDECODE nor CLAUDE_CODE_CHILD_SESSION set; otherwise Claude Code ignores the declaration (TypeScript SDK reference).
Turn timing and reply attribution¶
In streaming SDK sessions, priority sets mid-turn timing. With 'next' or priority omitted, "Claude reads the message in the same turn, as soon as the tool calls it is running finish" (TypeScript SDK reference). With 'later', Claude Code holds the message for a new turn. Headless CLI runs send structured streaming input through claude -p --input-format stream-json --output-format stream-json --verbose (CLI reference).
Setting isSynthetic: true changes user_message_uuid echoes. A regular message owns its turn for the whole run. On a synthetic message, "the turn answers that message at first. If Claude Code picks up a regular message of yours between tool calls, the turn answers the picked-up message from then on" (TypeScript SDK reference). Echoing a synthetic message's uuid requires Agent SDK v0.3.265 or later; earlier versions echo nothing on synthetic turns.
Why it works¶
Instruction-hierarchy training targets a root cause of prompt injection: "LLMs often consider system prompts (e.g., text from an application developer) to be the same priority as text from untrusted users and third parties" (Wallace et al., 2024). The hierarchy sets explicit precedence where "system messages take precedence over user messages, and user messages take precedence over third-party content" (Wallace et al., 2024).
When a harness injects a fetched page or another agent's output as an unmarked mid-turn message on Opus 4.8+ without fetched flags, Claude Code 2.1.332 counts that text as user input (changelog 2.1.332). Setting isSynthetic: true keeps it out of the human channel (unclassified), and client_composed: true blocks slash-command execution and @path expansion on the same payload (TypeScript SDK reference).
When this backfires¶
- Marking genuine human mid-turn input as synthetic removes human provenance. Checks requiring
{ kind: "human" }fail, including theultracodeworkflow keyword (TypeScript SDK reference, origin) and'now'backgrounding of active shell, subagent, or MCP calls on Claude Code v2.1.286+ (TypeScript SDK reference, priority). - Setting
isSynthetic: trueon host-declared scheduled runs violates the scheduled-run contract, which requirestask-notificationwithsubkind: "scheduled-trigger"and noisSynthetic(TypeScript SDK reference). - Setting
isSynthetic: truewithoutclient_composed: trueleaves untrusted relayed text able to trigger leading/commands or@pathfile expansion (TypeScript SDK reference). - Relying on
isSynthetic: truealone leaves tool execution exposed. Anthropic's Agent SDK guide states: "Defense in depth is still good practice though. For example, if an agent processes a malicious file that instructs it to send customer data to an external server, network controls can block that request entirely" (secure deployment guide). - Lower-priority signals carry an over-refusal cost. Wallace et al. note: "A key risk is that our models learn to never follow lower-priority instructions; in reality, we only want models to ignore lower-priority instructions when they conflict with higher-priority ones" (Wallace et al., 2024).
- Harnesses that keep external data inside tool results and send only human text as user messages have no relayed user messages to classify. Adding synthetic routing there adds complexity without closing an active path.
Example¶
An orchestrator running the TypeScript Agent SDK on Amazon Bedrock (where feature flags are not fetched) relays another agent's output mid-turn while the main agent runs tools.
Before — unmarked relay counts as user input on Opus 4.8+ and leaves / command and @path expansion enabled:
const relayed: SDKUserMessage = {
type: "user",
message: { role: "user", content: workerReportText },
parent_tool_use_id: null,
};
After — marked synthetic, verbatim, and with origin unset:
const relayed: SDKUserMessage = {
type: "user",
message: { role: "user", content: workerReportText },
parent_tool_use_id: null,
isSynthetic: true,
client_composed: true,
};
Setting isSynthetic: true with origin unset lets Claude Code assign unclassified instead of treating the text as user-authored, and client_composed: true stops any leading / or @path token in workerReportText from running or expanding.
Key Takeaways¶
- Audit SDK and
claude -p --input-format stream-jsonsenders before upgrading to Claude Code 2.1.332 on Opus 4.8+ in cloud-provider, gateway, CI-container, or telemetry-off deployments. - Send relayed non-human text as its own message with
isSynthetic: true,originunset, andclient_composed: true, and never set a reservedoriginkind such aspeer. - Keep human steering on
{ kind: "human" }and declared scheduled runs ontask-notificationwithoutisSynthetic, because mixing classes breaks human-only checks anduser_message_uuidattribution. - Pair
isSynthetic: truewith permission rules, sandboxing, and network controls; the flag signals provenance to the runtime and model and does not confine tool execution.
Related¶
- Non-Human Event Provenance Markers to Block Fabricated Approvals — harness-emitted non-human provenance markers for background task notifications and transcripts
- Authority Confusion: Untrusted Context Must Not Authorize Side Effects — dispatch-layer separation between untrusted context and action authorization
- Authority Disclaimers Read as Permission to Skip a Step — how an unbounded non-user-authority note can trigger over-refusal in delegated steps
- Steering Running Agents: Mid-Run Redirection and Follow-Ups — mid-turn redirection and queued follow-up mechanics across agent harnesses
- Claude Agent SDK — overview of the Claude Code runtime exposed as a TypeScript and Python library