Create Message

Send a list of messages and get back the next assistant turn as content blocks. Supports streaming, tool use, extended thinking, and image and document input.

Creates the next assistant turn for a conversation in the Anthropic Messages format. Supports both streaming and non-streaming modes. Any Anthropic SDK works out of the box - just point its base URL at https://api.aivene.com.

POST /v1/messages

Authentication

x-api-keystringoptional

API key, as sent by the Anthropic SDKs. Either this header or Authorization is required.

AuthorizationBeareroptional

API key as a bearer token. Takes precedence over x-api-key when both are sent. Create keys at Manage API Keys.

Headers

Content-Typestringrequired

Must be application/json.

anthropic-betastringoptional

Comma-separated beta flags, as sent by the Anthropic SDKs. Flags that are not supported are ignored rather than rejected.

Body

modelstringrequired

Model id from GET /v1/models. Example: claude-sonnet-4-6, claude-opus-4-7, gpt-5.4.

messagesMessage[]required

Conversation history, alternating user and assistant turns. Must contain at least one message. See Message shape.

max_tokensintegerrequired

Maximum number of tokens to generate, thinking included. Must not exceed the model's output limit.

systemstring | TextBlock[]optional

System prompt. Pass an array of text blocks to put cache_control on part of it.

streambooleanoptionalDefault false

Stream the response as named server-sent events instead of returning a single JSON body. See Streaming events.

temperaturenumberoptionalDefault 1

Between 0 and 1. Higher = more random.

top_pnumberoptional

Between 0 and 1. Nucleus sampling. Use temperature or top_p, not both.

top_kintegeroptional

Sample only from the top K options for each token.

stop_sequencesstring[]optional

Custom sequences that stop generation. When one is hit, stop_reason is stop_sequence and stop_sequence names it.

toolsTool[]optional

Tools the model may call. See Tool use.

tool_choiceobjectoptional

How the model uses tools:

  • { type: 'auto' } - the model decides (default).
  • { type: 'any' } - the model must call one of the tools.
  • { type: 'tool', name } - the model must call the named tool.
  • { type: 'none' } - the model must not call a tool.

auto, any and tool also take disable_parallel_tool_use: true to allow at most one tool call per turn.

thinkingobjectoptional

Extended thinking, on models that support it:

  • { type: 'enabled', budget_tokens } - think with a token budget. The budget counts toward max_tokens.
  • { type: 'adaptive' } - the model decides how much to think. Combine with output_config.effort.
  • { type: 'disabled' } - no thinking.

display (summarized or omitted) controls whether thinking text is returned, on models that support it. See Reasoning.

output_configobjectoptional

Output controls, on models that support them:

  • effort - low, medium, high, xhigh or max. How much effort the model spends, thinking included.
  • format - { type: 'json_schema', schema } asks the model to return JSON that follows your schema. Validate the output if your app relies on it.
cache_controlobjectoptional

{ type: 'ephemeral' } at the top level turns on automatic prompt caching. You can also put cache_control on individual content blocks, system blocks and tools. See Prompt caching.

context_managementobjectoptional

Context editing, for example clearing old tool results from long conversations, on models that support it. Requires the matching anthropic-beta flag.

metadataobjectoptional

{ user_id } - an opaque identifier for your end user. Do not send names, email addresses or other personal data.

Message shape

type Message =
  | { role: 'user'; content: string | ContentBlock[] }
  | { role: 'assistant'; content: string | ContentBlock[] };

A string content is shorthand for a single text block:

[
  {
    "role": "user",
    "content": [
      { "type": "image", "source": { "type": "url", "url": "https://example.com/cat.png" } },
      { "type": "text", "text": "What is in this image?" }
    ]
  }
]

Content block types

TypeUsed inDescription
textuser, assistantPlain text: { "type": "text", "text": "Hello" }.
imageuserImage by source: base64 (media_type, data), url, or file (file_id from Files).
documentuserDocument by source: base64 PDF, url, plain text, or file (file_id). Optional title, context, and citations: { enabled: true }.
tool_useassistantA tool call the model made: id, name, input. Send it back unchanged.
tool_resultuserYour result for a tool call: tool_use_id, content (string or blocks), optional is_error.
thinkingassistantThinking from an earlier turn, with its signature. Send it back unchanged.
redacted_thinkingassistantEncrypted thinking from an earlier turn. Send it back unchanged.

Any block except thinking and redacted_thinking can carry cache_control.

Images and documents

image and document blocks are only accepted by models with the matching input capability. Check GET /v1/models. See Image understanding and Documents.

Response

idstringoptional

Unique message id, prefixed avn_.

type'message'optional

Always message.

role'assistant'optional

Always assistant.

modelstringoptional

The model id you sent in the request.

contentContentBlock[]optional

The reply as text, tool_use, thinking and redacted_thinking blocks, in order. text blocks can carry citations when you enabled them on a document.

stop_reasonstringoptional

Why the model stopped: end_turn, max_tokens, stop_sequence, tool_use, or refusal.

stop_sequencestring | nulloptional

The custom stop sequence that was hit, if any.

usageobjectoptional

input_tokens, output_tokens, cache_creation_input_tokens and cache_read_input_tokens. input_tokens excludes cached tokens, so the total input is the sum of the three input fields. Always present: on the response, or on the stream's message_start and message_delta events.

Streaming

When stream: true, the response is text/event-stream with named events (message_start, content_block_start, content_block_delta, content_block_stop, message_delta, message_stop). There is no [DONE] sentinel: the stream closes after message_stop. See Streaming events.

Tool use

Provide a tools array. A custom tool has a name, a description and a JSON Schema input_schema:

{
  "name": "get_weather",
  "description": "Get the current weather for a city.",
  "input_schema": {
    "type": "object",
    "properties": { "city": { "type": "string" } },
    "required": ["city"]
  }
}

When the model calls a tool, the reply contains a tool_use block and stop_reason is tool_use. Run the tool yourself, then send a new request that appends the assistant turn unchanged and a user turn with a tool_result block whose tool_use_id matches. The model then continues.

Custom tools work with every model that supports tool use. On Claude models you can also use the Anthropic-defined client tools that your own code executes: bash_*, text_editor_*, memory_* and computer_*. See Tool calling.

Extended thinking

With thinking enabled, the reply starts with thinking blocks (or redacted_thinking blocks) before the answer. When you continue the conversation, for example after a tool call, send those blocks back unchanged in the assistant turn so the model keeps its train of thought. Don't edit them: the signature is checked. See Reasoning.

Errors

Errors returned before the response starts share one shape:

{ "error": { "message": "...", "type": "invalid_request_error" } }
Statuserror.typeMeaning
400validation_errorThe body failed schema validation. The message names the offending field.
400invalid_request_errorUnknown model, content or a feature the model does not support (for example images, tools or output_config.format), or max_tokens above the model's limit.
401authentication_errorMissing or invalid API key.
402invalid_request_errorNot enough credit for the request, an unpaid balance, or a spend limit was reached.
403permission_denied_errorThe account is suspended, an Admin key was used, or the model isn't included in your organization's tier.
404not_found_errorThe model is deprecated. The message names its replacement.
408timeout_errorThe model did not finish before the request timed out (non-streaming requests).
429rate_limit_errorYour organization hit its rate limit for this model. Back off and retry.
500internal_server_errorUnexpected server error.
502 / 503upstream_errorThe model is temporarily unavailable. Safe to retry.

When the model itself rejects a request, the response keeps the model's 4xx status with type upstream_error or invalid_request_error, and error.code names the cause when it is known (context_length_exceeded, content_filter, request_too_large). Errors after a stream has started arrive as an error event.