News / Learn / Working with model APIs
How do you make your first LLM API call?
You'll be able to send a prompt to a language model over HTTP and read the reply.
In plain English
A model API is a web address you send a request to. You send your text, the model returns text, and you pay for the tokens that go in and come out. Your first call is just one request and one response.
Why it matters
Almost every AI product starts with a single model call. Knowing the request shape, the key handling, and what the response contains lets you build on any provider. The same ideas carry over when you switch models or vendors.
How it works
A call has four parts: an endpoint URL, an API key sent in a header, a model name, and a list of messages. Each message has a role, such as system, user, or assistant, and some content. The system message sets the behavior, and the user message holds the request.
The server reads your messages, splits them into tokens, and runs the model to produce the next tokens. It returns a response object that holds the generated text, a finish reason that says why generation stopped, and a usage count of input and output tokens. Usage counts drive cost, so log them from the start.
Keep the API key on the server or in an environment variable. Never place it in front-end code or commit it to a repository. Set a timeout and handle errors, since network calls fail. Most providers offer an official SDK, but a plain HTTP request works the same way and helps you learn what the SDK does for you.
A concrete example
A minimal call in Python using the requests library. Replace the URL, model name, and response parsing with the values from your provider's documentation.
import os
import requests
API_URL = "https://api.example-provider.com/v1/chat"
API_KEY = os.environ["LLM_API_KEY"]
payload = {
"model": "your-model-name",
"messages": [
{"role": "system", "content": "You answer in one short sentence."},
{"role": "user", "content": "What is a token?"},
],
}
response = requests.post(
API_URL,
headers={"Authorization": f"Bearer {API_KEY}"},
json=payload,
timeout=30,
)
response.raise_for_status()
data = response.json()
print(data["choices"][0]["message"]["content"])
print("Tokens used:", data["usage"])A short sketch to show the idea. Check the official docs of the tool you use.
What goes wrong
- Hardcoding the API key Read the key from an environment variable or a secrets manager. Rotate it if it ever appears in code or logs.
- Skipping error handling Check the HTTP status and handle failures. Timeouts, server errors, and rejected requests all happen in production.
- Ignoring the usage field Store input and output token counts for each call. You need them to understand cost and to debug long prompts.
- Assuming one response shape everywhere Field names differ between providers. Read the docs for your provider and wrap the parsing in one function.
Going deeper
Learn how streaming changes the response, how tool calling adds structured actions, and how rate limits and retries keep your app stable under load.
FAQ
Do I need an SDK to call a model?
No. An SDK wraps the HTTP request and parsing for you. A plain HTTP call shows you exactly what is sent and received, which helps when you debug.
What is the difference between system and user messages?
The system message sets the rules and tone for the model. The user message carries the actual request. Both are sent on every call.
Why does my response stop mid-sentence?
The model hit a limit on output tokens. Check the finish reason in the response. Raise the output limit or shorten the request.
Can I call a model from the browser?
You should not. Browser code exposes your key to anyone who opens the developer tools. Call the model from your server and send the result to the browser.
Written with AI by CapyLaunch. Spotted a mistake? Email hello@capylaunch.com.