Personal course notes, in public

Building agents from first principles

What a language model is, how you call one from Next.js, and how the agent loop works when your code runs in the middle. TypeScript, AI SDK v6 — every term defined on first use.

16 chapters · ~272 min · AI SDK v6

Start with chapter 1

The model

A language model predicts the next token. This chapter is how you talk to one — in TypeScript, with the AI SDK.

  1. 01What is an LLM?A large language model predicts the next chunk of text. It is not a database and it does not 'know' your app.Day 1 · ~18 min
  2. 02OpenAI APIs and the othersProviders expose HTTP APIs. You send JSON, they return a completion. You pay per token.Day 1 · ~14 min
  3. 03What the AI SDK is (and why)The Vercel AI SDK is a TypeScript library: generateText, streamText, tools, structured output — provider-agnostic.Day 1 · ~14 min
  4. 04Messages, roles, and a replyA chat is an array of messages. Roles tell the model who is speaking. The next assistant message is the response.Day 2 · ~14 min
  5. 05generateText — wait for the answerThe smallest useful AI SDK call: pick a model, send a prompt, read text.Day 2 · ~16 min
  6. 06Why streaming beats waitingUsers perceive speed when tokens arrive incrementally — even if total latency is similar.Day 3 · ~16 min
  7. 07streamText in a route handlerA minimal API route that streams chat completions to the client.Day 3 · ~16 min
  8. 08Structured outputs with ZodgenerateText with Output.object enforces a Zod schema so the model returns JSON you can trust in TypeScript.Day 4 · ~18 min
  9. 09Vectors and similarityEmbeddings turn text into numbers so you can find similar documents mathematically.Day 5 · ~18 min

The loop

An agent is that model, calling your code, until it can answer. You'll write the tools and the stop condition.

  1. 10What is an agent loop?An agent loop lets the model call tools repeatedly until it can produce a final answer.Day 1 · ~18 min
  2. 11The tool loop in a routeMatch the Tool Calling lab: streamText, Zod tool schemas, and stopWhen in one API route.Day 1 · ~16 min
  3. 12Tool schemas that models followThe model reads tool descriptions and Zod schemas to decide when and how to call your functions.Day 2 · ~16 min
  4. 13Controlling agent runsProduction agents need caps, approvals, and visibility into each step.Day 3 · ~16 min

Context

The model only sees what you send it. You'll decide what stays in the window when the chat gets long.

  1. 14Why context windows matterModels only see a finite amount of text — memory, cost, and quality all depend on how you manage it.Day 1 · ~16 min
  2. 15Sliding and token-based strategiesDrop oldest messages or trim by token count before each model call.Day 1 · ~14 min
  3. 16Summarization techniquesCompress older context instead of deleting it blindly.Day 2 · ~16 min

Coming next

  • RetrievalWhen the answer lives in your files, not in the conversation.
  • VoiceThe same loop, spoken.
  • EvaluationA way to tell if the agent is getting better.