Agentic AI Tutorial 0/80 lessons ~6 min read Lesson 7

    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.

    Course progress0%
    Focus
    13 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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

    1. Define the goal How LLMs Work owns and the tools it is allowed to call.
    2. Walk the architecture diagram: planner, tool caller, memory, and termination.
    3. Implement the smallest agent loop for a TechLearningPRO Orders / support task.
    4. Inspect a failed run: missing observation, loop, or unsafe tool argument — then fix the guardrail.

    Syntax reference

    Visual workflow / architecture:

    bash
    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:

    python
    # TechLearningPRO agent sketch — How LLMs Work
    goal = "Resolve ticket T-104: refund order ORD-9"
    tools = [get_order, create_refund, notify_user] # least privilege
    for step in range(MAX_STEPS):
    thought = llm.plan(goal, memory, tools)
    if thought.done: break
    obs = tools[thought.tool](**thought.args) # validate args first
    memory.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.

    5 questions
    1BeginnerQuestionQ1. What does an LLM actually predict?+

    Answer

    LLMs predict the next token, one at a time.
    2IntermediateQuestionQ2. What is temperature?+

    Answer

    They are stateless; you must resend context each call.
    3IntermediateQuestionQ3. Why are LLMs stateless?+

    Answer

    Temperature and context window are the two knobs you'll tune most.
    4AdvancedQuestionQ4. Difference between context window and training data?+

    Answer

    Temperature and context window are the two knobs you'll tune most.
    5AdvancedQuestionQ5. Scenario: your agent keeps cutting off mid-answer. What's wrong?+

    Answer

    Temperature and context window are the two knobs you'll tune most.

    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.

    Ready to mark this lesson complete?Track your journey across the entire course.