Send mid-conversation system prompts as native `system` messages on Anthropic

Merged
DO
4mo ago
+2301-10

Anthropic's Messages API accepts a {"role": "system"} entry inside messages. It lets an application add an operator instruction partway through a conversation without rewriting the top-level system parameter — which sits at the very front of the prompt and would invalidate the cached prefix behind it.

A SystemPromptPart outside the first ModelRequest already means "mid-conversation instruction" — that's what _wrap_non_leading_system_prompts has encoded since #5509, and it's what RunContext.enqueue(SystemPromptPart(...)) produces. So there's no new API surface here: this changes only how that existing meaning is rendered on models that support it natively.

Behavior

AnthropicProvider.model_profile() sets supports_inline_system_prompts for the models that accept the role, and AnthropicModel._map_message emits the entry directly after the request's user turn.

Everywhere else the existing <system>-tagged user rendering is kept rather than sending a request the API would reject:

  • in leading position, or where the request contributes no user turn (the API requires a system entry to follow a user message);
  • on the legacy Bedrock, Vertex, and Foundry transports, which don't serve the role.

Model support

Verified against the live API: claude-opus-4-8, claude-sonnet-5, and claude-fable-5 accept it; claude-opus-4-7, claude-opus-4-6, claude-sonnet-4-6, and claude-haiku-4-5 reject it with role 'system' is not supported on this model. claude-mythos-5 is included per the docs (no access to verify). claude-opus-5 is not in the tuple because it isn't in _known_model_names on main yet — it should be added with #6723.

Note the published docs list Sonnet 5 as unsupported; that is wrong — it both accepts the role and honors it.

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • PR title is fit for the release changelog.