The OpenAI API, From First Call to Something Useful
The gap between 'I want to use the OpenAI API' and a working feature is about forty lines of code and one concept: tokens. This guide walks through authentication, the shape of a request and response, streaming, how the pieces fit for tools and structured output, how pricing actually works, and the errors everyone hits first.
The two things to understand first
- Tokens, not words. Input and output are measured and billed in tokens, roughly 3/4 of an English word. Longer prompts cost more; repeated context costs more every single call.
- It is stateless. The model remembers nothing between calls. Whatever context the next call needs, you send again — which is why apps keep a conversation array and resend it.
Your first call
curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "<a model id>",
"messages": [
{"role": "system", "content": "You are terse."},
{"role": "user", "content": "Explain what a hash table is."}
]
}'Three things to notice. The model id goes in the body. Roles matter: system sets behaviour, user is the request. And the response nests the useful text a couple of levels down, which trips up almost everyone the first time.
Parameters worth knowing
- temperature — higher is more varied. Keep it low for anything factual, extraction or code; raise it for brainstorming.
- max tokens — caps the reply length. Useful as a cost ceiling and as a guard against runaway output.
- stop sequences — text that ends generation. A cheap way to make the model output exactly one line.
- tools / functions — let the model ask your code to do something, such as look something up. This is how you get grounded answers instead of confident guesses.
- response_format — requests structured output like a JSON object, which removes almost all parsing pain.
What it actually costs
You are billed per token, separately for input and output, and output is priced higher. Two consequences matter more than the exact numbers: a large system prompt is a recurring cost on every single call, and sending a whole conversation history grows the bill on every turn.
- Set a hard spending limit on the account before you build anything. It is a real setting, not a suggestion.
- Summarise long history instead of resending it verbatim.
- Use the cheapest model that does the job; reserve the strongest one for genuinely hard requests.
- Cache what does not change between calls.
Mistakes everyone makes first
- Exposing the key client-side. The single most common serious mistake.
- Sending the whole conversation on every turn and wondering why the bill jumped.
- No error handling, so a rate limit looks like a broken app.
- Retrying non-retryable errors like a 400, which never succeeds and wastes your quota.
- Trusting output without validating it. Parse it; do not assume the shape.
You can debug almost any API integration in the browser before writing a line of framework code. Build the request, inspect the response, and turn that JSON into a TypeScript type for free.
All of these run in your browser — no account, no upload, free forever.
Explore the Full SlashAI Library
Every prompt in our guides is part of our offline-ready vault of verified commands and instant browser tools. Free forever, no account required.
Browse All Commands