How to Use an LLM API: A Step-by-Step Guide
Using an LLM API means sending text (or files, or tool definitions) to a hosted language model over HTTPS and getting a response back in JSON. In practice, that comes down to four things: getting an API key, sending a request with the right headers and payload, parsing the response, and handling the extras — streaming, retries, and usage tracking. There's no magic to it; it's a standard REST integration with a few LLM-specific quirks.
This guide walks through the process end to end, from your first request to production-ready patterns like streaming and tool use, so you can go from zero to a working integration in under an hour.
Step 1: Get API access and a key
Every LLM API requires authentication, usually via a bearer token passed in the Authorization header. Depending on the provider, you'll either:
- Sign up directly with the model provider and generate a key in their console, or
- Use a gateway service that sits between your app and the underlying model, adding things like team seats, usage dashboards, and a stable API surface.
If you already have Claude access through a subscription and want a proper HTTPS API without dealing with separate billing, SubToAPI turns that access into an API key (sub_live_...) you can use immediately. Sign up at /signup and grab your key from the dashboard — no separate provider account needed.
Whichever route you take, treat the key like a password:
- Store it in environment variables, never in source code.
- Scope keys per environment (dev, staging, prod).
- Rotate keys if one is ever exposed in logs or a client-side bundle.
Step 2: Make your first request
Once you have a key, the core interaction is a POST request with a model name, a list of messages, and a token limit. Here's a minimal example:
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-3-5-sonnet",
"max_tokens": 512,
"messages": [
{"role": "user", "content": "Summarize this ticket in two sentences."}
]
}'
The response is a JSON object containing the generated text, the stop reason, and token usage. In JavaScript:
const res = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "claude-3-5-sonnet",
max_tokens: 512,
messages: [{ role: "user", content: "Summarize this ticket in two sentences." }]
})
});
const data = await res.json();
console.log(data.content[0].text);
At this point you have a working integration. Everything else — streaming, tools, retries — is a layer on top of this same request shape. See /docs/messages for the full request and response schema.
Step 3: Handle streaming responses
For chat UIs or anything where users are waiting on output, streaming matters. Instead of waiting for the full response, the API sends chunks as they're generated, and you render them incrementally.
const res = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "claude-3-5-sonnet",
max_tokens: 512,
stream: true,
messages: [{ role: "user", content: "Write a haiku about deploys." }]
})
});
const reader = res.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
process.stdout.write(decoder.decode(value));
}
The stream sends server-sent events with incremental text deltas until it hits a final "done" event. Full details, including how to parse each event type, are in /docs/streaming.
Step 4: Add tool use for structured actions
Most real applications need the model to do more than generate text — call a function, query a database, or return structured data. This is done by describing tools (name, description, JSON schema for inputs) in the request, and the model responds with a tool call instead of plain text when appropriate.
{
"model": "claude-3-5-sonnet",
"max_tokens": 512,
"tools": [
{
"name": "get_order_status",
"description": "Look up the status of an order by ID",
"input_schema": {
"type": "object",
"properties": { "order_id": { "type": "string" } },
"required": ["order_id"]
}
}
],
"messages": [{ "role": "user", "content": "Where is order 4821?" }]
}
Your code checks the response for a tool call, executes the actual function, and sends the result back in a follow-up message so the model can produce a final answer. This is the pattern behind most "AI agent" features you see in production apps. Details and edge cases are covered in /docs/tools.
Step 5: Manage usage, keys, and teams
Once an integration is live, you need visibility into what it's costing and who's using it. Look for:
- Per-key usage metadata in each response, so you can log token counts without a separate call.
- Multiple keys per environment to isolate dev traffic from production.
- Team seats if more than one person or service needs access — this avoids sharing a single key across a codebase, which makes rotation and auditing painful.
SubToAPI includes usage metadata on every response and team seat management in the dashboard, so you're not building cost tracking from scratch. Plans start at €9/month for solo use, with Team (€19/seat) and Scale (€49/seat) tiers for larger setups — see /pricing for details, or start with the free trial at /signup.
Common mistakes to avoid
- Not setting
max_tokens. Some APIs default to a small or unpredictable limit, cutting off responses mid-sentence. - Ignoring stop reasons. A response can end because of length, a stop sequence, or a tool call — handle each case explicitly.
- Skipping retry logic. Network blips and rate limits happen; a basic exponential backoff on 429/5xx responses prevents silent failures.
- Hardcoding model names. Providers update model versions; keep it in config so you can upgrade without a redeploy.
questions
Do I need to know machine learning to use an LLM API? No. Using the API is a standard HTTP integration — you send JSON, you get JSON back. No model training or ML background required.
What's the difference between calling an LLM API directly and using a gateway like SubToAPI? A gateway adds a consistent API layer, usage tracking, and team management on top of existing access, so you don't manage separate provider billing or build your own dashboard.
Can I test an LLM API without committing to a paid plan? Yes — most providers and gateways, including SubToAPI, offer a free trial at signup so you can validate the integration before choosing a plan.