Python examples
Using the Model API with the OpenAI Python SDK.
- Can I use OpenAI and the Model API in the same Python program?Yes. Create two clients with different base URLs and keys, and pick the client per request.How-to
- How do I calculate the cost of a call in Python?Read usage.prompt_tokens and completion_tokens and multiply by the per-1M prices of the model you called.How-to
- How do I call the Model API asynchronously in Python?Use AsyncOpenAI with the same base URL and headers, then await calls or run them with asyncio.gather.How-to
- How do I call the Model API from a Flask app?Create the OpenAI client once at start-up and call it inside a route; keep the key in environment variables.How-to
- How do I call the Model API with Python's requests library?POST JSON to /v1/chat/completions with the Authorization and X-Clatter-Account headers; check the status and error code.How-to
- How do I count tokens before sending a request in Python?No tokenizer is published for SuperCosmos. Estimate from a test request's usage, then use usage for exact counts.How-to
- How do I force or block tool use in Python?Set tool_choice to required, none, auto or a specific function.How-to
- How do I get JSON back from SuperCosmos in Python?Ask for JSON in the prompt, parse it, and retry on a parse error; or use a tool with a JSON schema.How-to
- How do I get token usage when streaming in Python?Add stream_options={"include_usage": True}; the final chunk carries usage with empty choices.How-to
- How do I handle Model API errors in Python?Catch openai exceptions, read the status and error code, and retry only 429 and 5xx.How-to
- How do I handle running out of credits in Python?Catch the 402 out_of_credits error, stop sending requests, and alert someone; retrying won't help.How-to
- How do I keep a conversation going in Python?The API is stateless: append each reply to messages and send the whole history on every turn.How-to
- How do I limit the length of a reply in Python?Set max_tokens (reasoning included). finish_reason is length when the reply was cut off.How-to
- How do I list the available models in Python?client.models.list() calls GET /v1/models, which returns each id with its price.How-to
- How do I make Python results more repeatable?Pass a seed for best-effort repeatable sampling, and keep the prompt and parameters identical.How-to
- How do I pick Flash, Max or Heavy from Python?Call supercosmos-flash, -max or -heavy, or use the family id supercosmos with effort or reasoning_effort.How-to
- How do I process a CSV file with the Model API in Python?Read rows, send one request per row with a small delay or concurrency cap, and write results back with usage.How-to
- How do I read Max or Heavy's reasoning in Python?Max and Heavy return reasoning_content on the message (or delta when streaming). It's billed as output.How-to
- How do I read rate limit headers in Python?Use with_raw_response to read X-RateLimit-Limit, X-RateLimit-Remaining and X-Request-Id.How-to
- How do I send a system prompt from Python?Add a message with role system (or developer) before the user message.How-to
- How do I set retries and timeouts in the Python SDK?Use max_retries and timeout on the OpenAI client; the SDK backs off on 429 and 5xx.How-to
- How do I set temperature or top_p in Python?temperature accepts 0 to 2 and top_p accepts (0, 1]. Lower values give steadier answers.How-to
- How do I set up the OpenAI Python SDK for the Model API?Install openai, point base_url at the Clatter Labs gateway and add the X-Clatter-Account header.How-to
- How do I stream a reply in Python?Pass stream=True and print delta.content from each chunk as it arrives.How-to
- How do I stream Model API replies through a FastAPI backend?Call the Model API from FastAPI and return a StreamingResponse that yields text chunks.How-to
- How do I update Python code that used a Line Code model?Replace line-code-* ids with supercosmos-* ids; Line Code ids answer 410 model_retired.How-to
- How do I use stop sequences in Python?Pass stop as a string or a list of up to 4 strings; the reply ends before the first match.How-to
- How do I use the Model API in a Jupyter notebook?Install openai in the notebook, load the key from an environment variable or getpass, and call it as usual.How-to
- How do I use tool calling in Python?Define function tools, run the tool the model asks for, and send the result back as a tool message.How-to
- How should I store my key and Account ID for Python?Keep CLATTER_API_KEY and CLATTER_ACCOUNT_ID in environment variables or a secret manager, never in code.How-to
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.xyzFor deleting your whole account or the data of one service.