Productivity 19-Year-Old Founder Emerges From Stealth With $11 Million to Sell You a $3,499 'Brain in a Box' That Runs Your AI Agents at Home AI Agents Half a Million Interviews In: HackerRank's AI Interviewer Chakra Goes GA, and It Wants to Replace Three Hiring Rounds With One Security After Claude Agents Escaped Its Sandbox 3 Times, Anthropic Deploys Real-Time Classifiers to Stop the Next Escape Before It Happens Business Meta Halves Its Internal Claude Users to 30,000 and Microsoft Slashes a $1 Billion Anthropic Budget by More Than a Third Business Sony Innovation Fund Backs Primitive Labs, a Startup That Builds Simulated Crowds to Stress-Test Products Before Launch Apple Intelligence Apple Removed the Apple Intelligence Off Switch in macOS 27 — So a Developer Built a CLI That Deletes It Anyway Security OpenAI Turns On Invisible Text Watermarks for ChatGPT in the EU — and Publishes Exactly How Weak They Are News A Mystery 'Space Bunny Alpha' Model Just Topped OpenRouter's Leaderboard With 38.7 Trillion Tokens a Week Productivity 19-Year-Old Founder Emerges From Stealth With $11 Million to Sell You a $3,499 'Brain in a Box' That Runs Your AI Agents at Home AI Agents Half a Million Interviews In: HackerRank's AI Interviewer Chakra Goes GA, and It Wants to Replace Three Hiring Rounds With One Security After Claude Agents Escaped Its Sandbox 3 Times, Anthropic Deploys Real-Time Classifiers to Stop the Next Escape Before It Happens Business Meta Halves Its Internal Claude Users to 30,000 and Microsoft Slashes a $1 Billion Anthropic Budget by More Than a Third Business Sony Innovation Fund Backs Primitive Labs, a Startup That Builds Simulated Crowds to Stress-Test Products Before Launch Apple Intelligence Apple Removed the Apple Intelligence Off Switch in macOS 27 — So a Developer Built a CLI That Deletes It Anyway Security OpenAI Turns On Invisible Text Watermarks for ChatGPT in the EU — and Publishes Exactly How Weak They Are News A Mystery 'Space Bunny Alpha' Model Just Topped OpenRouter's Leaderboard With 38.7 Trillion Tokens a Week

Making Your First AI API Call: A Step-by-Step Walkthrough

Making Your First AI API Call: A Step-by-Step Walkthrough

From API key to a working request in Python, including the four errors that catch almost everyone in the first hour.

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 In

No comments yet