When to use it
The OpenAI SDKs handle retries and parsing for you. Plain requests is handy when you want no extra dependencies, want to see exactly what's sent, or are debugging.
Example
import os
import requests
resp = requests.post(
"https://api.luv13.ai/v1/chat/completions",
headers={"Authorization": f"Bearer {os.environ['LUV13_API_KEY']}"},
json={
"model": "luv13/glm-5.3-flash",
"messages": [{"role": "user", "content": "Give me one tip for clean code."}],
},
timeout=60,
)
resp.raise_for_status()
data = resp.json()
print(data["choices"][0]["message"]["content"])
print(data["usage"])Passing a dict to json= encodes the body and sets Content-Type: application/json for you.
Handling errors
- A
401means the key is missing or wrong. - A
429means you're sending too fast. Wait and retry. See Retrying Requests. - A
5xxmeans a server-side problem. Retrying later often works.
For luv13's own error codes, see Errors and Status Codes. To read a streamed reply with requests, see Server-Sent Events.
Related
For a short luv13-only version of this example, see Python Example.
