LLM Provider SDKs: OpenAI & Anthropic
Learn the OpenAI and Anthropic Python SDKs — chat completions, system prompts, and streaming — with working examples of each.
Introduction
OpenAI and Anthropic are the two most widely used LLM provider SDKs, and nearly every orchestration framework later in this course supports both as a backend. Learning their two APIs directly, before adding any framework on top, makes it much easier to understand what a framework is actually doing for you.
- How to install and call the openai SDK for chat completions.
- How to install and call the anthropic SDK for messages.
- A direct comparison of the two APIs' shapes and conventions.
A Real-Life Analogy First
Think of OpenAI and Anthropic as two different, excellent translation agencies. Both employ highly skilled translators (the models), both accept a document to translate (your messages) and hand back a translated document (the response) — but each agency has its own intake form: one asks you to note special instructions in a dedicated field at the top (Anthropic's separate system parameter), while the other lets you slip that same instruction in as the first item on the document itself (OpenAI's system message inside the list). Once you have filled out one agency's form, the second one's form looks instantly familiar.
OpenAI: The GPT SDK
Use case: the openai package calls OpenAI's GPT models through a chat-completions style API — a list of role-tagged messages in, a generated message out. It also supports streaming, structured outputs, and tool/function calling.
pip install openaiOpenAI in Action
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 coding assistant."}, {"role": "user", "content": "What does the yield keyword do in Python?"}, ], temperature=0.3,)
print(response.choices[0].message.content)Click Run to see what this code prints.
The system message sets persistent behavior for the whole conversation (tone, constraints), while user messages carry the actual request. Nearly every provider SDK in this course, including Anthropic's below, uses this same role-based message structure — it is the same idea as briefing a new employee once ("always answer politely and concisely") before handing them individual customer questions one at a time.
Anthropic: The Claude SDK
Use case: the anthropic package calls Anthropic's Claude models through a Messages API. It looks almost identical to OpenAI's shape by design, but keeps the system prompt as a separate top-level parameter rather than a message in the list.
pip install anthropicAnthropic in Action
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from the environment
response = client.messages.create( model="claude-sonnet-5", max_tokens=200, system="You are a concise coding assistant.", messages=[ {"role": "user", "content": "What does the yield keyword do in Python?"} ],)
print(response.content[0].text)Click Run to see what this code prints.
OpenAI vs Anthropic: Key Differences
| Aspect | OpenAI SDK | Anthropic SDK |
|---|---|---|
| Response call | client.chat.completions.create() | client.messages.create() |
| System prompt | A message with role "system" inside the list | A separate top-level system parameter |
| max_tokens | Optional, model has a default | Required on every call |
| Response text location | response.choices[0].message.content | response.content[0].text |
| Both support | Streaming, tool/function calling, structured outputs | Streaming, tool use, structured outputs |
In practice, most orchestration frameworks (LangChain, LlamaIndex — covered in lessons 9 and 10) abstract these differences behind a shared interface, so switching providers often means changing one line of configuration rather than rewriting your application logic.
Where You've Already Used Both
Even if you have never written a line of code calling either SDK, you have very likely used products built on top of both.
ChatGPT (chat.openai.com)
OpenAI's own product, built on the exact SDK shown in this lesson's first example, running against their models directly.
Claude.ai
Anthropic's own product, built on the exact SDK shown in this lesson's second example.
GitHub Copilot, Notion AI, many IDE assistants
Frequently call OpenAI's models under the hood for code and text completion features.
Many customer support chat widgets
Often built on Anthropic's Claude for its strengths in careful, instruction-following responses.
Common Mistakes
- Forgetting max_tokens on an Anthropic call — unlike OpenAI, it is a required parameter and the call will raise an error without it.
- Reading response text from the wrong field after switching providers (choices[0].message.content vs content[0].text).
- Not setting a temperature or max token limit at all, leading to unpredictably long or expensive responses.
Best Practices
- Wrap provider calls in your own thin function so switching providers later touches one place, not every call site.
- Set an explicit max_tokens and temperature rather than relying on defaults, especially in production.
- Use the system prompt for stable behavior and instructions, and user messages only for the actual request.
Frequently Asked Questions
Either — the concepts transfer directly. OpenAI's API is slightly more common in tutorials; Anthropic's is a close second and very similar in shape.
Yes, and it is common — some teams call different providers for different tasks, or use one as a fallback if the other is unavailable.
Yes, both support streaming so you can display tokens as they are generated instead of waiting for the full response — useful for chat interfaces.
It controls how "safe versus adventurous" the model's word choices are. A low temperature (like 0.2) makes the model stick closely to its most likely, predictable answer every time — good for factual or code-related tasks. A high temperature (like 0.9) lets it take more creative risks with word choice — better suited for brainstorming or creative writing, at the cost of some consistency.
Yes — unlike the free ChatGPT or Claude.ai website (which has its own separate free tier), the API is billed per request based on tokens in and out, which is exactly why lesson 23 dedicates a full lesson to tracking that cost.
Key Takeaways
- openai and anthropic are the two most widely used LLM provider SDKs.
- Both use a role-based message structure, with a small but important difference in how the system prompt is passed.
- Response text lives in different fields on each SDK — check the shape when switching providers.
- Orchestration frameworks later in this course abstract most of these differences away.
- Products you likely already use daily, like ChatGPT and Claude.ai, are built on exactly these two SDKs.
Summary
OpenAI and Anthropic's SDKs are the entry point to nearly everything else in this course — every framework and pipeline ahead is ultimately built on top of a call that looks like the two shown here.
- You can install and call the openai SDK for a chat completion.
- You can install and call the anthropic SDK for a message.
- You know the key differences between the two, and can name real products built on each.