Python Introduction to LLMs
Table of Contents + −
In the last lesson, Introduction to Deep Learning, you learned how neural networks find patterns in raw data. So you have the foundation. Now we reach the technology behind the AI everyone is talking about: ChatGPT, Claude, and the chatbots that write essays and answer questions like a person. These are large language models, or LLMs. They sound like magic, but underneath they do one surprisingly simple thing. This lesson explains what that is, in plain words, and shows how you would use one from Python.
🤔 What problem do LLMs solve?
For a long time, computers were terrible with human language. They could store text and search it, but they could not really understand a question or write a sensible reply. Language is just too flexible and full of meaning.
LLMs changed this. They can:
- Answer questions in plain language.
- Write emails, stories, and code.
- Summarize a long document into a few lines.
- Translate between languages.
- Hold a back-and-forth conversation that makes sense.
A large language model is a very big neural network trained on a huge amount of text from the internet, books, and more. From all that text it learned the patterns of language so well that it can produce new, sensible text of its own.
🧠 How does an LLM actually work?
Here is the part that surprises people. Under all the cleverness, an LLM does one thing: it predicts the next word. That is it. Given some text, it guesses what word most likely comes next, then again, and again.
Picture your phone’s keyboard suggesting the next word as you type. An LLM is that idea, but enormously more powerful. Say you give it “The sky is”. It has read so much text that it knows “blue” is a very likely next word, so it picks it. Then it looks at “The sky is blue” and predicts the next word, and keeps going. One word at a time, it builds a whole answer.
So the loop is simple:
- You give it some text to start with, called the prompt.
- It predicts the most likely next word and adds it.
- It repeats, each time looking at everything so far, until the answer is complete.
The amazing thing is that “just predicting the next word”, done with a big enough network and enough training text, is enough to write essays, answer questions, and even write working code. The simple idea scales up into something that feels intelligent.
🔤 Tokens: how an LLM sees text
An LLM does not actually work with whole words. It breaks text into pieces called tokens. A token is usually a word or a part of a word. For example, “cat” might be one token, while “unbelievable” might split into “un”, “believ”, and “able”.
Why this matters to you:
- LLMs read and write in tokens, not letters. Their “next word” prediction is really “next token”.
- You are usually charged by the number of tokens when you use a paid LLM, both what you send and what it sends back.
- Models have a token limit, the most tokens they can handle at once. A long document might be too many tokens to fit.
A rough rule of thumb: in English, one token is about four characters, so roughly three-quarters of a word on average. So a 100-word message is around 130 tokens. You do not need exact numbers, just the idea that text is measured in tokens.
Note
The prompt is the text you send the model, and the part it generates back is the response. Writing a clear, specific prompt is the main skill in getting good answers, and people call it prompt engineering.
💡 Using an LLM from Python
You do not train an LLM yourself. Training one costs millions and needs enormous computing power. Instead, you use one that a company already trained, by calling it over the internet through an API, the same idea you learned in the APIs module. You send your prompt, and the model sends back its answer.
This is what calling an LLM from Python looks like. It uses the Anthropic SDK to talk to Claude, one of the leading models. The exact library and model id may change over time, so treat this as the shape, not something to memorize.
import anthropic
client = anthropic.Anthropic(api_key="your-key-here")
response = client.messages.create( model="claude-opus-4-8", max_tokens=100, messages=[ {"role": "user", "content": "Explain what Python is in one sentence."} ])
print(response.content[0].text)Output
Python is a popular, easy-to-read programming language used forweb development, data analysis, automation, and artificial intelligence.Let’s read the important parts:
anthropic.Anthropic(api_key=...)sets up a client with your secret key, which proves you are allowed to use the service.messagesis your conversation. Each message has arole(here"user", meaning you) and thecontent, which is your prompt.max_tokens=100caps how long the answer can be, so it does not run on forever.- The model sends back its reply, and you read it from
response.content[0].text.
So from your side, using an LLM is just an API call: send a prompt, get text back. All the deep learning happens on the company’s powerful computers, not yours.
Caution
Never put your API key directly in code you share or upload, like the "your-key-here" spot above. A real key is a secret. Keep it in an environment variable or a separate config file, the same safe habit you learned with API authentication.
⚖️ What LLMs are good and bad at
LLMs are powerful, but they are not magic, and knowing their limits keeps you out of trouble.
| Good at | Not reliable at |
|---|---|
| Writing and rewriting text | Exact facts and dates (it can make them up) |
| Summarizing long text | Hard math and precise counting |
| Answering general questions | Very recent events after its training |
| Explaining and writing code | Knowing what it does not know |
The biggest trap is the hallucination: when an LLM states something false but sounds completely confident. Because it predicts likely-sounding text, it can invent a fact, a name, or a source that does not exist. So always check important facts it gives you, especially numbers, dates, and quotes.
⚠️ Common Misunderstandings
A few myths worth clearing up:
- “The LLM knows the truth.” It does not. It predicts likely text, so it can be confidently wrong. Always verify important facts.
- “It understands like a person.” It is matching patterns from training text, not understanding the way you do. It is astonishingly good at it, but it is still prediction.
- “I have to train my own model.” Almost never. You use a model a company already trained, through an API. Training your own is rare and very expensive.
✅ Best Practices
Good habits for working with LLMs:
- Write clear, specific prompts. The better your question, the better the answer. Vague in, vague out.
- Always check important facts the model gives you. Treat it as a smart assistant, not a source of truth.
- Keep your API key secret, in an environment variable, never pasted into shared code.
- Set a sensible
max_tokensso answers stay the length you need and you do not pay for more than you want.
🧩 What You’ve Learned
✅ A large language model (LLM) is a huge neural network trained on massive amounts of text to understand and produce language.
✅ Underneath, it just predicts the next token over and over, building an answer one piece at a time.
✅ Text is measured in tokens (a word or part of a word), which affect cost and the model’s size limit.
✅ You use an LLM by sending a prompt through an API and reading the text it sends back; you do not train it yourself.
✅ LLMs can hallucinate, stating false things confidently, so always check important facts and keep your API key secret.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
At its core, what does a large language model actually do?
Why: An LLM predicts the most likely next token given the text so far, then repeats. That simple loop, at huge scale, produces its answers.
- 2
What is a 'token' in the context of LLMs?
Why: LLMs work in tokens, usually a word or a piece of a word. Cost and the model's size limit are both measured in tokens.
- 3
How do you normally use a powerful LLM from Python?
Why: You send your prompt to a pre-trained model over an API and read the response. The heavy computing happens on the provider's machines, not yours.
- 4
What is a 'hallucination' from an LLM?
Why: Because it predicts likely-sounding text, an LLM can invent facts, names, or sources that do not exist, so you should verify important details.
🚀 What’s Next?
You now understand the AI world from data all the way up to LLMs. That is a huge amount of ground. It is time to put your Python skills to work on real, finished programs. Next we start the projects module, beginning with a classic everyone should build: a calculator application.