Introduction to LLMs
What large language models are and what they can do
Skills taught: llms.basics
What is a Large Language Model?
A Large Language Model (LLM) is a neural network trained on vast text corpora to predict the next token. Through this training they develop impressive language and reasoning abilities. Modern LLMs like claude-opus-4-5, GPT-4o, and Gemini 1.5 Pro can write code, answer questions, summarise documents, translate languages, and reason through complex problems.
- Token
- The basic unit an LLM processes. A token is roughly 4 characters or 0.75 words in English. 'ChatGPT is great!' is about 6 tokens. Code tends to use more tokens per character than prose.
- Context Window
- The maximum number of tokens an LLM can process at once — including both the input (prompt + history) and output. Claude 3.5 Sonnet has a 200K token context window (~150,000 words).
How LLMs Work — the Core Loop
- You send a text prompt (tokenised to integers)
- The model runs the tokens through transformer layers (attention + feed-forward)
- It outputs a probability distribution over its full vocabulary
- The highest-probability token is selected (or sampled with temperature)
- That token is appended to the context and the loop repeats until the response is complete
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY env var
message = client.messages.create(
model="claude-opus-4-5",
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain what an LLM is in one paragraph."}
]
)
print(message.content[0].text)
print(f"Tokens used — in: {message.usage.input_tokens}, out: {message.usage.output_tokens}")
Major LLM Providers
Claude vs GPT-4o
- Claude (Anthropic): Constitutional AI, long context (200K), very strong at coding and analysis
- GPT-4o (OpenAI): Multi-modal (vision + audio), massive ecosystem, fine-tuning available
Tip: LLMs are probabilistic. The same prompt can produce different outputs. Set temperature=0 for deterministic, reproducible results (best for extraction/classification). Use higher temperatures (0.7–1.0) for creative tasks.
Warning: LLMs can hallucinate — confidently state false information. Always validate LLM outputs that will be shown to users or used in automated pipelines, especially for facts, numbers, and citations.
import anthropic
client = anthropic.Anthropic()
# Deterministic: use temperature=0 for classification/extraction
result = client.messages.create(
model="claude-haiku-5-5",
max_tokens=10,
temperature=0,
messages=[{"role": "user", "content": "Classify: 'I love this product!' -> POSITIVE or NEGATIVE?"}]
)
print(result.content[0].text) # Always: POSITIVE
# Creative: use higher temperature
story = client.messages.create(
model="claude-haiku-5-5",
max_tokens=200,
temperature=0.9,
messages=[{"role": "user", "content": "Write the first line of a sci-fi story."}]
)
print(story.content[0].text) # Different every time
API pricing is per token. claude-haiku-5-5 costs ~$0.25/1M input tokens. claude-opus-4-5 costs ~$15/1M. For prototyping, always use Haiku or Sonnet to control costs.
Frequently Asked Questions
Can LLMs remember previous conversations?
No intrinsic memory. You must include prior turns in the context window (messages array) to give the model conversation history.
What is the difference between a base model and a chat model?
A base model only does next-token prediction. A chat model has been fine-tuned with RLHF to follow instructions and be helpful. All commercially available models (Claude, GPT-4) are chat models.
Which model should I start with?
Start with claude-haiku-5-5 or gpt-4o-mini for cost-effective prototyping. Move to Sonnet or GPT-4o when you need higher quality. Use Opus/GPT-4 only when other models fall short.