Before you run anything
You need a key and nothing else. Create one on the API key page; new accounts can claim $0.50 of trial credit with one click, with no card. Export it so the examples can read it:
export API_KEY="YOUR_KEY"
pip install openai
npm install openai
The API follows the OpenAI chat completions format. It exposes POST /v1/chat/completions and GET /v1/models, accepts Authorization: Bearer, and serves a single model with the ID uncensored. That model is an open-weight model run by an independent provider and tuned to answer without content refusals for lawful adult use. It is not the official API of any model vendor. It handles text only: no images, audio, embeddings or fine-tuning.
The examples below go from the simplest call to tool calling. If you only need the shortest possible check that your key works, the first curl command is enough.
A first request with curl, and the parameters that matter
curl https://api.glmapikey.com/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "uncensored",
"messages": [
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "What is a mutex, in one sentence?"}
],
"temperature": 0.3,
"max_tokens": 80
}'
The response is a JSON object with choices[0].message.content holding the answer and a usage object with prompt_tokens and completion_tokens. The response headers include X-Request-Id, X-Balance-USD and the two X-RateLimit-Limit headers. Add -i to curl to see them.
The fields temperature, top_p, stop, seed and the presence and frequency penalties are passed through to the model. max_tokens is optional. Without it, the completion can use whatever is left of the 100,000-token window after your prompt.
The chat completions body has many optional fields, but five do most of the work. All of them are passed through to the model.
| Field | What it does | Typical use |
|---|---|---|
temperature | Higher values give more varied wording | 0.2 for extraction, 0.8 for fiction |
top_p | Limits sampling to the most likely tokens | Leave at default unless you tune it |
stop | Ends the reply when a string appears | Cut off at a delimiter such as a blank line |
seed | Makes sampling repeatable on a best-effort basis | Comparing two prompt versions |
presence_penalty, frequency_penalty | Discourage repeating tokens | Small values such as 0.3 when text loops |
Change one value at a time and keep the prompt the same, otherwise you cannot tell which change helped. For anything that feeds code, such as classification or extraction, use a low temperature and a fixed seed so you can rerun a test and see the same result. For creative writing, raise the temperature and let the penalties handle repetition.
The max_tokens field is optional here. When you leave it out, the reply may use all the room left in the 100,000-token window, which is rarely what you want for a one-line answer. Set it whenever you know how long the answer should be.
Python: a conversation with memory
The API is stateless, so you keep the conversation yourself and send it every time. This loop keeps a list of messages and appends both sides:
from openai import OpenAI
client = OpenAI(
base_url="https://api.glmapikey.com/v1",
api_key="YOUR_KEY",
)
messages = [{"role": "system", "content": "You are a helpful assistant."}]
while True:
user = input("you> ")
if not user:
break
messages.append({"role": "user", "content": user})
resp = client.chat.completions.create(model="uncensored", messages=messages)
reply = resp.choices[0].message.content
messages.append({"role": "assistant", "content": reply})
print("bot>", reply)
Because each call resends the full list, input tokens grow with the length of the chat. At $0.15 per 1M input tokens that is cheap, but it adds up in long sessions, so cut old messages once the thread passes the size you want to pay for. The pricing page has worked examples.
Streaming in Python and Node
Set stream: true and the server sends tokens as they are produced, over server-sent events. In Python:
stream = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Write four lines about the sea."}],
stream=True,
)
text = ""
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
piece = chunk.choices[0].delta.content
text += piece
print(piece, end="", flush=True)
if getattr(chunk, "usage", None):
print("\ntokens:", chunk.usage.prompt_tokens, chunk.usage.completion_tokens)
The stream ends with usage information, which is why the loop checks for it. The same thing in Node:
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.glmapikey.com/v1",
apiKey: process.env.API_KEY,
});
const stream = await client.chat.completions.create({
model: "uncensored",
messages: [{ role: "user", content: "Write four lines about the sea." }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
One detail about limits: a streamed request keeps its concurrency slot until the stream ends. If you start a stream and never read it to the end, that slot stays busy. Always consume or close the stream.
JSON mode for structured output
When your code needs to parse the answer, ask for a JSON object with response_format. Also say in the prompt what shape you want, because the format flag guarantees valid JSON syntax, not your particular schema.
import json
resp = client.chat.completions.create(
model="uncensored",
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": "Reply only with JSON: {\"title\": string, \"tags\": [string]}."},
{"role": "user", "content": "Suggest a title and three tags for a post about home espresso."},
],
temperature=0.2,
)
data = json.loads(resp.choices[0].message.content)
print(data["title"], data["tags"])
Check that the keys you need exist before you use them, and keep the temperature low for extraction work. If a parse fails, retrying once with the same input usually works, and failed requests are not charged.
Tool calling: a full round trip
Tool calling lets the model ask your code to run a function. You describe the function in tools, the model replies with a tool_calls entry, you run it and send the result back as a role: "tool" message.
import json
def get_weather(city: str) -> dict:
return {"city": city, "temp_c": 18, "sky": "cloudy"}
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}]
messages = [{"role": "user", "content": "Is it warm in Lisbon right now?"}]
first = client.chat.completions.create(
model="uncensored", messages=messages, tools=tools, tool_choice="auto"
)
msg = first.choices[0].message
if msg.tool_calls:
messages.append(msg)
for call in msg.tool_calls:
args = json.loads(call.function.arguments)
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps(get_weather(**args)),
})
final = client.chat.completions.create(
model="uncensored", messages=messages, tools=tools
)
print(final.choices[0].message.content)
else:
print(msg.content)
The function here returns fixed data, so swap in a real lookup. Tool arguments arrive as a JSON string, so always parse them and validate the values before acting on them. With stream: true, the tool_calls arrive in pieces that you assemble before parsing. Use tool_choice to force a specific tool when you need that.
What the examples cost, and mistakes that waste an hour
Running examples costs very little, but it helps to see the arithmetic once. At $0.15 per 1M input tokens and $0.50 per 1M output tokens:
- The conversation loop, with a 1,200-token prompt and a 300-token reply: $0.00018 for input plus $0.00015 for output is $0.00033. A thousand turns cost $0.33.
- The tool round trip makes two calls. If the first uses 400 tokens in and 60 out, and the second 500 in and 150 out, the totals are 900 in and 210 out: $0.000135 plus $0.000105 is $0.00024.
- Streaming costs the same as a normal call with the same token counts. It changes when you see the text, not what it costs.
The trial credit of $0.50 would pay for more than 1,500 turns of the conversation loop at that size ($0.50 / $0.00033), so you can experiment freely before topping up. Failed and refused requests are not charged, which means a bug in your parsing that makes you re-run a call does not cost you for the failed attempts.
- A wrong base URL. It must end in
/v1. Adding/chat/completionsyourself to an SDK base URL gives a 404 because the SDK adds the path. - A different model name. Names copied from other platforms do not exist here. The ID is
uncensored. - Expecting memory. The API does not remember earlier calls. Send the history every time.
- Parsing the model's JSON without a guard. Wrap
json.loadsin a try block and have a plan for a bad response. - Leaving streams open. An unfinished stream keeps a concurrency slot busy, and a trial key has only two.
- Putting the key in front-end code. Keep it on a server.
If something still looks wrong, send a request with curl and compare it to what your code produces. The curl form removes the SDK from the picture and makes differences in the body obvious.
Reading usage, balance and errors
To see headers from the Python SDK, use the raw response wrapper:
raw = client.chat.completions.with_raw_response.create(
model="uncensored",
messages=[{"role": "user", "content": "Say ok."}],
)
print("request id:", raw.headers.get("x-request-id"))
print("balance usd:", raw.headers.get("x-balance-usd"))
print("rpm limit:", raw.headers.get("x-ratelimit-limit-requests"))
completion = raw.parse()
print(completion.usage.total_tokens)
Log the request ID with every call so you can point to a single request later. Use the balance header to raise an alert before credit runs out.
When you pass a limit, the API returns HTTP 429, and the error code tells you which one:
| Code or header | Meaning | What to do |
|---|---|---|
rate_limited | Too many requests per minute (300 on a full key, 60 on trial) | Back off and spread out calls |
concurrency | Too many at once (8 on a full key, 2 on trial) | Wait for a running request to finish |
Retry-After: 5 | Temporary overload | Retry after five seconds |
Failed or refused requests are not charged. The request body can be up to 8 MB. For the complete reference and how to reuse existing client code, see the docs. For a walk-through of how to get a key, see the GLM API key article.
