Anthropic Messages

Create a message

Changed on

Creates a message using the Anthropic Messages API format. Supports text, images, PDFs, tools, and extended thinking.

post/messages

Request

  • Base URL
    https://openrouter.ai/api/v1
  • Auth
    HTTP bearer

Headers

X-OpenRouter-Metadata, 'disabled' | 'enabled'

Opt-in level for surfacing routing metadata on the response under openrouter_metadata.

Example:enabled

Opt-in to surface routing metadata on the response under openrouter_metadata. Defaults to disabled. The legacy header X-OpenRouter-Experimental-Metadata is also accepted for backward compatibility.

Request body

MessagesRequest required

Request schema for Anthropic Messages API endpoint

cache_control, AnthropicCacheControlDirective

Enable automatic prompt caching. When set at the top level, the system automatically applies cache breakpoints to the last cacheable block in the request. When set on an individual content block, it marks an explicit cache breakpoint; block-level markers also work on OpenAI models that support explicit prompt caching — OpenRouter converts them to the provider's native format.

context_management, object nullable
deferred_tools, DeferredToolsControl

Opt-in versioned router-level deferred-tool protocol. Replay assistant reasoning unchanged on continuation. The portable profile needs the catalog unchanged across a continuation; the universal profile accepts a changed catalog and, with announce_catalog_changes, tells the model about it.

fallbacks, MessagesFallbackParam[] nullable

Fallback models to try if the primary model fails or refuses, in order. Handled by OpenRouter multi-model routing rather than Anthropic server-side fallbacks; cannot be combined with models. Each entry accepts only model. Maximum of 3 entries.

max_tokens, integer
messages, MessagesMessageParam[] nullable required
metadata, object
model, string required
models, string[]
output_config, MessagesOutputConfig

Configuration for controlling output behavior. Supports the effort parameter and structured output format.

plugins, union[]

Plugins you want to enable for this request, including their settings.

provider, ProviderPreferences nullable

When multiple model providers are available, optionally indicate your routing preference.

route, 'fallback' | 'sort' | 'null' nullable

DEPRECATED Use providers.sort.partition instead. Backwards-compatible alias for providers.sort.partition. Accepts legacy values: "fallback" (maps to "model"), "sort" (maps to "none").

safeguards, AnthropicSafeguard[] nullable
service_tier, string
session_id, string

A unique identifier for grouping related requests (e.g., a conversation or agent workflow). When provided, OpenRouter uses it as the sticky routing key, routing all requests in the session to the same provider to maximize prompt cache hits. Also used for observability grouping. If provided in both the request body and the x-session-id header, the body value takes precedence. Maximum of 256 characters.

speed, intersection

Controls output generation speed. When set to fast, uses a higher-speed inference configuration at premium pricing. Defaults to standard when omitted.

stop_sequences, string[]
stop_server_tools_when, StopServerToolsWhenCondition[]

Stop conditions for the server-tool agent loop. Any condition firing halts the loop (OR logic). When set, this overrides max_tool_calls. When a condition fires while the model is still emitting tool calls, the pending tool calls are executed and one final turn is made with tool calls disabled so the response ends with a natural-language answer instead of an unfinished tool call.

stream, boolean
system, union
temperature, number double
thinking, union
tool_choice, union
tools, union[]
top_k, integer
top_p, number double
trace, TraceConfig

Metadata for observability and tracing. Known keys (trace_id, trace_name, span_name, generation_name, parent_span_id) have special handling. Additional keys are passed through as custom metadata to configured broadcast destinations.

user, string

A unique identifier representing your end-user, which helps distinguish between different users of your app. This allows your app to identify specific users in case of abuse reports, preventing your entire app from being affected by the actions of individual users. Maximum of 256 characters.

Example request

{
  "max_tokens": 1024,
  "messages": [
    {
      "content": "Hello, how are you?",
      "role": "user"
    }
  ],
  "model": "anthropic/claude-4.5-sonnet-20250929",
  "temperature": 0.7
}

Response

Successful response

MessagesResult required

Non-streaming response from the Anthropic Messages API with OpenRouter extensions

object
context_management, object nullable
openrouter_metadata, OpenRouterMetadata
provider, string
safeguard_results, AnthropicSafeguardResult[] nullable
usage, intersection

Example response

{
  "container": null,
  "content": [
    {
      "citations": [],
      "text": "Hello! I'm doing well, thank you for asking.",
      "type": "text"
    }
  ],
  "id": "msg_01XFDUDYJgAACzvnptvVoYEL",
  "model": "claude-sonnet-4-5-20250929",
  "role": "assistant",
  "stop_details": null,
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "type": "message",
  "usage": {
    "cache_creation": null,
    "cache_creation_input_tokens": null,
    "cache_read_input_tokens": null,
    "inference_geo": null,
    "input_tokens": 12,
    "output_tokens": 15,
    "output_tokens_details": null,
    "server_tool_use": null,
    "service_tier": "standard"
  }
}

Changes

    • warning

      added the new Celeris enum value to the data/oneOf[#/components/schemas/MessagesStartEvent]/message/provider response property for the response status

    • info

      added the new optional request property provider/options/celeris

    • info

      added the new Celeris enum value to the request property provider/ignore/items/anyOf[#/components/schemas/ProviderName]

    2 more changes in this revision