Clatter LabsHelp Center

How do I limit the length of a reply in Python?

How-toUpdated

Set max_tokens (or max_completion_tokens). On Max and Heavy it includes reasoning tokens, so leave room for thinking.

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.gateway.clatterlabs.xyz/v1",
    api_key=os.environ["CLATTER_API_KEY"],
    default_headers={"X-Clatter-Account": os.environ["CLATTER_ACCOUNT_ID"]},
)

r = client.chat.completions.create(
    model="supercosmos-flash",
    messages=[{"role": "user", "content": "Describe the ocean."}],
    max_tokens=120,
)
print(r.choices[0].message.content)
if r.choices[0].finish_reason == "length":
    print("(cut off by max_tokens)")

Each model has its own cap; GET /v1/models lists max_output_tokens per id (32,768 for Flash and Max, 49,152 for Heavy).

The client setup above (base URL, key and the X-Clatter-Account header) is the same in every Python example; see Set up the Python SDK. Full parameter list: Create a chat completion.

Good to know#

  • If calls fail unexpectedly, check status.clatterlabs.com before debugging further.
  • Every response has an X-Request-Id; log it so a request can be found later. Why log it
  • New accounts get $5 of free credit; top up from $1 to $800 per purchase in Console › Billing › Credits. Billing

Still need help?#

We usually reply within the first hour, and always within 24 hours. A full support team is with you.

Related articles

Still need help?

Tried the steps above and still stuck? Email us. We usually reply within the first hour, and always within 24 hours. A full support team is with you.

Infrastructure questions, or permanent deletion of your data
infrastructure@clatterlabs.xyz
For deleting your whole account or the data of one service.