Calling a language model API directly is easier than most guides suggest. The difficulty is almost entirely in the setup and error handling around it.
Step 1: get a key and store it safely
Generate an API key in your provider's console and put it in an environment variable. Never commit it. If a key has ever been pasted into a file that reached version control, rotate it immediately — automated scanners find these within minutes.
Step 2: make one request
Send a request with a system instruction and a user message. Read the response object rather than printing the whole thing; the useful text usually sits nested several levels deep, and the surrounding metadata tells you how many tokens you were charged for.
Step 3: handle the four common errors
- 401. Key missing, malformed or revoked. Check that the environment variable is actually loaded in your shell.
- 429. Rate limited. Retry with exponential backoff and jitter rather than immediately.
- 400. Malformed request, usually an oversized input or a message role used incorrectly.
- Timeout. Set an explicit client timeout. The default in many libraries is effectively infinite.
Step 4: log tokens and cost
Record prompt tokens, completion tokens and cost per call from day one. This is much harder to retrofit than to add now, and it is the first thing you will need when costs surprise you.
Step 5: keep the call thin
Put the API call behind one small function. When a provider changes a parameter or you switch vendors, you edit one place.
Comments (0)
Log in to join the discussion
Log InNo comments yet