Clatter LabsHelp Center

How do I keep a conversation going in Python?

How-toUpdated

The Model API doesn't remember earlier requests. Keep a messages list, append the assistant's reply after each call, and send the full list next time.

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"]},
)

messages = [{"role": "system", "content": "You are a friendly tutor."}]

def ask(text):
    messages.append({"role": "user", "content": text})
    resp = client.chat.completions.create(model="supercosmos-flash", messages=messages)
    reply = resp.choices[0].message
    messages.append({"role": "assistant", "content": reply.content})
    return reply.content

print(ask("What is a prime number?"))
print(ask("Is 51 one?"))

History counts as input tokens every turn, so long chats cost more per turn. A request can hold up to 800 messages and 3 MB; trim or summarise old turns well before that.

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#

  • Retry only 429 and 5xx errors, with backoff; honour Retry-After when it's sent. Which errors to retry
  • Every response has an X-Request-Id; log it so a request can be found later. Why log it
  • Each key has its own limit: 10, 30 (default), 60 or 120 requests per minute. Rate limits

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.