What Is an LLM API and How Do You Call One?

A
Author
18 Oct, 2026 4 min read 0 views

An LLM API is a web service that accepts text (and sometimes images or files) and returns text generated by a large language model. You send an HTTPS request with your prompt, the provider runs the model on its own servers, and you receive the model's answer in the response. You never download or host the model yourself.

The basic request and response cycle

Every hosted LLM API follows the same pattern. Your code builds a JSON body that names a model, includes the conversation so far, and sets a few limits. It sends that body to the provider with an API key in a header. The provider returns JSON that contains the generated text plus metadata such as how many tokens were used and why generation stopped.

The calls are stateless. The server does not remember your earlier requests. If you want a conversation, you send the whole conversation again each time. This single fact shapes most of the design choices you will make later, from history management to cost.

What goes into a request

  • Model: a string that names which model should answer. Larger models are smarter but slower and more expensive.
  • Messages: a list of turns, each with a role (such as user or assistant) and content.
  • System prompt: standing instructions that set the model's behaviour for the whole conversation.
  • Limits: most importantly the maximum number of tokens the model may generate.
  • Optional settings: sampling controls, streaming, stop sequences, tools and output format.

What comes back

The response holds the generated content, a reason the model stopped (it finished, hit your token limit, or matched a stop sequence), and a usage object with input and output token counts. Token counts matter because providers bill by tokens, not by request.

A minimal call, without an SDK

Under the hood it is plain HTTP. This Python example uses the requests library to call the Claude Messages API directly. In real projects you will use the official SDK shown in the next posts, but seeing the raw call removes the mystery.

import os
import requests

response = requests.post(
    "https://api.anthropic.com/v1/messages",
    headers={
        "x-api-key": os.environ["ANTHROPIC_API_KEY"],
        "anthropic-version": "2023-06-01",
        "content-type": "application/json",
    },
    json={
        "model": "claude-haiku-5-5",
        "max_tokens": 200,
        "messages": [{"role": "user", "content": "Explain DNS in two sentences."}],
    },
    timeout=60,
)
response.raise_for_status()
data = response.json()
print(data["content"][0]["text"])
print(data["usage"])

Run it with ANTHROPIC_API_KEY set and you will see a short answer followed by a usage dictionary such as {"input_tokens": 14, "output_tokens": 52}. The exact numbers vary. The anthropic-version header pins the API version, and the SDKs set it for you.

Providers, models and endpoints

Several companies sell this kind of access, including Anthropic (Claude), OpenAI, and Google (Gemini), and cloud platforms resell some models too. Each company ships a family of models in different sizes. A small model answers quickly and costs little, and a large model handles harder reasoning at a higher price and a slower pace. You pick a model by name in each request, which means you can use a cheap model for simple jobs and a strong one for difficult ones within the same application.

Most providers also expose extra endpoints beyond plain chat. Examples are token counting, batch processing at a discount, file upload, and a model listing endpoint. This course starts with the core chat endpoint and adds the others when they become useful.

Why use an API instead of running a model

Hosted APIs give you frontier-quality models with no GPUs to manage, instant scaling, and pay-per-use pricing. The trade-offs are network latency, ongoing cost, rate limits and the need to send your data to a third party. If you need full control or offline use, see the topic on running LLMs locally.

Key ideas to carry forward

  • Calls are stateless: you resend the context every time.
  • You pay for tokens in and tokens out, so prompt length and output length both cost money.
  • Output is probabilistic. The same prompt can produce different text, so validate anything your program depends on.
  • Providers differ in field names, but the concepts (model, messages, limits, usage) are shared.

Next steps

The following posts install the official SDK, walk through each field of a request, and then build up to streaming, structured output, caching and production patterns. Start by making your first real call.

Was this article helpful?

Comments

No comments yet. Have a question or a tip? Be the first to share it.

Leave a comment

Comments are reviewed before they appear.