AI Infrastructure & Tools9 min read

AI APIs & SDKs

Build AI into your apps β€” OpenAI, Anthropic, and Google APIs
scope:Intermediatedifficulty:Medium

What Is an AI API?

You can chat with ChatGPT or Claude on their websites β€” but what if you want to add that same intelligence to your own app? That's where APIs (Application Programming Interfaces) come in.

An API is a way for two pieces of software to talk to each other. AI APIs let your website, mobile app, or any software communicate with OpenAI's, Anthropic's, or Google's models.

Think of a restaurant. You (the client) don't go into the kitchen β€” you tell the waiter (API) your order and they bring you food. An API works the same way β€” your app sends a request and gets an answer back.

Major AI API Providers

  • OpenAI API: GPT-4o, GPT-4o-mini, DALL-E, Whisper, and more. The most popular and widely used.
  • Anthropic API: Claude Opus, Sonnet, Haiku. Strong on long contexts and safety.
  • Google AI API: Gemini Pro, Gemini Flash. Integrated with Google's ecosystem.
  • Together AI, Groq: Host open-source models (Llama, Mistral) β€” cheap and fast.
Note: API Key Security: Your API key is your digital key. Never put it on GitHub, in frontend code, or anywhere public. Always use environment variables and make API calls from the backend.

What Is an SDK?

An SDK (Software Development Kit) is a library that makes using an API easier. Instead of writing raw HTTP requests, you call SDK functions.

For example, with OpenAI's Python SDK, you just call client.chat.completions.create() β€” no need to worry about HTTP headers, JSON parsing, or error handling.

Understanding Costs

AI API costs are typically calculated in tokens:

  • Input tokens: What you send (prompt + system message).
  • Output tokens: What the model generates. Usually 2–4x more expensive than input.

Cost-saving strategies: Use smaller models for simple tasks (GPT-4o-mini, Haiku), keep prompts concise, use caching, and limit max_tokens.

Using Different AI APIs

# === OpenAI API ===
from openai import OpenAI
client = OpenAI() # Reads OPENAI_API_KEY from env
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hello!"}],
max_tokens=100
)
print(response.choices[0].message.content)
# === Anthropic API ===
import anthropic
client = anthropic.Anthropic() # Reads ANTHROPIC_API_KEY from env
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=100,
messages=[{"role": "user", "content": "Hello!"}]
)
print(message.content[0].text)
# === Google Gemini API ===
import google.generativeai as genai
genai.configure() # Reads GOOGLE_API_KEY from env
model = genai.GenerativeModel("gemini-pro")
response = model.generate_content("Hello!")
print(response.text)
Output
# All three APIs work similarly:
# 1. Create a client (with your API key)
# 2. Send a model name and messages
# 3. Get a response
# The difference is just in function names and parameters.
Challenge

Quick check

What is an API Key and why is it important?

Continue reading