How LLMs Work
Large Language Models (LLMs) like GPT-4, Claude, Gemini and Llama predict the next token (a word piece) given the previous tokens.
Introduction
Large Language Models (LLMs) like GPT-4, Claude, Gemini and Llama predict the next token (a word piece) given the previous tokens. By doing this on trillions of tokens, they learn grammar, facts, reasoning patterns and code. That single skill — next-token prediction — is enough to power every agent you'll ever build.
Beginner analogy: autocomplete on your phone, but trained on the entire internet and books. The agent on top tells it what to autocomplete.
Learning objective: Apply How LLMs Work when building TechLearningPRO agents — with a narrow tool set, observable traces, and stop conditions you can defend in review.
Treat agents as production systems: plan → act → observe → reflect, with budgets on steps, tokens, and tool blast radius. Prefer one reliable tool loop before multi-agent swarms.
Purpose of this lesson
Apply How LLMs Work when building TechLearningPRO agents — with a narrow tool set, observable traces, and stop conditions you can defend in review.
Understanding the topic
Core concepts:
- Input is tokenised into ~3-4 characters per token.
- The model outputs logits — probability over every token in its vocabulary.
- Sampling (temperature, top-p) picks the next token.
- The model is stateless — context = the whole prompt every call.
- Context window limits how much fits (8k → 1M+ tokens depending on model).
Step-by-step explanation
- Define the goal How LLMs Work owns and the tools it is allowed to call.
- Walk the architecture diagram: planner, tool caller, memory, and termination.
- Implement the smallest agent loop for a TechLearningPRO Orders / support task.
- Inspect a failed run: missing observation, loop, or unsafe tool argument — then fix the guardrail.
Syntax reference
Visual workflow / architecture:
prompt: "The cat sat on the"│ tokenize▼[The][ cat][ sat][ on][ the]│▼ transformer attention│logits → P(mat)=0.7, P(rug)=0.2, P(table)=0.05 …│ sample▼next token: " mat"│ append▼"The cat sat on the mat" → repeat
Informative example
Minimal agent loop sketch:
# TechLearningPRO agent sketch — How LLMs Workgoal = "Resolve ticket T-104: refund order ORD-9"tools = [get_order, create_refund, notify_user] # least privilegefor step in range(MAX_STEPS):thought = llm.plan(goal, memory, tools)if thought.done: breakobs = tools[thought.tool](**thought.args) # validate args firstmemory.append(thought, obs)return memory.summary()
Real-world use
GPT-4 powers ChatGPT and Copilot; Claude powers Cursor; Gemini powers Google Workspace; Llama powers Meta AI and most open-source agents.
Security implications
Agents amplify blast radius: every tool call is privileged automation.
- Allowlist tools and validate schemas before execution; never pass raw LLM JSON unchecked.
- Keep secrets in a vault — agents get scoped credentials, not root keys.
- Log traces with redaction; require human approval for irreversible money/data actions.
Best practices
- Learn temperature: 0 = deterministic, 1+ = creative.
- Mind the context window — pay for every token in and out.
- Cache prompts when possible (Anthropic & OpenAI both support it).
Common mistakes
- Treating the LLM as omniscient — it's a next-token predictor, not a database.
- Forgetting the context limit until production errors.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionQ1. What does an LLM actually predict?+
Answer
2IntermediateQuestionQ2. What is temperature?+
Answer
3IntermediateQuestionQ3. Why are LLMs stateless?+
Answer
4AdvancedQuestionQ4. Difference between context window and training data?+
Answer
5AdvancedQuestionQ5. Scenario: your agent keeps cutting off mid-answer. What's wrong?+
Answer
Hands-on exercise
Hands-on + interview drill:
- Build a single-tool agent for How LLMs Work with MAX_STEPS=5 and a dry-run mode.
- Q1. What does an LLM actually predict?
- Q2. What is temperature?
- Q3. Why are LLMs stateless?
- Q4. Difference between context window and training data?
- Q5. Scenario: your agent keeps cutting off mid-answer. What's wrong?
Summary
How LLMs Work is a production agentic skill: narrow tools, observable loops, and hard stop conditions — autonomy without uncontrolled side effects.