LLM Foundations & Prompting

This content was generated with the assistance of AI and should be verified against the official documentation of each tool and framework before being relied on in production.

This section’s bibliography lists the reference material consulted while preparing these pages.

Examples across this sub-section target Python 3.12 with the current anthropic and openai Python SDKs (anthropic 1.8.x), Hugging Face transformers 5.17 (with tokenizers 0.23.2) and tiktoken 0.14.0, TRL 1.14, DSPy 3.4.0, Ollama 0.34 serving llama3.1:8b (and the thinking-capable qwen3:8b), and Java 21 with Spring Boot 4.1 / Spring AI 2.0.1, calling the hosted models claude-sonnet-5, claude-haiku-4-5, claude-opus-5-5 and claude-fable-5-1, alongside OpenAI’s current flagship line (gpt-6-luna, gpt-6-sol, gpt-6-astra), with gpt-6-sol as the OpenAI model the reasoning and general examples call. This baseline was checked and re-verified as current against each vendor’s own documentation on 2026-09-28; every page states any narrower version it targets in its own intro, and this landing page’s baseline is the one to trust if an individual page and this page ever appear to disagree after a later update.

Most pages reuse DocsAssistant, the same running example the Vector Databases & RAG section uses: an agent that answers questions about this site’s own AsciiDoc pages, using one embedding model and one chat model across every stack so the examples stay interoperable (see that page’s scenario table for the exact models, corpus and indexes). This sub-section picks the story up one level below retrieval: what the chat model itself is, how it turns text into tokens and those tokens into an answer, how to prompt it well, and how to choose which model to call in the first place.

Reading order

The 19 pages below build up in three stages, plus a cheat sheet. How LLMs work explains the model itself, from what an LLM is down to why it sometimes gets things wrong. Prompt and context engineering covers how to talk to that model reliably, and how to manage what occupies its context window as an application grows. Choosing models is about picking among today’s actual models and estimating what a call will cost. Read them in order if you are new to LLMs; jump straight to a page if you already know the basics and need one topic.

What’s covered

How LLMs work

  • What Is an LLM? — language model to LLM to foundation model, the three-layer AI stack, and what self-supervised pre-training on next-token prediction actually produces.

  • Tokens & Tokenization — BPE and byte-level tokenizers, chat templates, counting tokens, and the token-to-cost-and-latency formula.

  • Transformer Architecture — the decoder-only forward pass, self-attention, RoPE, MQA/GQA, MoE and Mamba-style alternatives.

  • How LLMs Are Trained — pre-training, scaling laws, SFT, preference tuning (RLHF, DPO) and RL for reasoning (RLVR/GRPO).

  • Sampling & Decoding — temperature, top-k/top-p, stop sequences, logprobs, and which parameters reasoning models ignore.

  • Context Windows & Memory — the context window as working memory, the KV cache, long-context degradation, and prompt caching economics.

  • Reasoning Models — extended thinking, effort and budget parameters, and when reasoning is worth its extra latency and cost.

  • Multimodal Models — ViT patches, CLIP, and native image/PDF and audio input and output.

  • Hallucinations & Limitations — why models hallucinate, related failure modes, and the mitigations that actually help.

Prompt and context engineering

  • Prompt Engineering Fundamentals — roles, the anatomy of a prompt, the specificity checklist, and few-shot examples.

  • Prompting Techniques — chain-of-thought, prompt chaining, self-consistency, tree-of-thought, self-critique, role prompting and prefilling.

  • Structured Output & Tool Calling — JSON-schema structured outputs, constrained decoding, Pydantic/Jackson and Spring AI’s entity(), and tool calling.

  • Context Engineering — context budgets, just-in-time retrieval, compaction, structured note-taking and progressive disclosure.

  • Managing Prompts — prompts as versioned code, golden-set regression testing, prompt improvers, and DSPy-style optimisation.

Choosing models

  • The Model Landscape — proprietary and open-weight families, model tiers and modalities.

  • Choosing a Model — a selection workflow, the build-vs-buy criteria table, and routing/cascade patterns.

  • Benchmarks & Leaderboards — what MMLU-Pro, GPQA, SWE-bench and friends measure, and how to read arena-style leaderboards.

  • Cost, Latency & Throughput — TTFT, TPOT, token pricing and batch APIs, and the trade-offs behind picking a smaller model or effort level.

  • AI Application Architecture — the end-to-end reference architecture these pieces form once wired together: context enhancement, guardrails, routing, caching, agent patterns, monitoring and the feedback loop.

Cheat sheet

  • Cheat Sheet (PDF) — a single-page, printable summary of every concept in this sub-section.

mindmap root((LLM Foundations & Prompting)) How LLMs work What Is an LLM? Tokens & Tokenization Transformer Architecture How LLMs Are Trained Sampling & Decoding Context Windows & Memory Reasoning Models Multimodal Models Hallucinations & Limitations Prompt and context engineering Prompt Engineering Fundamentals Prompting Techniques Structured Output & Tool Calling Context Engineering Managing Prompts Choosing models The Model Landscape Choosing a Model Benchmarks & Leaderboards Cost, Latency & Throughput AI Application Architecture Cheat sheet

Bibliography

The following sources were consulted while preparing this sub-section’s pages. Every code example was verified against the official documentation and current SDKs — never copied from a book — and where a book’s example was outdated, the page in question says so in prose and shows the current API.

Requester-provided books, each a consulted reference for concepts and the practitioner’s angle only:

oreilly.com answers automated fetches with an HTTP 403 response, but every publisher page above is a live, reachable page when opened in a browser.

Official documentation:

Leaderboards:

Engineering articles:

Specifications and standards:

Papers:

These books are consulted references, not the primary source of any example on this sub-section’s pages. On any discrepancy between a book and a tool’s own official documentation, that documentation is authoritative, and every code example was verified against the versions stated at the top of this page when the pages were written.