AI Development · Level 0 · Start here

APIs, Tokens and Context Windows Explained

How apps talk to language models: what APIs and API keys are, how text becomes tokens, what fits into a context window and why you pay per token.

By 11 min read
How apps talk to language models: what APIs and API keys are, how text becomes tokens, what fits into a context window and why you pay per token.

Most AI apps do not run a language model themselves. They send text to a model at a provider and get text back. Three ideas shape this exchange: the API that carries each request, the tokens in which the model counts every piece of text, and the context window that limits how much text fits into one request. Together, they decide what your app can do, how quickly it answers and what it costs.

This is part 3 of Level 0 in our AI Development learning path. You need no programming knowledge. You will learn what an API and an API key are, how text turns into tokens, what fits into a context window, why providers limit your requests and why you pay per token. Part 2 showed the parts of an AI app. This part looks closely at the line between your app and the model.

What an API is

Part 1 described an API (application programming interface) as a fixed way for two programs to exchange data. For a language model, the API is a web address at the provider, called an endpoint. Your app sends a request to it: which model should answer, your instructions and the user’s text. The provider runs the model and sends back a response: the answer, plus facts about the request, such as the number of tokens it used.

You may know the same models from chat apps. The API is a second way in, built for programs instead of people:

Two columns compare a chat app and an API: in the chat app, a person types and reads, the app sets the instructions and keeps the history, and a free plan or subscription covers the use; with the API, your program sends requests, you decide instructions, data and history, use is usually billed per token, and access needs an API key
Chat app or API: the same model, two ways to use it.

One difference matters from the start. As part 2 showed, with a typical chat API the model does not remember your earlier requests. A chat app keeps the history for you. With the API, your app has to send everything the model needs in every request: the instructions, the relevant data and the earlier messages.

Not every model can be downloaded. Of the 343 models in our LLM database, 160 are closed: you can use them only through an API or an app. 172 have open weights. You can run them yourself if the license allows your use, and many hosting services offer them through an API as well. A few current models from both groups:

Models from our LLM database
ModelReleasedWeightsECIRank
Claude Opus 5.5 AnthropicSep 22, 2026closed166.81
GPT-6.1 Sol OpenAISep 29, 2026closed166.33
Gemini 3.8 Flash Google DeepMindSep 2, 2026closed157.219
Grok 4.6 xAIAug 12, 2026closed156.327
Kimi K3 MoonshotJul 16, 2026open157.417
DeepSeek V4.1 Flash DeepSeekSep 9, 2026open155.631

ECI = Epoch Capabilities Index, one overall score across many benchmarks; rank among all models with an ECI. Data: Epoch AI, retrieved on October 17, 2026, licensed CC BY 4.0. All models: LLM database.

If you build without code, platforms such as MindStudio send these API requests for you. The same rules about tokens and limits still apply in the background.

API keys: keep the secret on the server

An API key is a long, secret string of characters that identifies your account. You create it in the provider’s console, the web dashboard of your account. The provider checks the key with every request. Each request made with your key counts toward your limits and lands on your bill, no matter who sent it. Treat a key like a password.

The key belongs in the backend, the part of your app that runs on a server. The browser or the mobile app talks to your backend, and only the backend talks to the model provider. On the server, the key lives in an environment variable: a setting outside your code that your program reads when it runs. Hosting platforms such as Netlify offer environment variables for server-side functions, so the key never appears in your code.

APIs, Tokens and Context Windows Explained
DoDon’t
Keep the key in an environment variable or a secret manager (a vault for keys and passwords) on the serverWrite the key into your code or into a file stored with your code
Call the model from your backendCall the model with your key from a web page or a mobile app, where anyone can read it
Create one key per app and per environment, such as test and liveShare one key across all projects and colleagues
Set a budget limit and usage alerts in the provider’s console, if it offers themNotice unusual use only when the invoice arrives

Tokens: how models count text

Language models do not read letters or whole words. They read tokens: pieces of text from a fixed vocabulary that holds tens of thousands of pieces or more. A small program called a tokenizer splits your text into these pieces and replaces each piece with its number in the vocabulary. The model calculates only with these numbers. Its answer also comes out as token numbers, which are turned back into text.

A common short word is usually one token, often together with the space in front of it. Rare or long words, numbers and code are split into several pieces:

Three example texts split into tokens shown as chips: an English customer question with 8 tokens, the same question in German with 9 tokens, and a short piece of JSON code with 7 tokens; below, the rule of thumb that 100 English tokens are about 75 words
Example of how a tokenizer might split text. Each model family splits a little differently.

For estimates, a rule of thumb helps: in English, 100 tokens are about 75 words, and one token is about four characters (Source: OpenAI Help Center, help.openai.com). Other kinds of text usually need more tokens for the same content:

APIs, Tokens and Context Windows Explained (2)
TextEffect on the token count
Everyday EnglishThe baseline of the rule of thumb
German and many other languagesOften more tokens for the same message
Order numbers, amounts, datesSplit into several short pieces
Code and JSONBrackets, quotes and spaces count, too
Images, audio, PDF filesConverted into tokens as well, often many per file

Tokenizers differ between model families, so the same text can have a different token count with each one. For exact numbers, use the token counter your provider offers, or read the token usage in the API response.

Input tokens and output tokens

Every request has two sides, and the provider counts both:

  • Input tokens are everything you send: instructions, earlier messages, documents such as a help article, and the new question.
  • Output tokens are everything the model writes. The model produces them one at a time, so a long answer takes longer than a short one. A setting called max output tokens caps the length. If the model reaches the cap, the answer is cut off.

Many newer models also “think” before they answer. These reasoning tokens are often hidden from you, but with many providers they count as output tokens and appear on your bill.

Because a typical chat API does not remember earlier requests, the input grows during a conversation. Each new request carries the whole history again:

Stacked bars for six requests in a support chat: instructions and help article stay the same, the earlier messages grow with each request and the new question stays short; the output of 150 tokens per answer stays the same
Example values: the history makes every request longer, even when the questions stay short.

In this example, the sixth request carries almost twice as many input tokens as the first, although the customer’s questions stay short. Long chats get slower and more expensive with every turn. At some point, they no longer fit into the context window.

The context window: the limit of one request

The context window is the maximum number of tokens a model can handle in one request: input and output together. Think of it as a box of fixed size. Instructions, documents, chat history and the question go in first. The answer has to fit into the space that is left.

Three boxes of the same size stand for the context window. Fits: instructions, help article, chat so far and question as input, the answer as output and some free space. Too long: three help articles and a long chat fill the box, and the answer has no room. Trimmed: a summary and one relevant passage replace them, and everything fits again
The context window holds input and output. If they do not fit, your app has to shorten the input.

Each model has its own context window. The provider lists it on the model page, often together with a lower limit for the output alone. If the input and the space reserved for the answer do not fit, the API usually rejects the request with an error. Many chat apps handle this for you, for example by dropping or shortening old messages. In your own app, you decide what happens. Three common strategies:

  1. Keep only recent messages. Send the last few turns and drop the oldest ones.
  2. Summarize. Replace the older part of the chat with a short summary.
  3. Retrieve instead of copying. Keep your documents in a search index, for example a vector database such as Pinecone, and send only the passages that match the question. Level 2 explains this method, retrieval-augmented generation, step by step.

A bigger window is not automatically better. Every token you send costs money and time, and models do not use all parts of a long input equally well. One study found that models can miss information placed in the middle of a long input (Source: Liu et al., “Lost in the Middle: How Language Models Use Long Contexts”, 2023, arxiv.org). Send what the task needs, not everything you have.

Rate limits: how much you can send per minute

Providers protect their servers with rate limits: caps on how much one account may send in a given time. The most common are requests per minute (RPM) and tokens per minute (TPM). Some providers add daily limits or count input and output tokens separately. Whichever limit you reach first stops you.

APIs, Tokens and Context Windows Explained (3)
LimitWhat it countsWhen you hit it
Requests per minute (RPM)Calls, no matter how long they areMany short requests, such as sorting thousands of short emails
Tokens per minute (TPM)Tokens of all calls togetherA few long requests, such as summaries of long PDF files
Daily limitsRequests or tokens per dayBig batch jobs that run for hours

If you go over a limit, the API answers with the HTTP status code 429 (“Too Many Requests”). Your app should then wait and try again, with a longer pause each time. Level 1 shows this pattern in your first LLM API call.

Limits differ from account to account. Many providers place accounts in tiers that rise with usage and payment history, and they show the current limits in the console. Many also send your remaining requests and tokens with every response, so your app can slow down before it hits a limit.

Why APIs bill per token: a worked example

Tokens measure the work a model does: it has to process every input token and generate every output token. That is why providers bill API use per token. Prices are usually stated per million tokens, with one price for input and one for output, and output tokens usually cost more. A chat subscription usually does not include API use. Some providers charge less for input that repeats in many requests (prompt caching) or for jobs that can wait (batch processing). If you run an open-weight model yourself, for example on a GPU cloud such as Runpod, you pay for GPU time instead of tokens. Level 3 explains when renting GPUs makes sense.

Before you estimate the costs of a project, look up these facts for your model:

APIs, Tokens and Context Windows Explained (4)
What to look upWhere you find itWhat it tells you
Context windowThe provider’s model pageHow much input and output fit into one request
Max output tokensModel pageThe longest possible answer
Price per million input and output tokensPricing pageWhat one request costs
Rate limits of your tierConsole or limits pageHow many requests and tokens per minute you can send
Token counterDocumentation or consoleHow your own texts translate into tokens

Now the estimate. Here is the support bot from part 2, with example values for one request:

APIs, Tokens and Context Windows Explained (5)
Part of the requestTokens (example)
Instructions400
Help article (about 450 English words)600
Chat so far (average)300
Customer question50
Input per request1,350
Output: the answer150

With 2,000 questions a day, the bot reads 2.7 million input tokens and writes 0.3 million output tokens per day. In a month of 30 days, that adds up to about 81 million input and 9 million output tokens. Multiply these numbers by your provider’s current prices per million tokens, and you have a first estimate.

The example also shows where the tokens go. The bot reads nine times as many tokens as it writes, and most of them are instructions and help text, sent again with every request. That is the first place to look when you want to save. Level 4 shows how to optimize cost and response time at scale.