OpenAI Compatible API Chat: Python, Node and curl
An openai compatible api chat call is one POST with a list of messages and one reply from the model. This guide walks through the request, multi-turn conversations, streaming and the errors you can hit, using an uncensored endpoint as the example.
Updated

The request in one minute
Any client that can talk to the OpenAI chat format can talk to this service. You change two things, the base URL and the key, and keep everything else. Here the base URL is https://api.mistralapikey.com/v1 and the model ID is uncensored. Auth is a standard Authorization: Bearer header.
A first request looks like this:
curl https://api.mistralapikey.com/v1/chat/completions -H "Authorization: Bearer YOUR_API_KEY" -H "Content-Type: application/json" -d '{
"model": "uncensored",
"messages": [
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Explain what an API key is in two sentences."}
]
}'
This is an independent service that runs one open-weight model, tuned to answer without content refusals for lawful adult use. It is not the official API of Meta, Mistral, OpenAI or any other vendor. It exposes two methods, POST /v1/chat/completions and GET /v1/models, and it handles text only. If you still need a key, the API key page explains sign-up and the trial credit.
Reading the response
A successful reply is a JSON object in the usual shape. The text is at choices[0].message.content, the stop reason is in choices[0].finish_reason, and token counts are in usage.
{
"id": "chatcmpl-...",
"object": "chat.completion",
"model": "uncensored",
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": "An API key is a secret string..."},
"finish_reason": "stop"
}
],
"usage": {"prompt_tokens": 31, "completion_tokens": 48, "total_tokens": 79}
}
A finish_reason of stop means the model finished its answer. A value of length means it hit a token limit, so raise max_tokens or leave it unset. The headers matter too: X-Request-Id identifies the call for support, X-Balance-USD shows your remaining credit, and X-RateLimit-Limit-Requests and X-RateLimit-Limit-Concurrency report your limits.
Messages and roles
The messages array is the whole conversation, in order. Each item has a role and content.
- system sets behavior, tone and rules. Put it first.
- user is what the person typed.
- assistant is what the model said earlier in the conversation.
- tool carries the result of a function call, if you use tool calling.
The model only knows what is in the array. Nothing is remembered between requests, so the server does not keep a session for you. That is the single most important thing to understand about chat endpoints, and it explains why a "chat" costs more with every turn: the history you send is billed as input each time.
Multi-turn chat in Python
To hold a conversation, keep a list, append the user message, call the API, append the assistant reply, and repeat. This is a complete terminal chat.
from openai import OpenAI
client = OpenAI(
base_url="https://api.mistralapikey.com/v1",
api_key="YOUR_API_KEY",
)
history = [{"role": "system", "content": "You are a friendly, direct assistant."}]
while True:
text = input("you> ").strip()
if not text:
break
history.append({"role": "user", "content": text})
resp = client.chat.completions.create(
model="uncensored",
messages=history,
temperature=0.7,
)
answer = resp.choices[0].message.content
history.append({"role": "assistant", "content": answer})
print("bot>", answer)
print(f" [{resp.usage.prompt_tokens} in / {resp.usage.completion_tokens} out]")
Watch the bracketed numbers as you chat. The "in" count climbs every turn because it includes everything said so far. That growth is normal and is also your main lever for cost control, covered below.
Streaming replies in Node
For a chat interface you want text to appear as it is generated. Set stream: true and the API sends server-sent events. The OpenAI Node SDK wraps this in an async iterator.
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.mistralapikey.com/v1",
apiKey: process.env.API_KEY,
});
const stream = await client.chat.completions.create({
model: "uncensored",
messages: [{ role: "user", content: "Write a four-line poem about debugging." }],
stream: true,
});
for await (const chunk of stream) {
const piece = chunk.choices[0]?.delta?.content ?? "";
process.stdout.write(piece);
}
process.stdout.write("\n");
Usage is reported at the end of the stream, so you can still log token counts without a second call. One operational detail: a streamed request holds its concurrency slot until the stream finishes. If a user closes a tab, cancel the request on your side so the slot frees up sooner.
Parameters you can use
The standard sampling controls are passed through to the model. These are the ones you will touch most.
| Field | What it does | Practical tip |
|---|---|---|
temperature | Randomness of word choice | 0 to 0.3 for extraction, 0.7 to 1.0 for creative text |
top_p | Limits sampling to the most likely words | Change this or temperature, rarely both |
max_tokens | Upper bound on reply length | Optional; set it to cap cost on open-ended prompts |
stop | Sequences that end generation | Useful for line-based formats |
seed | Repeatable sampling | Handy for tests |
frequency_penalty, presence_penalty | Discourage repetition | Small values go a long way |
response_format | JSON mode | Use {"type":"json_object"} and describe the keys in the prompt |
Tool calling is available through tools and tool_choice as well. The quickstart covers the setup for common SDKs.
What a conversation costs
Prices are $0.15 per 1M input tokens and $0.50 per 1M output tokens, from prepaid credit. The context window is 100,000 tokens for prompt and reply together. Because history is resent, the cost of a chat grows faster than the number of messages.
Take a chat where each user message is about 100 tokens and each reply about 200 tokens, plus a 200-token system message. Turn 1 sends 300 input tokens. Turn 10 sends roughly 200 + 10 x 100 + 9 x 200 = 3,000 input tokens. Turn 30 sends about 200 + 30 x 100 + 29 x 200 = 9,000.
Add up all 30 turns of input: the total is 30 x 200 + 100 x (1 + 2 + ... + 30) + 200 x (0 + 1 + ... + 29) = 6,000 + 46,500 + 87,000 = 139,500 input tokens. Output is 30 x 200 = 6,000 tokens. Cost: 139,500 / 1M x $0.15 = $0.020925, plus 6,000 / 1M x $0.50 = $0.003. The whole 30-turn chat comes to about $0.024. Cheap, but notice that input is 23 times larger than output, and it would grow with the square of the length if you never trimmed.
Ways to keep it down: drop or summarize old turns once the history passes a size you choose, keep the system prompt short, and set max_tokens when you do not need long replies. Failed or refused requests are not charged. Top-up amounts and bonus tiers are listed on the pricing page.
Trimming history without breaking the chat
Once a conversation gets long you have two choices: pay for the full history every turn, or cut it. A simple rule is to keep the system message and the last several turns. This function does that and leaves the rest of your loop unchanged.
from openai import OpenAI
client = OpenAI(
base_url="https://api.mistralapikey.com/v1",
api_key="YOUR_API_KEY",
)
def trimmed(history, keep=8):
system = history[:1]
rest = history[1:]
return system + rest[-keep:]
resp = client.chat.completions.create(
model="uncensored",
messages=trimmed(history),
)
The price of trimming is that the model forgets what it no longer sees. For support bots that is fine. For long stories you may prefer to ask the model to write a short summary of the old turns once, store it as a system note, and drop the originals. That costs one extra call but keeps the plot.
Writing a useful system message
Keep it short and concrete: who the assistant is, how long answers should be, and any format rules. "You are a support agent for a bike shop. Answer in at most three sentences and ask a question if the request is unclear" does more than a page of general virtue. Every word of it is billed as input on every turn, so a 1,000-token system message adds $0.00015 to each request.
Errors and limits
A full key allows 8 concurrent requests and 300 requests per minute; a trial key allows 2 and 60. The maximum request body is 8 MB. Go over a limit and you receive HTTP 429, with one of these meanings:
- Error code
rate_limited: too many requests per minute. Space them out. - Error code
concurrency: too many requests at the same time. Queue them. - A
Retry-After: 5header: temporary overload. Wait 5 seconds and retry.
A 401 means the key is missing or wrong. Keep keys out of front-end code; call the API from your server. If you are moving over from a Mistral setup, the Mistral API key page covers access with this service.
For production code, wrap calls in a retry with a short wait, as the OpenAI SDKs already do for some failures, and log the X-Request-Id value next to every error. When something looks wrong, that ID is the fastest way to point support at the exact request. Also set a client-side timeout: a long completion on a big prompt can take a while, and you do not want a stuck connection to hold one of your 8 concurrency slots.
The service does not produce sexual content involving minors, and prompts are not used for training.
More guides
Questions and answers
What does OpenAI compatible mean?
It means the endpoint accepts the same request format as the OpenAI chat completions API, so OpenAI SDKs and tools work after you change the base URL and key.
Does the server remember the conversation?
No. Send the full message history with each request. The server keeps no session between calls.
Which endpoints are available?
POST /v1/chat/completions and GET /v1/models. The service is text only, with no image, audio or embeddings endpoints.
How do I stream the reply?
Set stream to true. The API sends server-sent events and reports usage at the end of the stream.
What model name do I use?
Use uncensored as the model ID in every request.
Your key is one form away
Create an account, copy the key, change the base URL. That is the whole setup.