Create a message
Generate an answer using the Anthropic Messages protocol.
Draft API contract. The hostname, model ID, authentication, and endpoint are examples pending Koah validation. Use issued connection details after approval. The example response does not come from a live Koah service.
Use the host root as the Anthropic SDK base URL. The SDK appends /v1/messages.
Configure the connection
before using the example.
Headers
- Name
- Authorization
- Type
- string
- Required
- required
- Description
The example gateway uses
Bearerfollowed by the issued inference key. The Python example adds this header because the Anthropic SDK normally usesx-api-key. Koah's accepted authentication still needs confirmation.
- Name
- Content-Type
- Type
- string
- Required
- required
- Description
application/json.
- Name
- anthropic-version
- Type
- string
- Required
- required
- Description
The example uses
2023-06-01. The SDK sends its version header automatically; the gateway must confirm which version it accepts.
Body parameters
- Name
- model
- Type
- string
- Required
- required
- Description
The approved provider model ID. The Anthropic client does not imply a Claude model.
- Name
- max_tokens
- Type
- integer
- Required
- required
- Description
The maximum output token count. The example uses
1024; this is not a published Koah limit. Confirm the allowed value for the chosen model.
- Name
- messages
- Type
- object[]
- Required
- required
- Description
Conversation turns with
userorassistantroles and message content. Put the system instruction insystem, not in a system-role message.
- Name
- system
- Type
- string
- Description
A top-level instruction. The example uses a text string.
- Name
- stream
- Type
- boolean
- Description
Request Messages events when streaming is validated for the model.
- Name
- tools
- Type
- object[]
- Description
Messages-format tool definitions where supported. Tool schemas and returned blocks differ from Chat Completions. The host owns tool execution.
Response
The example returns a 200 Messages JSON object for a completed request.
- Name
- id
- Type
- string
- Description
The message identifier.
- Name
- content
- Type
- object[]
- Description
Content blocks with a
type. Readtextfrom text blocks; do not assume every block contains text. Messages responses do not containchoices.
- Name
- stop_reason
- Type
- string
- Description
The stop reason. The example uses
end_turn; other values depend on model behavior.
- Name
- usage
- Type
- object
- Description
Example
input_tokensandoutput_tokenscounts. These are illustrative, not a confirmed settlement or accounting contract.
Streaming
Use stream: true or the SDK's messages.stream helper. Messages events have
names such as content_block_delta and message_stop. See
Streaming; do not parse them as Chat Completions chunks.
Errors and compatibility
Follow the error handling guide. Messages compatibility
alone does not enable vendor-hosted tools, message batches, token counting, or
managed agents. Keep the ad placement separate from the answer.
Koah requests an ad only when it returns the proposed X-Koah-Ad-Id response
header. Not every response includes it. If present, forward the ID to the
client and call window.koah.displayAd(adId, { target }). If absent, show the
content without an ad. The
quickstart shows the candidate
response header; the Messages body does not contain an ad payload.
The compatibility shape follows the Messages reference.
import os
from anthropic import Anthropic
with Anthropic(
base_url="https://inference.koah.ai",
api_key=os.environ["KOAH_INFERENCE_API_KEY"],
default_headers={"Authorization": f"Bearer {os.environ['KOAH_INFERENCE_API_KEY']}"},
timeout=60.0,
max_retries=0,
) as client:
message = client.messages.create(
model=os.environ["KOAH_INFERENCE_MODEL"],
max_tokens=1024,
system="Suggest short, memorable product names.",
messages=[{"role": "user", "content": "Name a community garden app. Give three options."}],
)
for block in message.content:
if block.type == "text":
print(block.text)
Response
{
"id": "msg-example",
"type": "message",
"role": "assistant",
"model": "sail/DeepSeek-V4.1-Flash-1017-free",
"content": [
{
"type": "text",
"text": "1. Garden Circle\n2. Shared Soil\n3. Sprout Together"
}
],
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": { "input_tokens": 32, "output_tokens": 18 }
}