Clatter LabsHelp Center

How do I build a tiny chatbot with the Model API?

How-toUpdated
# pip install openai
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 helper. Keep answers short."}]

while True:
    text = input("you> ").strip()
    if text in ("quit", "exit"):
        break
    messages.append({"role": "user", "content": text})
    reply = client.chat.completions.create(model="supercosmos-flash", messages=messages)
    answer = reply.choices[0].message.content
    messages.append({"role": "assistant", "content": answer})
    print("bot>", answer)

Run it, type a question, and type quit to stop. Each turn re-sends the history, so very long chats cost more per turn.

Good to know#

  • Keep keys on your server, never in a browser or mobile app. Why
  • Each key has its own limit: 10, 30 (default), 60 or 120 requests per minute. Rate limits
  • Retry only 429 and 5xx errors, with backoff; honour Retry-After when it's sent. Which errors to retry

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.