LLMs Basics
10 examples to get you started with LLMs & GenAI - 7 basic and 3 intermediate.
Prerequisites
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_KEY- Python 3.14.0 with provider SDK installed.
- API key for at least one provider (OpenAI or Anthropic).
Basic Examples
1. Your First Chat Completion
Send 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)messagesis a list of role/content dicts.roleissystem,user, orassistant.- Response text is in
choices[0].message.content.
Related: OpenAI & Other SDKs - provider patterns
2. System and User Prompts
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?"},
],
)- System prompt sets persona, tone, and constraints.
- User prompt carries the actual question or task.
- System prompts are not visible to end users in most UIs.
Related: Prompt Engineering - prompt patterns
3. Count Tokens Before Sending
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)}")- Token count determines API cost and whether input fits the context window.
- Rough rule: 1 token ~ 4 characters in English.
- Count both input and expected output when budgeting.
Related: Tokens, Cost & Rate Limits - budgeting
4. Control Randomness with Temperature
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=0for factual, repeatable answers.temperature=0.7-1.0for creative writing.top_p(nucleus sampling) is an alternative to temperature.
5. Limit Output Length
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_tokenscaps completion length, not input.- Set based on expected answer size.
- Stopping early saves money on verbose models.
6. Multi-Turn Conversation
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)- Include full conversation history for multi-turn context.
- Longer history uses more tokens - trim old turns when needed.
- Assistant responses must be included for continuity.
7. Call Anthropic Claude
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)- Anthropic separates
systemfrommessages. contentis a list of content blocks (text, images, tools).- Model IDs change - check the provider docs for current names.
Related: Anthropic Claude SDK - full Claude guide
Intermediate Examples
8. Few-Shot Prompting
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)- Few-shot examples teach format without fine-tuning.
- 2-5 examples usually suffice; more uses tokens.
- Examples should cover edge cases you care about.
Related: Prompt Engineering - few-shot patterns
9. Structured JSON Output
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.- Mention "JSON" in the prompt for best results.
- Validate parsed output with Pydantic before using.
Related: Structured Output - Pydantic schemas
10. Environment-Based API Key
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)- Use
.envfiles locally withpython-dotenv(add.envto.gitignore). - Production: secrets manager (AWS Secrets Manager, Vault).
- Rotate keys if accidentally committed.
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+.