EN ▾

LLM API Tutorial: Requests, Tokens and Costs

An LLM API is a web endpoint that takes a list of messages and returns text. This page walks through one request, what tokens are, and how to price a call before you make it.

Updated:

Isometric illustration of a request flowing through a pipeline of token blocks to a language model

What an LLM API is, in one paragraph

This llm api tutorial starts with the smallest possible picture. You send an HTTPS POST with a JSON body. The body names a model and carries a list of messages. The server runs the model and sends back JSON that contains the generated text. That is the whole contract. Everything else, such as streaming, tools and JSON mode, is an option layered on top of that one exchange.

This site exposes an OpenAI-compatible API. That means the request and response shapes match the ones the OpenAI SDKs already speak, so you do not need a special client. You change two things, the base URL and the key, and keep the rest of your code. The base URL here is https://api.uncensoredimagegenerator.com/v1 and the model ID is uncensored. It is a text model, so in this setup it writes prompts, captions and descriptions for image tools. It does not draw the images itself.

Two endpoints are enough for almost everything: POST /v1/chat/completions to generate text and GET /v1/models to list what the key can use.

Your first request

You need an API key first. After sign-up, the API key page shows it and offers a one-click trial credit. With a key in an environment variable, this curl call is a complete request:

curl https://api.uncensoredimagegenerator.com/v1/chat/completions   -H "Authorization: Bearer $API_KEY"   -H "Content-Type: application/json"   -d '{
    "model": "uncensored",
    "messages": [
      {"role": "system", "content": "You write short, concrete image prompts."},
      {"role": "user", "content": "A foggy harbor at dawn, one lantern on a wooden pier."}
    ],
    "max_tokens": 200
  }'

The same call through the official Python SDK looks like this:

from openai import OpenAI

client = OpenAI(
    base_url="https://api.uncensoredimagegenerator.com/v1",
    api_key="YOUR_API_KEY",
)

reply = client.chat.completions.create(
    model="uncensored",
    messages=[
        {"role": "system", "content": "You write short, concrete image prompts."},
        {"role": "user", "content": "A foggy harbor at dawn, one lantern on a wooden pier."},
    ],
    max_tokens=200,
)

print(reply.choices[0].message.content)
print(reply.usage)

The text sits in choices[0].message.content. The usage object next to it reports how many tokens went in and how many came out. That object is what your bill is built from, so print it while you experiment. The max_tokens field is optional here. Leave it out and the reply can use whatever is left of the context window.

Messages and roles

Each message has a role and a content string. Three roles cover most work.

  • system sets standing instructions: tone, format, what to avoid. Put the rules that never change here.
  • user is what the person or your program is asking right now.
  • assistant is a previous reply from the model, which you send back so it can see the earlier turns.

When you use tool calling, results come back to the model as messages with role: "tool". The docs page on the quickstart shows the exact fields.

The detail that surprises most beginners is that the API has no memory. Each request stands alone. If you want a conversation, you keep the list yourself and append every new user message and every reply, then send the whole list again. The model only knows what is in the request you are sending right now. This design is simple and predictable, and it has a direct effect on cost, which the next sections cover.

Tokens are the unit of everything

Models do not read characters or words. They read tokens, which are chunks of text that are often a short word or a piece of a longer one. For ordinary English prose, a common rule of thumb is that one token is a little under four characters, so 1,000 tokens is roughly 750 words. Code, unusual names and non-English text usually take more tokens per word. Treat the rule as an estimate and trust the usage object for the real number.

Every call has two token counts:

  • Input tokens: everything you send, including the system message, the full history and the new question.
  • Output tokens: everything the model writes back.

The context window is the ceiling for both together. On this API it is 100,000 tokens, prompt plus completion. If your prompt is 90,000 tokens, the reply has at most about 10,000 left. For prompt writing this is rarely a limit. For pasting in long documents it can be.

Prices are set per million tokens: $0.15 per 1M input tokens and $0.50 per 1M output tokens. Output costs more because the model produces it one token at a time. You pay from prepaid credit, there is no subscription, and failed or refused requests are not charged. The pricing page lists the top-up options.

Worked cost examples

The formula is short: cost = input tokens x $0.15 / 1,000,000 + output tokens x $0.50 / 1,000,000. Here it is applied to three realistic calls.

CallInput tokensOutput tokensInput costOutput costTotal
Short chat reply300150$0.000045$0.000075$0.00012
Prompt with a style guide attached6,000500$0.0009$0.00025$0.00115
Long document summary80,0002,000$0.012$0.001$0.013

Take the middle row. 6,000 x $0.15 / 1,000,000 is $0.0009, and 500 x $0.50 / 1,000,000 is $0.00025. Together that is $0.00115, so about 870 such calls cost one dollar. Even the 80,000-token document costs 1.3 cents.

Now the effect of having no memory. Suppose a 20-turn chat where each user message is 200 tokens and each reply is 200 tokens. On turn 1 you send 200 tokens. On turn 2 you send the first exchange (400) plus the new message (200), so 600. By turn 20 you send 7,800. Add up all twenty requests and the input total is 80,000 tokens, while the output total is only 4,000. The cost is 80,000 x $0.15 / 1,000,000 = $0.012 for input plus 4,000 x $0.50 / 1,000,000 = $0.002 for output, which makes $0.014 in all. The history dominates the bill. If a long conversation gets expensive, trim old turns or summarize them into one short message.

For scale: the one-click trial gives $0.50 of credit, which pays for roughly 1,300 calls of the 300-in, 150-out kind from the table.

Streaming, JSON mode and tools

Once the basic call works, three options matter most.

Streaming. Set stream: true and the server sends the reply as server-sent events, token by token, with usage reported at the end. This makes chat interfaces feel responsive because text appears as it is written. A streamed request holds one of your concurrent slots until the stream finishes.

JSON mode. Pass response_format: {"type": "json_object"} and tell the model in the prompt which keys you want. This is the easy way to get output your program can parse, for example a list of five prompt variants with a short label each.

Tool calling. You describe functions in the tools field. The model can answer with a tool call instead of text, your code runs the function, and you send the result back as a tool message. It is how you connect the model to a database, a search call or your own scripts.

Sampling settings such as temperature, top_p, stop, seed and the penalties are passed through. Lower temperature gives steadier output, higher gives more variety. For brainstorming image prompts, something around 0.9 works well; for extraction into JSON, use 0 to 0.3.

Limits and what to do when you hit them

A full key allows 8 concurrent requests and 300 requests per minute. A trial key allows 2 concurrent and 60 per minute. The body size cap is 8 MB. Every response carries headers worth logging: X-Request-Id for support questions, X-Balance-USD for your remaining credit, and the two X-RateLimit-Limit-* headers.

When you go over, the API answers HTTP 429. The error code tells you which limit: rate_limited means too many requests per minute, and concurrency means too many at once. A 429 with a Retry-After: 5 header means temporary overload, and the right move is to wait five seconds and try again. A short retry loop handles all of it:

import time
from openai import OpenAI, RateLimitError

client = OpenAI(
    base_url="https://api.uncensoredimagegenerator.com/v1",
    api_key="YOUR_API_KEY",
)

def ask(messages, tries=4):
    for attempt in range(tries):
        try:
            r = client.chat.completions.create(model="uncensored", messages=messages)
            return r.choices[0].message.content
        except RateLimitError:
            time.sleep(5 * (attempt + 1))
    raise RuntimeError("still rate limited after retries")

Keep the number of worker threads at or below your concurrency limit and you will rarely see the second kind of 429.

Where to go next

You now have the pieces: a key, a request, a way to read usage, and a formula for cost. A good next step is to put a few dozen real prompts through the API and compare your measured token counts with your estimates. The documentation covers each parameter in detail, and the web search page shows one way to feed fresh facts into a prompt. When you are ready to go past the trial, top-ups start at $20, with a bonus of 5% from $50, 10% from $100 and 15% from $200, paid in USDT on TRC20 or USDC on Base. The credit never expires, so a small first top-up is a low-risk way to start.

Questions and answers

Do I need the OpenAI library to use this API?

No. Any HTTP client works, since it is a plain JSON POST. The OpenAI SDKs are convenient because they already match the format, and you only change the base URL and key.

How many tokens is a typical prompt?

A paragraph of English is usually 100 to 200 tokens. For an exact figure, read the usage object in the response, which lists input and output tokens for that call.

Why does a long chat cost more per message?

The API keeps no memory, so you resend the whole history each turn. Input tokens grow with every turn even if your new message is short.

Can the model generate images?

No. It is a text-only model. It can write prompts and descriptions that you then paste into an image generator of your choice.

Your key is one form away

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

Get API key
Get API key