Using the Anthropic Messages API with Hetzner Inference API

Last change on 2026-11-11 • Created on 2026-11-11 • ID: AI-7389D

Hetzner Inference API supports the /v1/messages and /v1/messages/count_tokens endpoints of the Anthropic Messages API. This means you can use the official Anthropic SDK and tools built on it with the models of Hetzner Inference API. This guide shows you how to configure the SDK and send your first requests.

Prerequisites

  • An Inference Token. For instructions, see Creating an Inference Token.

  • The Inference Token in the HETZNER_INFERENCE_TOKEN environment variable:

    export HETZNER_INFERENCE_TOKEN="<your-inference-token>"
  • For the Python or TypeScript examples, the Anthropic SDK:

    pip install anthropic
    npm install @anthropic-ai/sdk

Base URL and authentication

Two things are different for the Anthropic-compatible endpoints:

  • Base URL without /v1: The Anthropic SDK adds /v1 on its own. For this reason, set the base URL to https://inference.hetzner.com. For requests with cURL, use the full path, for example https://inference.hetzner.com/v1/messages.
  • Inference Token in the Authorization header: The API expects the Inference Token as Authorization: Bearer <your-inference-token>. The API does not support the x-api-key header that the Anthropic API usually uses. For this reason, pass the Inference Token to the SDK as auth_token (Python) or authToken (TypeScript).

You do not need the anthropic-version and anthropic-beta headers. If you send them, they have no effect.

Sending a first request

Use POST /v1/messages to send a message to a model. As in the Anthropic API, max_tokens is a required field.

cURL

curl https://inference.hetzner.com/v1/messages \
  -H "Authorization: Bearer $HETZNER_INFERENCE_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "zai-org/GLM-5.3-Flash",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

Python

import os
from anthropic import Anthropic

client = Anthropic(
    base_url="https://inference.hetzner.com",
    auth_token=os.environ["HETZNER_INFERENCE_TOKEN"],
)

message = client.messages.create(
    model="zai-org/GLM-5.3-Flash",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}],
)

for block in message.content:
    if block.type == "text":
        print(block.text)

TypeScript

import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  baseURL: "https://inference.hetzner.com",
  authToken: process.env.HETZNER_INFERENCE_TOKEN,
});

const message = await client.messages.create({
  model: "zai-org/GLM-5.3-Flash",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Hello!" }],
});

for (const block of message.content) {
  if (block.type === "text") console.log(block.text);
}

The answer is in the content field as a list of content blocks. You find the text in the blocks of the type text. As in the Anthropic API, pass the system prompt in the system field and not as a message in messages.

Controlling reasoning

Models such as GLM 5.3 Flash reason by default before they answer. With Hetzner Inference API, this also applies to /v1/messages. With the Anthropic API, thinking is turned off by default. The API returns the reasoning in content blocks of the type thinking.

Reasoning tokens are output tokens and count toward max_tokens. If max_tokens is too low, the model can use up the limit on reasoning. The response then contains no text, and stop_reason has the value max_tokens.

Use output_config.effort to set how much the model reasons. With the level low, GLM 5.3 Flash does not reason before it answers:

curl https://inference.hetzner.com/v1/messages \
  -H "Authorization: Bearer $HETZNER_INFERENCE_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "zai-org/GLM-5.3-Flash",
    "max_tokens": 1024,
    "output_config": {"effort": "low"},
    "messages": [{"role": "user", "content": "What is 2 + 2?"}]
  }'

In the Python SDK, pass the parameter with extra_body:

message = client.messages.create(
    model="zai-org/GLM-5.3-Flash",
    max_tokens=1024,
    extra_body={"output_config": {"effort": "low"}},
    messages=[{"role": "user", "content": "What is 2 + 2?"}],
)

Please note: The API also accepts the thinking parameter. However, with GLM 5.3 Flash, "thinking": {"type": "disabled"} does not fully turn off reasoning. The model still reasons briefly. For this reason, use output_config.effort.

To see which levels a model supports, go to Models and licenses.

Streaming responses

Set "stream": true to receive the answer in parts while the model generates it. The API uses the same events as the Anthropic API, for example message_start, content_block_delta, and message_stop.

cURL

curl -N https://inference.hetzner.com/v1/messages \
  -H "Authorization: Bearer $HETZNER_INFERENCE_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "zai-org/GLM-5.3-Flash",
    "max_tokens": 1024,
    "stream": true,
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

Python

with client.messages.stream(
    model="zai-org/GLM-5.3-Flash",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

print()

TypeScript

const stream = client.messages.stream({
  model: "zai-org/GLM-5.3-Flash",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Hello!" }],
});

stream.on("text", (text) => process.stdout.write(text));
await stream.finalMessage();

Sending images

GLM 5.3 Flash can process images. Send images as base64 in a content block of the type image. The API does not load images from a URL.

import base64

with open("photo.png", "rb") as f:
    image_data = base64.b64encode(f.read()).decode()

message = client.messages.create(
    model="zai-org/GLM-5.3-Flash",
    max_tokens=1024,
    messages=[{
        "role": "user",
        "content": [
            {
                "type": "image",
                "source": {"type": "base64", "media_type": "image/png", "data": image_data},
            },
            {"type": "text", "text": "What is in this image?"},
        ],
    }],
)

A request, including the images, can be up to 32 MiB in size and must fit into the context window of the model. Images as base64 are about one third larger than the original file. For details, see Limits of Hetzner Inference API. The API does not support documents such as PDF files. Extract the text yourself and send it as a text block.

Counting tokens

Use POST /v1/messages/count_tokens to find out how many input tokens a request has without sending it to the model:

count = client.messages.count_tokens(
    model="zai-org/GLM-5.3-Flash",
    messages=[{"role": "user", "content": "Hello!"}],
)

print(count.input_tokens)

Depending on the model, the settings for thinking and output_config change the number of input tokens. For this reason, pass the same settings as in the actual request.

Connecting tools with environment variables

Many tools based on the Anthropic SDK read the connection details from environment variables. This applies to many coding agents, for example. Set the following variables:

export ANTHROPIC_BASE_URL="https://inference.hetzner.com"
export ANTHROPIC_AUTH_TOKEN="$HETZNER_INFERENCE_TOKEN"

In the configuration of the tool, enter a model name of Hetzner Inference API as the model, for example zai-org/GLM-5.3. The API does not map Anthropic model names such as claude-... to a Hetzner model.

Next steps

Table of Contents