Calling a language model from your own code is the first step toward building AI features. Every major provider — OpenAI, Anthropic, Google and others — works in a similar way, so the concepts in this guide transfer between them. The examples use Python and an OpenAI-style client, but each provider’s quickstart shows the equivalent in its own SDK.
Step 1 — Get an API key and keep it secret
Create an account with a provider, add billing or use the free tier where available, and generate an API key. Treat it like a password:
- Store it in an environment variable, never in your code.
- Never commit it to Git or paste it into chats or screenshots.
- Set a spending limit in the provider’s dashboard.
On macOS or Linux:
export OPENAI_API_KEY="your-key-here"
On Windows PowerShell:
$env:OPENAI_API_KEY = "your-key-here"
Step 2 — Install the SDK
pip install openai
Step 3 — Send a request
from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY from the environment
response = client.chat.completions.create(
model="MODEL_NAME", # pick a current model from the provider's docs
messages=[
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Explain what an API is in two sentences."},
],
max_tokens=200,
)
print(response.choices[0].message.content)
Replace MODEL_NAME with a model listed in the provider’s documentation.
Understanding the request
- model: which model answers. Cheaper, faster models are ideal for learning.
- messages: the conversation. A system message sets behavior; user messages contain requests; assistant messages contain previous replies. The API is stateless: to continue a conversation, send the whole history again.
- max_tokens: an upper limit on the length of the reply, which also caps cost.
- temperature (optional): higher values give more varied answers, lower values more consistent ones.
Understanding the response
The response includes the generated text and a usage field with input and output token counts. Log usage from the start — it is how you will estimate costs later. See our guide to AI pricing.
Step 4 — Handle errors
Network calls fail. Handle at least:
- Authentication errors — wrong or missing key.
- Rate limits — too many requests; retry after a delay with exponential backoff.
- Timeouts and server errors — retry a limited number of times.
- Content refusals — the model may decline some requests; handle gracefully.
Step 5 — Stream the output
For chat interfaces, streaming returns tokens as they are generated, so users see text appear immediately. Most SDKs support a stream option that yields chunks you print or send to the browser.
Next steps
- Ask for structured output so your code can parse results reliably — see structured outputs and function calling.
- Add retrieval to answer from your documents.
- Build a small evaluation set before changing prompts or models.
Common mistakes to avoid
- Hard-coding the API key. Keys in source code end up in repositories, logs and screenshots. Use environment variables or a secrets manager.
- Calling the API from the browser. Front-end code exposes your key to every visitor. Call the API from your server.
- Forgetting that the API has no memory. Each request must include the conversation history you want the model to see.
- No limits. Set max_tokens, spending limits and rate limits from day one to avoid surprise bills.
A small project to practice
Turn the snippet into something useful: a command-line summarizer.
- Read a text file whose path is passed as an argument.
- Send it to the model with a system message such as “Summarize the user’s text in five bullet points for a busy manager.”
- Print the summary and the token usage reported in the response.
- Add a flag to choose between two models, and compare quality, speed and cost on the same files.
- Save each run’s inputs, outputs and token counts to a log file.
This tiny project exercises every core concept — messages, parameters, usage, errors and model choice — and the log you build becomes the seed of an evaluation set. When the summaries are good enough, try asking for the bullets as JSON so another program can use them.
Key takeaways
- Store API keys in environment variables and set spending limits.
- A request specifies a model and a list of messages with system, user and assistant roles.
- The API is stateless: resend history to continue a conversation.
- Log token usage from the start to understand costs.
- Handle authentication errors, rate limits and timeouts with retries and backoff.
Frequently asked questions
Which provider should I start with?
Any major provider works for learning. Choose one with good documentation in your language and a free tier or low-cost small models.
How much will experimenting cost?
Learning with small models typically costs very little, but always set a spending limit in the dashboard to stay safe.
Can I switch providers later?
Yes. Concepts are similar, and many libraries offer a common interface. Keep model and provider settings in configuration rather than scattered through code.