Skip to main content

3.2 OpenAI & Anthropic SDKs

AI-Generated Content

AI-generated content may contain errors. Always verify against official sources.

3.2 OpenAI & Anthropic SDKs

Key Concepts: Python SDK setup · Messages API · Streaming responses

Official Docs: OpenAI Python SDK · Anthropic Python SDK


OpenAI Python SDK

pip install openai python-dotenv

Basic Chat Completion

from openai import OpenAI
from dotenv import load_dotenv

load_dotenv()
client = OpenAI() # reads OPENAI_API_KEY from environment

response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain recursion in 2 sentences."}
],
temperature=0.3,
max_tokens=256,
)

print(response.choices[0].message.content)
print(f"Tokens used: {response.usage.total_tokens}")

Streaming Responses

Streaming returns tokens as they are generated — essential for chat UIs where you want text to appear immediately.

with client.chat.completions.stream(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Write a haiku about Python."}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
print() # newline after stream ends

Accessing the Full Response After Streaming

with client.chat.completions.stream(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Tell me a joke."}],
) as stream:
full_response = stream.get_final_completion()

print(full_response.choices[0].message.content)
print(f"Total tokens: {full_response.usage.total_tokens}")

Anthropic Python SDK

pip install anthropic python-dotenv

Basic Message

import anthropic
from dotenv import load_dotenv

load_dotenv()
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from environment

message = client.messages.create(
model="claude-3-5-haiku-20241022",
max_tokens=256,
system="You are a helpful assistant.",
messages=[
{"role": "user", "content": "Explain recursion in 2 sentences."}
],
)

print(message.content[0].text)
print(f"Input tokens: {message.usage.input_tokens}")
print(f"Output tokens: {message.usage.output_tokens}")

Streaming with Anthropic

with client.messages.stream(
model="claude-3-5-haiku-20241022",
max_tokens=256,
messages=[{"role": "user", "content": "Write a haiku about the ocean."}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
print()

SDK Comparison

FeatureOpenAI SDKAnthropic SDK
system messageInside messages arraySeparate system parameter
Response textresponse.choices[0].message.contentmessage.content[0].text
Token usageresponse.usage.total_tokensmessage.usage.input_tokens + output_tokens
Streaming classclient.chat.completions.stream()client.messages.stream()
Structured outputclient.beta.chat.completions.parse()Tool use / XML tags

Common Mistakes

Common Mistakes
  1. Anthropic system in the wrong place — Anthropic takes system as a top-level parameter, not inside the messages array.
  2. Not flushing stdout during streaming — always use flush=True in print() when streaming to terminal.
  3. Accessing response.content directly (Anthropic) — always use message.content[0].text; content is a list even for single responses.
  4. Using deprecated openai.ChatCompletion.create — the new SDK uses client = OpenAI(); client.chat.completions.create(...). The old global function was removed in SDK v1.0.

Quick Quiz

Test Your Understanding

Q1. In the Anthropic SDK, where does the system prompt go?
A1. As a top-level system="..." parameter in client.messages.create(), NOT inside the messages list.

Q2. How do you access the reply text from an OpenAI streaming response after it finishes?
A2. Call stream.get_final_completion() inside the with block to get the full ChatCompletion object.

Q3. Which OpenAI SDK version removed the old openai.ChatCompletion.create() global function?
A3. SDK v1.0 (released November 2023). Always use client = OpenAI(); client.chat.completions.create(...).


Student Exercise

Exercise 3.2 — Streaming chat
Build a terminal chat loop using streaming. The user types a message, the assistant streams its response token-by-token, and the conversation continues until the user types exit. Maintain conversation history.


Further Reading

Next → 3.3 Running DeepSeek R1 Locally