Best practices for live operation of Hetzner Inference API

Last change on 2026-11-11 • Created on 2026-11-11 • ID: AI-8F973

This page sums up what you should keep in mind when you use Hetzner Inference API in a production application. For details on each topic, see the linked articles.

Separating Inference Tokens by application

Create a separate Inference Token for each application and each environment, for example for development, testing, and production. This has several advantages:

  • Each Inference Token has its own consumption cap. An error in the test environment does not use up the production budget.
  • The rate limit applies per Inference Token and model. Requests from the test environment do not slow down production.
  • Hetzner Console shows the consumption of each Inference Token separately.
  • If an Inference Token gets into the wrong hands, you only delete this one Inference Token.

Use labels to assign Inference Tokens to a team or an application, for example. You can create up to 20 Inference Tokens per account. For details, see Limits of Hetzner Inference API.

Storing Inference Tokens securely

  • Store Inference Tokens in environment variables or in a secret manager. Do not write them into your code, and do not commit them to a repository.
  • Only use Inference Tokens on the server. Code that runs in the browser or in an app on the user's device must not contain an Inference Token.
  • Replace Inference Tokens regularly. To learn how to do this without downtime, see Managing Inference Tokens.

Handling errors

Always use the HTTP status code to handle errors in your application. Some error texts arrive as plain text, and their wording can change.

Not every error goes away when you retry the request:

Status code Retry?
400, 401, 404 No. Fix the request, the Inference Token, or the model name first.
429 (rate limit) Yes, after the time in the Retry-After header.
429 (consumption cap) No. Increase the consumption cap or wait until the next calendar month.
504 Yes, after you shorten the request. For details, see Timeouts.
Other status codes from 500 Yes, after waiting.

Retry requests with exponential backoff. This means you wait longer before each new attempt than before the previous one, and you add a small random delay. This prevents many requests from failing at the rate limit again at the same time. With a 429, the Retry-After header tells you after how many seconds the API accepts requests again. Wait at least this long.

The official OpenAI and Anthropic SDKs automatically retry requests after 429 and after status codes from 500, twice by default. Use max_retries to change the number:

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://inference.hetzner.com/v1",
    api_key=os.environ["HETZNER_INFERENCE_TOKEN"],
    max_retries=4,
)

If requests still fail with 429 after several attempts, check in Hetzner Console whether the Inference Token has reached its consumption cap. For all status codes and their causes, see Error codes of Hetzner Inference API.

Timeouts

On the server side, a request has 300 seconds, with or without reasoning. This also applies to requests with streaming. If a request takes longer, the API cancels it. For requests without streaming, the API then returns status code 504 Gateway Timeout. For details, see Limits of Hetzner Inference API.

  • Shorten requests: High reasoning levels in particular make a request take longer. Set a lower max_tokens value, choose a lower reasoning level, or split large tasks into several smaller requests.
  • Use streaming: With "stream": true, your users see the answer while the model generates it. The limit of 300 seconds also applies here.
  • Set a generous client timeout: If you set your own timeout in your client, do not set it too short. A short timeout cancels long requests that the API would still answer.

Controlling costs

  • Consumption cap: Set a consumption cap for each Inference Token that fits its use. When the Inference Token reaches the cap, the API rejects further requests.
  • Cost alert: In Hetzner Console, you can set up an email alert for each project under Usage. The costs there also include the consumption of the Inference API. Note that the amounts on this page include VAT. The consumption cap excludes VAT.
  • max_tokens: Limit the length of answers so that single requests do not get unexpectedly expensive.
  • Reasoning level: Reasoning tokens are output tokens. For simple tasks, choose a low level with reasoning_effort.
  • Prompt caching: Cached input tokens cost less. Put parts that do not change at the start of your prompt. This includes the system prompt, tool definitions, and long documents. Put the parts that change at the end. Very short prompts are never cached.

To learn how costs are calculated, see Billing for Hetzner Inference API.

Managing the conversation history

The API does not store requests or responses. For a conversation with several messages, you therefore send the full history with every request. Every message in the history counts as input tokens again.

  • Shorten long conversations. Remove old messages or replace them with a short summary.
  • Make sure that the history and the answer fit into the context window of the model together.
  • Avoid changing the start of the history. This way, the API can reuse it from the cache.

Monitoring consumption

  • usage field: Every response contains the number of input, output, and cached tokens in the usage field. Store these values in your logs. This way, you notice early when single requests or features of your application use an unusual number of tokens.
  • Hetzner Console: The details of an Inference Token show the consumption in the current calendar month per model.
  • Status page: We announce incidents and maintenance of the Inference API on status.hetzner.com.

Preparing for model changes

When we shut down a model, we usually announce it two months in advance. To be able to react quickly:

  • Set the model name in the configuration of your application, for example in an environment variable. Do not hard-code it.
  • Test your application with a second model in advance that you can switch to if needed.
  • Keep in mind that models have different capabilities. Not every model can process images, and the reasoning levels differ between models.

For the available models and their capabilities, see Models and licenses.

Next steps

Table of Contents