Messages API

Send a conversation to POST /v1/messages and get the model's reply as content blocks.

POST /v1/messages takes a conversation and returns the model's next message.

request
curl "$KEYDAN_API_BASE/v1/messages" \
  -H "x-api-key: $KEYDAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "kd-balanced",
    "max_tokens": 512,
    "system": "You are a concise assistant.",
    "messages": [
      {"role": "user", "content": "Say hello in three languages."}
    ]
  }'
response
{
  "id": "msg_0a1b2c3d",
  "type": "message",
  "role": "assistant",
  "model": "kd-balanced",
  "content": [
    {"type": "text", "text": "Hello! Bonjour! Hola!"}
  ],
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 24,
    "output_tokens": 9,
    "cache_creation_input_tokens": 0,
    "cache_read_input_tokens": 0
  }
}

Request body

Request body
FieldTypeNotes
modelstringrequired: a model id from the catalogue
max_tokensintegerrequired: the most tokens the answer may have
messagesarrayrequired: turns with role user or assistant; content is a string or a list of blocks (text, image, tool_use, tool_result)
systemstring or arrayinstructions for the model, as a string or text blocks
temperature, top_p, top_knumbersampling controls
stop_sequencesarraystrings that end the answer
streambooleananswer as server-sent events (streaming)
tools, tool_choicearray, objectfunction calling (tools)
metadataobjectoptional request metadata

Images

Send images as image blocks with a base64 source. Check the model's capabilities in the catalogue.

images, caching, stop sequences
curl "$KEYDAN_API_BASE/v1/messages" \
  -H "x-api-key: $KEYDAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "kd-balanced",
    "max_tokens": 1024,
    "temperature": 0.2,
    "system": [
      {"type": "text", "text": "You answer questions about the attached guide.",
       "cache_control": {"type": "ephemeral"}}
    ],
    "messages": [
      {"role": "user", "content": [
        {"type": "text", "text": "What does this chart show?"},
        {"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "iVBORw0KGgo..."}}
      ]}
    ],
    "stop_sequences": ["END"]
  }'

Prompt caching

Mark the end of a long, stable prefix (instructions, documents, tool definitions) with "cache_control": {"type": "ephemeral"}. Later requests that start with the same prefix read it from the cache at the cache-read price. The answer's usage shows cache_creation_input_tokens (written, billed at the cache-write price) and cache_read_input_tokens (read).

Response

  • content: a list of blocks: text, and tool_use when the model calls a tool.
  • stop_reason: end_turn, max_tokens, stop_sequence or tool_use.
  • usage: input, output, cache-write and cache-read tokens of this request.
  • model: the model id you sent.

Headers: x-request-id, the limit headers, and for subscriptions the window headers.