LLMs Basics
10 examples to get you started with LLMs & GenAI - 7 basic and 3 intermediate.
Search across all documentation pages
10 examples to get you started with LLMs & GenAI - 7 basic and 3 intermediate.
uv venv && source .venv/bin/activate
uv pip install "openai>=1.60" "anthropic>=0.40" "tiktoken>=0.8"
export OPENAI_API_KEY="sk-..." # or ANTHROPIC_API_KEYSend a message and get a text response.
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Explain Python list comprehensions in one sentence."}],
)
print(response.choices[0].message.content)messages is a list of role/content dicts.role is system, user, or assistant.choices[0].message.content.Related: OpenAI & Other SDKs - provider patterns
Separate instructions from the user task.
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a concise Python tutor. Answer in 2 sentences max."},
{"role": "user", "content": "What is a generator expression?"},
],
)Related: Prompt Engineering - prompt patterns
Estimate cost and check context limits.
import tiktoken
enc = tiktoken.encoding_for_model("gpt-4o-mini")
text = "Explain gradient descent for a junior developer."
tokens = enc.encode(text)
print(f"tokens: {len(tokens)}")Related: Tokens, Cost & Rate Limits - budgeting
Lower temperature = more deterministic output.
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Give me a creative team name for a Python project."}],
temperature=0.9, # creative
)
response2 = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Is 2+2 equal to 4?"}],
temperature=0.0, # deterministic
)temperature=0 for factual, repeatable answers.temperature=0.7-1.0 for creative writing.top_p (nucleus sampling) is an alternative to temperature.Cap response tokens to control cost.
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Summarize the Python GIL."}],
max_tokens=100,
)max_tokens caps completion length, not input.Pass prior messages for context.
messages = [
{"role": "user", "content": "My app uses FastAPI and PostgreSQL."},
{"role": "assistant", "content": "Great stack! What would you like help with?"},
{"role": "user", "content": "How should I structure database migrations?"},
]
response = client.chat.completions.create(model="gpt-4o-mini", messages=messages)Same pattern, different SDK and message format.
import anthropic
claude = anthropic.Anthropic()
message = claude.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=256,
system="You are a helpful Python assistant.",
messages=[{"role": "user", "content": "What is pydantic used for?"}],
)
print(message.content[0].text)system from messages.content is a list of content blocks (text, images, tools).Related: Anthropic Claude SDK - full Claude guide
Show examples of the desired input/output format.
messages = [
{"role": "system", "content": "Classify sentiment as positive, negative, or neutral."},
{"role": "user", "content": "Text: I love this library!\nSentiment: positive"},
{"role": "assistant", "content": "positive"},
{"role": "user", "content": "Text: It crashed twice today.\nSentiment:"},
]
response = client.chat.completions.create(model="gpt-4o-mini", messages=messages, temperature=0)Related: Prompt Engineering - few-shot patterns
Request JSON for downstream parsing.
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "List 3 Python web frameworks as JSON with name and year fields."}],
response_format={"type": "json_object"},
)
import json
data = json.loads(response.choices[0].message.content)response_format={"type": "json_object"} enforces valid JSON.Related: Structured Output - Pydantic schemas
Load keys from environment, never hardcode.
import os
from openai import OpenAI
api_key = os.environ.get("OPENAI_API_KEY")
if not api_key:
raise RuntimeError("Set OPENAI_API_KEY")
client = OpenAI(api_key=api_key).env files locally with python-dotenv (add .env to .gitignore).Stack versions: This page was written for Python 3.14.0 (stable 3.14, maintenance 3.13), FastAPI 0.115+, Django 5.2, Flask 3.1, Pydantic 2, PyTorch 2.6+, pandas 2.2+, Polars 1.x, ruff 0.9+, and uv 0.6+.
Reviewed by Chris St. John·Last updated Jul 16, 2026