Anthropic Messages API

Use Anthropic's SDKs with Morse: send messages, stream them, count tokens, and see what Morse refuses.

Give Anthropic's SDK the base URL https://inference.trymorse.ai and your Morse key, and name a Morse model.

pip install anthropic
import os
import anthropic

client = anthropic.Anthropic(
    base_url="https://inference.trymorse.ai",
    api_key=os.environ["MORSE_API_KEY"],
)

message = client.messages.create(
    model="moonshotai/kimi-k3.1-pro",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)
print(message.content[0].text)

Every chat model serves Messages, with the same prices, privacy tiers, and failover as chat completions. Morse keeps no state between requests. For Claude Code, see Use Claude Code with a private API.

Send the key

Send the key in x-api-key, as Anthropic's SDK does, or as a Bearer token. Morse ignores anthropic-version, anthropic-beta, and the query string.

Stream a message

with client.messages.stream(
    model="moonshotai/kimi-k3.1-pro",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
  • A stream sends Anthropic's named events and ends with message_stop, with no [DONE].
  • Morse sends a ping event every 15 seconds while a model reasons without showing it.
  • Ignore the obfuscation string in each event's data. Its random length hides the event's size.
  • A stream that fails after it starts ends with an error event and no message_stop.

Count tokens

count = client.messages.count_tokens(
    model="moonshotai/kimi-k3.1-pro",
    messages=[{"role": "user", "content": "Hello"}],
)
print(count.input_tokens)
  • Counting is free, needs no balance, and runs no model.
  • Each account may count 60 times a minute, on every plan.
  • On a model whose provider publishes no tokenizer, or for a request with an image, the count is an upper bound.

See Count a message's tokens.

Thinking and effort

  • Set output_config.effort to low, medium, high, xhigh, or max.
  • Or set thinking to enabled with budget_tokens, and Morse picks an effort from the budget.
  • thinking of disabled asks for the effort none. A model without none runs at its lowest effort.
  • Thinking blocks stream where the model shows its reasoning. They carry no signature.

See Reasoning for how each model takes an effort.

Sampling settings

A temperature or top_p other than 1, and stop_sequences, reach the provider only while Let my settings reach the model host on the key's page in the console is on. With it off, the request returns 400 invalid_request_error, which names the parameter.

Send Morse's own parameters

Put provider in the body to set a privacy floor for one request. See Provider routing.

Python
client.messages.create(
    model="moonshotai/kimi-k3.1-pro",
    max_tokens=1024,
    messages=messages,
    extra_body={"provider": {"min_privacy": "tee"}},
)

What Morse refuses

Each of these returns 400 invalid_request_error, and the message names it.

SentWhat to do
Server tools, such as web_search and code_executionDefine the tool yourself, as a custom tool.
document blocksSend the text in a text block.
Images linked by URLSend the image as a base64 source.
output_config.format of a type other than json_schemaUse json_schema. See Structured outputs.
context_management, container, and mcp_serversLeave them out.

Morse takes and drops a context_management that asks to clear nothing, as Claude Code sends it. It ignores cache_control, metadata, and service_tier. Errors come in Anthropic's shape, with one exception: see Errors.