EN ▾
Get API key

What Is a Chat API? Messages, Roles and Tokens

What is chat api in plain terms? It is a web endpoint where you send a list of messages and get the next message back. The list carries roles, the model reads it as tokens, and you are billed for the tokens that go in and come out.

Updated

Glass message tiles entering a slot and a reply tile emerging, illustrating how a chat API works
Isometric scale weighing stacks of token blocks against coins, illustrating per-token API pricing
per 1M input tokens
$0.15
Output tokens / 1M
$0.50
token context
100,000
trial credit
$0.50
requests per minute
300

Key points

  • A chat API takes a list of role-tagged messages and returns one new assistant message.
  • Roles are system, user, assistant and tool; the model remembers nothing between calls.
  • Tokens are pieces of text; billing counts input and output tokens separately.
  • A first request is one curl command or five lines of Python.

No card needed. After sign-up, claim $0.50 of free test credit with one click (once per person).

Get API key

The idea in one picture

Think of a chat API as a function. You pass in a conversation so far, and it returns what the assistant would say next. That is the whole contract. There is no session stored on the server and no hidden state: each call stands alone, and the conversation exists only in the list you send.

The common format is called chat completions. It started with OpenAI and many other services copy it, including the endpoint on this site. The request is a POST to /v1/chat/completions with a JSON body. The answer is JSON too, or a stream of small JSON events if you ask for streaming.

Because the format is shared, one piece of client code can talk to many providers. You swap the address, the key and the model name, and the rest stays put.

Messages and roles

The body has a messages array. Each item has a role and content. The role tells the model who is speaking.

RoleWho writes itPurpose
systemYou, the developerSets behavior, tone and rules for the whole chat
userThe person using your appThe question or instruction
assistantThe model, earlierPrevious replies, sent back so the model sees them
toolYour codeThe result of a function the model asked you to run

A key consequence: if you want the model to remember something, it has to be in the array. When a chat app seems to remember your name, the app is resending earlier messages every time. Drop them and the memory is gone.

Your first request

Here is the smallest useful request against the endpoint on this site. Put your key in an environment variable first.

curl https://api.uncensoredaichat.cc/v1/chat/completions \
  -H "Authorization: Bearer $CHAT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "uncensored",
    "messages": [
      {"role": "system", "content": "Answer in one sentence."},
      {"role": "user", "content": "What is a token?"}
    ]
  }'

The same call in Python with the OpenAI package:

from openai import OpenAI
import os

client = OpenAI(base_url="https://api.uncensoredaichat.cc/v1",
                api_key=os.environ["CHAT_API_KEY"])
r = client.chat.completions.create(
    model="uncensored",
    messages=[{"role": "user", "content": "What is a token?"}],
)
print(r.choices[0].message.content)
print(r.usage)

Authentication is a bearer token in the Authorization header. The model ID here is uncensored. You can list models with GET /v1/models. If you have no key yet, the API key page gives new users $0.50 of trial credit with one click and no card.

Reading the response

A normal reply looks like this, trimmed to the parts you use:

{
  "id": "chatcmpl-...",
  "model": "uncensored",
  "choices": [
    {
      "index": 0,
      "message": {"role": "assistant", "content": "A token is a small piece of text..."},
      "finish_reason": "stop"
    }
  ],
  "usage": {"prompt_tokens": 24, "completion_tokens": 18, "total_tokens": 42}
}

choices[0].message.content is the text. finish_reason tells you why it stopped: stop for a natural end, length if it ran out of room, and tool_calls if the model wants you to run a function. The usage block is what billing is based on. The HTTP response also carries X-Request-Id for support questions and X-Balance-USD with your remaining credit.

Tokens: the unit everything is measured in

Models read text in pieces called tokens. A token can be a whole short word, part of a long one, or a punctuation mark. For English, a handy rule is about four characters or three quarters of a word per token, so 1,000 tokens is roughly 750 words. That is an estimate, not an exact count, and other languages often use more tokens per word.

Tokens matter for two reasons. The first is the context window, which is the most the model can handle in one call. Here it is 100,000 tokens, shared by the prompt and the completion. The second is cost, covered next. The usage field in each response tells you the real numbers, so log it from day one.

How billing works

Input tokens (everything you send) and output tokens (what the model writes) are priced separately: $0.15 per 1M input tokens and $0.50 per 1M output tokens. Use the numbers from the example response above, 24 in and 18 out:

  • Input: 24 / 1,000,000 x $0.15 = $0.0000036
  • Output: 18 / 1,000,000 x $0.50 = $0.000009
  • Total: $0.0000126, about a thousandth of a cent

Real chats are larger because the whole history is resent each turn. A chat that has 5,000 tokens of history and gets a 500 token answer costs 5,000 x $0.15 / 1M = $0.00075 for input plus 500 x $0.50 / 1M = $0.00025 for output, which is $0.001. That is one tenth of a cent.

You prepay credit and spend it down. There is no subscription, failed or refused requests are not charged, and credit never expires. See the pricing page for top-ups from $20 to $500 and the bonus tiers. If you want a general explanation of how token prices compare, how much is the OpenAI API shows the method on another service.

Mistakes that cost beginners an hour

Most first-day problems come from a short list of habits.

  • Expecting memory. The reply to "what did I just say?" is wrong if you sent only the last message. Send the history.
  • Putting everything in the user message. Rules and persona belong in the system message, where they stay separate from the user's text.
  • Ignoring finish_reason. A value of length means the answer was cut off. Raise the limit or shorten the prompt.
  • Hard-coding the key. Keys pasted into a public repository get found. Use environment variables.
  • Retrying without waiting. On a 429, pause before the next try, and respect Retry-After when it is there.
  • Not logging usage. Without the token numbers you cannot explain a bill. Write usage to your logs, along with X-Request-Id, so that any odd answer or charge can be traced to one specific call later.

Another common surprise is the mismatch between a chat product and a chat API. A consumer app may trim history, add hidden instructions and run safety layers for you. With the API, you control all of that, which is more work and also more freedom. For a step-by-step build of a working bot, the article on how to use an AI API continues from here.

Useful options and limits

Beyond messages, a few fields change behavior:

  • stream: true returns text as it is generated, over server-sent events, with usage at the end.
  • temperature and top_p control randomness; stop, seed and the penalties are passed through.
  • response_format: {"type":"json_object"} asks for valid JSON.
  • tools and tool_choice let the model request function calls.
  • max_tokens is optional and caps the reply.

Limits on a full key are 8 concurrent requests and 300 per minute, with a body size up to 8 MB. Trial keys get 2 and 60. Past a limit you receive HTTP 429 with rate_limited or concurrency; temporary overload adds a Retry-After: 5 header. There is also a newer POST /v1/responses endpoint for clients that use it. The docs cover every field.

This is a text service. It does not make images, audio or video, and it offers no embeddings or fine-tuning. The model is run by an independent provider and is not an official API of any model vendor.

Questions and answers

Is a chat API the same as ChatGPT?

No. ChatGPT is a consumer app. A chat API is an endpoint your own code calls, so you can build your own app around a model.

Does the API remember my earlier messages?

No. Each call is independent. You must send the earlier messages again in the messages array.

What is the difference between prompt tokens and completion tokens?

Prompt tokens are the input you send, including history. Completion tokens are the text the model writes. They are priced separately.

How do I try it cheaply?

Claim the $0.50 trial credit on the API key page, no card needed, valid for 7 days. Each short request costs a tiny fraction of a cent.

Your key is one form away

Create an account, copy the key, change the base URL. That is the whole setup.

Start with trial credit

Email and password, key on screen right away.