Sub topic 1
What a provider SDK gives you
Prerequisites
This chapter assumes you can read basic Python, including functions and virtual environments. It also assumes you have called an LLM chat API at least once and know what JSON and HTTP requests are. You do not need any framework experience. Later chapters cover LangChain, LlamaIndex, and PydanticAI.
What a provider SDK is
A provider SDK is a client library published by a model vendor. It wraps the vendor's HTTP API in Python classes and functions. The OpenAI Python package is one example. Many other providers publish SDKs with a similar shape, and some offer OpenAI-compatible endpoints that the same package can call.
The SDK does a few jobs for you:
- It builds the HTTP request and sets the authentication header.
- It reads the API key from an environment variable, so the key stays out of your code.
- It turns the JSON response into typed Python objects.
- It raises exceptions for failures such as rate limits and timeouts. Recent versions also retry some of these failures automatically.
Everything else is your job. That includes prompt design, conversation memory, parsing, and deciding what to do next. Keep this boundary in mind, because every framework in this tutorial starts from it.
The request and response cycle
A chat call has three parts: a client, a list of messages, and a response.
- The client holds your credentials and connection settings.
- A message is a dictionary with a
roleandcontent. The role issystem,user, orassistant. - The response holds one or more choices. Each choice contains a message from the model.
A first call
This is the whole request with no abstraction layer in between.
from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY from the environment
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Explain what an API client library is in one sentence."},
],
)
print(response.choices[0].message.content)Run pip install openai in a virtual environment and set OPENAI_API_KEY before you try it.
Three details matter here:
OpenAI()finds the key on its own. If the variable is missing, the constructor raises an error.- The
messageslist is the entire context the model sees. The API is stateless, so nothing carries over between calls. response.choices[0].message.contentis the reply text. Thechoiceslist usually has one item unless you ask for more.
What you still control
Because the SDK stays thin, you can see and control every part of the call:
| Concern | Who handles it |
|---|---|
| HTTP, auth, response parsing | SDK |
| Prompt text and message order | You |
| Conversation history | You |
| Model name and parameters such as temperature | You |
| Validating the reply | You |
| Provider-specific features | SDK, if the vendor exposes them |
This control has a cost. Each task in the table's "You" rows is code you write and maintain. The next chapter looks at where that starts to hurt.
Check your understanding
Exercise
A user asks a follow-up question, but the model answers as if the first question never happened. Your code calls client.chat.completions.create with only the new user message each time. What is wrong, and how do you fix it?
Show answer
The API is stateless, so each call sees only the messages you send. Keep a list of all messages, append the model's reply (as an assistant message) and the new user message after each turn, and send the full list on every call.
Exercise
Name two jobs a provider SDK does for you and two jobs it leaves to you.
Show answer
The SDK handles authentication and HTTP requests, and it parses responses into Python objects (it also raises errors for failed calls). You still handle prompt design and conversation history. Validating the model's output is also your job.