Artificial Intelligence
for Engineers

Transformers, LLMs, RAG and agents: 384 pages that take language models apart, from arithmetic worked out by hand to systems in production.

Buy on Amazon.com English edition. Also published in Italian.

Attention on “the cat sleeps” Chapter 5, page 90. Nine dot products and three softmaxes.

Inside the answer.

Between the prompt and the answer: dot products, one softmax, billions of parameters. The book shows them one by one.

Attention, by hand.

Chapter 5 runs the formula on a three-word sentence and hides no digit: nine dot products, the scaling by the square root of two, three softmaxes. Pick a word and watch where its attention goes.

softmax(QKT / √dk) V

Attention weights α, chapter 5, page 90. Every row sums to 1.
Word doing the lookingthecatsleeps
the0.30570.62000.0743
cat0.40110.40110.1978
sleeps0.28400.57600.1400

“sleeps” enters the layer as (0; 1) and leaves as (0.8600; 0.4240): 58 % of it is the content of “cat”, 28 % that of “the”, 14 % its own. It is no longer the verb in the abstract, it is “to sleep, in a sentence whose subject is a cat”.

Two more instruments

The same chapter that runs attention by hand then hands over the whole architecture as a stack of stages, and that stack written in PyTorch.

The architecture, block by block

The reference model is decoder-only, and the chapter walks it from the bottom up, one station at a time. The whole stack is this:

  1. token embeddings
  2. position embeddings
  3. N identical blocks: causal attention, feed-forward, layer norm, residual connections
  4. a final layer norm
  5. a linear projection onto the vocabulary
  6. softmax: the probabilities of the next token

GPT-2 small's numbers show what each stage weighs. The position embeddings are learned, one row per position, and weight tying makes the embedding matrix and the final projection the same tensor: it removes 50 257 × 768 ≈ 38.6 million parameters, nearly a third of the 124 million total. The last stage also gives the first sanity check: a freshly initialised model has to start from a loss of ln 50 257 ≈ 10.82, and if the first training step reports anything else, the bug is in the code.

Chapter 6, page 102.

A mini-GPT, line by line

The chapter closes by writing a complete mini-GPT in PyTorch, read line by line, with weight tying settled in a single line. The block is the heart of it, and the two lines of the forward say everything: the sum is the residual connection, the layer norms sit before the sublayers, and the work is split in two, attention is communication between tokens, the feed-forward is per-token processing. They are the same two lines you find in Llama or GPT-2 under different names. In the public repository the file is mini_gpt.py, in the chapter 6 directory.

class Block(nn.Module):
    def __init__(self, cfg):
        super().__init__()
        self.ln1 = nn.LayerNorm(cfg.n_embd)
        self.att = CausalSelfAttention(cfg)
        self.ln2 = nn.LayerNorm(cfg.n_embd)
        self.mlp = MLP(cfg)

    def forward(self, x):
        x = x + self.att(self.ln1(x))   # communication between tokens
        x = x + self.mlp(self.ln2(x))   # per-token processing
        return x

Chapter 6, page 120.

The English edition

Artificial Intelligence for Engineers

Transformers, LLMs, RAG, and AI Agents: from foundations to code

384 pages, seventeen chapters, four parts.

The same book exists in Italian, the original edition: Intelligenza artificiale per ingegneri.

The method.

Every hard idea arrives three times: worked out by hand with all the numbers in view, rewritten as code that runs, and taken into production, where cost, latency, security and the law are what decide.

Page 89 of the book: the scaling of the scores and the three softmaxes computed in full.

By hand

A thirteen-parameter network trained on paper, attention run on three tokens down to the last digit.

Page 121 of the book: the PyTorch listing of the mini-GPT, part four.

In code

The same numbers, rewritten in Python: 127 short, commented listings, all in the book's public repository.

Page 319 of the book: the security perimeter of an LLM application, with gateway, logs and model providers.

In production

Cost, latency, the threat model, GDPR and the AI Act, observability: the questions that arrive the day after the demo.

The fullcontents

Part one. Foundations: understanding what is inside

The Birth and Development of Artificial Intelligence

From Turing and the perceptron to the Transformer era, by way of the AI winter.

p. 2

Mathematical Foundations

Vectors, matrices, derivatives, probability, entropy and cross-entropy: only what is really needed.

p. 18

Neural Networks

A thirteen-parameter network, with the forward pass and backpropagation worked out number by number.

p. 39

Representing language: tokens and embeddings

How text becomes numbers, and why tokenization decides part of the final bill.

p. 59

The attention mechanism

Queries, keys and values, the formula run by hand, multi-head and causal attention.

p. 81

The Transformer Architecture

Blocks, positions, layer norm, weight tying and a complete mini-GPT written line by line.

p. 100

From the Transformer to the LLM

Pretraining, instruction tuning, alignment, LoRA, quantization, open models on a laptop.

p. 126

Part two. Building with LLMs

Using LLMs via API

Parameters, streaming, token counting, cost, and a client written the way it should be.

p. 147

Prompt and context engineering

Context handled by engineers: structure, examples, constraints, measurement, instead of guesswork.

p. 170

Semantic Search and Vector Databases

Embeddings, indexes, HNSW, chunking and retrieval that holds up over a real archive.

p. 191

Retrieval-Augmented Generation

Answering with citations, and saying so when the archive has nothing to say.

p. 211

Part three. Agents and multi-agent systems

Agents

Tools, reasoning loops, actions with consequences, and the human in the right place.

p. 235

The Model Context Protocol

Primitives, channels, an MCP server built from scratch and the security it demands.

p. 260

Multi-agent systems

Topologies, coordination, cost, and when a single agent is still the better answer.

p. 283

Part four. In production

LLMs in production

Architecture, a latency budget, routing by complexity, threat model, cloud, GDPR and the AI Act.

p. 306

Observability and evaluation

Tracing, metrics, eval sets, regression testing and monitoring: measuring instead of hoping.

p. 327

Case study: a complete enterprise assistant

Requirements, architecture, evaluation, deployment with the sums done, and an honest retrospective.

p. 348

Bibliography and References

Thirty-nine sources grouped by theme, from the original papers to the documentation of the standards.

p. 368

Four parts, seventeen chapters.

Browse the code on GitHub

Who wrote it

Portrait of Luciano Salemme, the author of the book.

Luciano Salemme has been building distributed systems for more than twenty years and is Chief Technology Officer of an AdTech platform. He writes from Rome, and has two technical books to his name.

Follow the author on Amazon to hear when the next book is out.

Common
questions

What do I need to know to read it?

Basic Python and high-school algebra. All the mathematics the book uses, vectors, matrices, derivatives, probability and entropy, is introduced in chapter 2 and picked up again wherever it is needed, with nothing taken for granted.

Is it a theory book or a practical one?

Both, in the same order in every chapter: first the mechanism with a numerical example worked out by hand, then the same example in Python, which has to return the same digits. In total, 127 listings, short and commented.

Is the code available?

Yes, in a public GitHub repository, organised by chapter, with installation instructions in the README.

What exactly does it cover?

Four parts: the foundations (mathematics, neural networks, tokens and embeddings, attention, the Transformer, from the Transformer to the LLM), building (APIs, prompt and context engineering, semantic search, RAG), agents (agents, the Model Context Protocol, multi-agent systems) and production (cost, security, regulation, observability, a full case study).

Is there an Italian edition?

Yes: Intelligenza artificiale per ingegneri is the original Italian edition, on Amazon.it. The content is the same, including the code; the reader picks the language.

Is it useful if I already use ChatGPT or the APIs?

It is written for readers already past the first prompt: why the model answered that way, what it will cost in six months, what happens when it is wrong, and how to measure a system that has no single right answer.

Does it cover cost, security and regulation?

Yes, that is the fourth part. Chapter 15 covers architecture, a latency budget, routing by complexity, the threat model, cloud deployment, GDPR and the AI Act; chapter 16 is about tracing, metrics, eval sets and monitoring.

How long is it and in what format?

384 pages, 7 by 10 inches, in paperback on Amazon.com.

Can I see some of it before buying?

Every code example from every chapter is public on GitHub, and the first chapter, with the preface and the full table of contents, comes by email when you subscribe to the newsletter from the form at the bottom of this page.

The first chapter, by email.

Subscribe to the newsletter and get an excerpt with the first chapter: The Birth and Development of Artificial Intelligence, with the preface and the full table of contents, laid out as in the book. After that, only what is new: new editions and new books. Unsubscribe with one click, whenever you like.

You will get a confirmation email: the subscription only starts after your click.

Open the box.

The fastest way to stop using these systems blind is to look inside them. The book is on Amazon.com.

The Italian edition, Intelligenza artificiale per ingegneri, is on Amazon.it. Same book, same code.

Luciano Salemme, the author of the book. Luciano Salemme
Buy on Amazon.com Buy on Amazon.it