---
title: "AI foundations | SurrealDB University"
description: "How LLMs work - tokens, context windows, why they hallucinate - and the difference between prompt engineering and context engineering."
url: https://surrealdb.com/learn/ai/ai-foundations
---

![Course content preview](https://surrealdb.com/assets/static/course-ai.LEsq_J_G.avif)

Course chapters

[Back to courses](https://surrealdb.com/learn) [SurrealDB for AI Engineers](https://surrealdb.com/learn/ai) [AI foundations](https://surrealdb.com/learn/ai/ai-foundations) [Vector embeddings and search](https://surrealdb.com/learn/ai/vector-embeddings) [Full-text search and BM25](https://surrealdb.com/learn/ai/fulltext-search-bm25) [Building a RAG knowledge base](https://surrealdb.com/learn/ai/rag-knowledge-base) [Hybrid search and reranking](https://surrealdb.com/learn/ai/hybrid-search-reranking) [Building an agent memory store](https://surrealdb.com/learn/ai/agent-memory-store) [Text-to-SurQL: agentic prompt engineering](https://surrealdb.com/learn/ai/text-to-surrealql) [Many sources and agents, one context layer](https://surrealdb.com/learn/ai/multi-source-context-layer) [Chunking strategies](https://surrealdb.com/learn/ai/chunking-strategies) [Evaluating retrieval quality](https://surrealdb.com/learn/ai/evaluating-retrieval) [Graph RAG beyond one hop](https://surrealdb.com/learn/ai/graph-rag-multi-hop) Certificate Pending

# AI foundations

This first lesson is the background the rest of the course uses: how a language model works, and the difference between writing a prompt and choosing what goes into the window. The reading is all external. Lessons 02 to 08 assume you've seen these ideas.

The later lessons use the usual terms - context window, context rot, attention budget, the agent loop - and put a database underneath them. Each link below is here because a later lesson needs it, and the note says which one.

## How LLMs work

A language model does one thing: given a sequence of tokens, predict the next one.

Everything else (answering questions, writing code, holding a conversation) is that operation in a loop, each prediction fed back in.

Two things follow from that, and both show up for the rest of the course:

1. \1.

   The **context window** is the model's entire world at inference time: no memory between calls, no access to your data, so a fact that isn't in the window doesn't exist.
2. \2.

   Nothing in *predict a plausible next token* checks whether the continuation is true - hallucination is what the mechanism does when the window doesn't hold the answer. The fix is to put the answer there.

| Resource | What it covers |
| --- | --- |
| [**Transformers, the tech behind LLMs**](https://www.youtube.com/watch?v=wjZofJX0v4M) 3Blue1Brown, 27 minutes | Grant Sanderson builds a transformer up one piece at a time, animating what an embedding, an attention head and a softmax are each doing to the numbers. If you watch only one thing here, watch this one: lesson 02's talk of vectors and closeness in meaning gets a lot less abstract once you have seen the geometry move. |
| [**Intro to Large Language Models**](https://www.youtube.com/watch?v=zjkBMFhNj_g) Andrej Karpathy, one hour | Pretraining, fine-tuning and where capabilities actually come from, at a brisk pace. Karpathy is blunt about what models can't do, which most intros skip. He also draws a line this course sticks to: capability comes from training, while knowledge of your data has to arrive at inference time. That's why nothing from lesson 02 onwards fine-tunes a model. |
| [**The Illustrated Transformer**](https://jalammar.github.io/illustrated-transformer/) Jay Alammar | The diagram set that everyone else borrows from. It covers similar ground to the 3Blue1Brown video but on the page, which is easier when you want to stop on one step and stare at it. Its walk through how a token becomes a vector is the piece lesson 02 builds on most directly. |
| [**Tiktokenizer**](https://tiktokenizer.vercel.app/) Tat Dat Duong | A tool rather than a reading. Paste in any text and watch it break into tokens. Two minutes here makes the context window concrete. Notice how little a bare error code or an order number gives a model to work with: those are the queries lesson 03 is for. |

![The Tiktokenizer web app tokenising four lines of text, each twenty characters long, with the coloured token blocks growing more numerous down the four lines and a total token count of 29](https://surrealdb.com/assets/static/tiktokenizer.OvGrjSVC.avif)

*Four lines, twenty characters each, run through gpt-4o. The token count climbs with unfamiliarity rather than with length: `I like cats and dogs` costs 5, the same sentence with `SurrealDB` in it costs 6, an error code and an order number cost 7, and an invented word costs 8. Every step down that list leaves a model less to work with, which is the blind spot [lesson 03](https://surrealdb.com/learn/ai/fulltext-search-bm25) picks up. Screenshot of [Tiktokenizer](https://tiktokenizer.vercel.app/), built by [Tat Dat Duong](https://github.com/dqbd/tiktokenizer) and MIT licensed.*

## Prompt engineering and context engineering

**Prompt engineering** is the wording of the instruction: role, format, examples. Write it once and reuse it.

**Context engineering** is deciding what information occupies the finite window on each request, and where it comes from. You solve that at runtime, on every call, against data that changes. It's a retrieval problem, which makes it a database problem.

Lessons 04 to 07 are all context engineering: retrieving the right documents, fusing two rankings, recalling what the agent learned last week, and generating the prompt itself out of live schema so it can't go stale.

> **When the data keeps changing.** A store that only ever grows ends up holding the old version of a fact next to the new one, and a retriever with no idea of time will hand a model both. SurrealDB's [Agent Memory](https://surrealdb.com/agent-memory) is built around that: it keeps what was said, what is true now, and what used to be true as three separate things, so a superseded fact can be closed rather than left to compete with its replacement. [Lesson 06](https://surrealdb.com/learn/ai/agent-memory-store) builds a small version of the same idea by hand, with an expiry field and a write-back on recall.

| Resource | What it covers |
| --- | --- |
| [**Effective context engineering for AI agents**](https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents) Anthropic, September 2025 | The distinction set out in full by the people who popularised the term. The *context rot* section is the useful bit: past a certain size, adding tokens to the window measurably degrades the model's recall, so you want less in the window, not more. It also covers compaction, structured note-taking and sub-agent architectures as three ways of living within an attention budget. Lesson 04 turns that into a `WHERE` clause: weak matches never reach the prompt. |
| [**Prompt engineering overview**](https://platform.claude.com/docs/en/docs/build-with-claude/prompt-engineering/overview) Anthropic | The practical checklist: be explicit, give examples, let the model think, assign it a role. Skim it once now and come back to it when a prompt misbehaves. It works better as a reference than as a read. Lesson 07 uses this checklist almost line by line, except the database assembles the prompt rather than a person. |
| [**Building effective AI agents**](https://www.anthropic.com/engineering/building-effective-agents) Anthropic, December 2024 | Where retrieval sits inside an agent loop, and the argument for simple composable patterns over frameworks. The post now opens with a warning that its tooling advice has dated; the patterns have held up rather better than the tools around them. The agent loop it describes is the one lesson 06 writes back into, and the multi-agent shape it sketches is what lesson 08 takes apart. |
| [**LLM powered autonomous agents**](https://lilianweng.github.io/posts/2023-06-23-agent/) Lilian Weng, about half an hour's read | A survey, organised as planning, memory and tool use. The memory section is the bit lesson 06 follows most closely, and its detour through maximum inner product search is the same retrieval problem that lesson 02 hands to an HNSW index. |

## Where this goes next

The window is finite, and nobody gets to choose what the user asks. So the question is: given a question you never anticipated, what goes in the window?

There are two ways to find that content, by meaning and by literal term. [Lesson 02](https://surrealdb.com/learn/ai/vector-embeddings) covers the first, [lesson 03](https://surrealdb.com/learn/ai/fulltext-search-bm25) the second, and every later lesson leans on one or both.

At a glance

**You are on**

Chapter 1 of 11

**Chapters**

11

**Format**

Text, with queries you can run

**Runs in**

SurrealDB Studio, in the browser

**Cost**

Free

**Certificate**

On completion

[Next: Vector embeddings and search](https://surrealdb.com/learn/ai/vector-embeddings)

## Continue

### [SurrealDB for AI Engineers](https://surrealdb.com/learn/ai)

Previous

### [Vector embeddings and search](https://surrealdb.com/learn/ai/vector-embeddings)

Next lesson

THE PLATFORM

## Everything an application and its agents know. Five surfaces, one engine.

Database

Document, graph, vector, time-series and relational in one engine.

![Five data models as dotted tiles: documents, graph, vector, time-series and relational](https://surrealdb.com/assets/static/platform-database.DUdumYDz.avif)

Read more

[Database](https://surrealdb.com/surrealdb)

Agent Memory

What an agent learns, with its source and its time, in the same engine.

![A timeline of remembered facts, each with its source](https://surrealdb.com/assets/static/platform-agent-memory.B4RNjvbX.avif)

Read more

[Agent Memory](https://surrealdb.com/agent-memory)

Cloud

Managed clusters in the regions you choose, scaled on demand.

![Clusters in three regions on a world map, each running or scaling](https://surrealdb.com/assets/static/platform-cloud.--QZnaVi.avif)

Read more

[Cloud](https://surrealdb.com/cloud)

Studio

Query, explore and design the schema from the browser.

![A SurrealQL query in Studio and the schema graph under it](https://surrealdb.com/assets/static/platform-studio.7ykBFNLq.avif)

Read more

[Studio](https://surrealdb.com/studio)

MCP

Every model that speaks MCP reaches the database and the memory directly.

![Three models connected through MCP to the database and Agent Memory](https://surrealdb.com/assets/static/platform-mcp.D_oH0_wm.avif)

Read more

[MCP](https://surrealdb.com/mcp)

IN PRODUCTION

## Trusted at scale. Samsung, Nvidia, Verizon, Tencent, and Walmart run on SurrealDB.

14,000+

Developers building on SurrealDB Cloud

4M+

Developers building on SurrealDB worldwide

FROM THE TEAMS

> SurrealDB gives us a foundation where we can unify semantic search, knowledge graphs, and AI-driven decision making without stitching together multiple systems. Collapsing responsibility into SurrealDB has become our default engineering posture.

*Justin Foley*

VP of Engineering, Later

```json
{"@context":"https://schema.org","@type":"Course","name":"SurrealDB for AI Engineers","description":"An eleven-lesson course from calling an LLM API to an agent that retrieves by meaning, remembers what it learned, writes its own queries, and can prove its retrieval works - all against one database.","url":"https://surrealdb.com/learn/ai","inLanguage":"en","isAccessibleForFree":false,"provider":{"@type":"Organization","name":"SurrealDB","url":"https://surrealdb.com"},"hasPart":[{"@type":"LearningResource","name":"SurrealDB for AI Engineers","url":"https://surrealdb.com/learn/ai"},{"@type":"LearningResource","name":"AI foundations","url":"https://surrealdb.com/learn/ai/ai-foundations"},{"@type":"LearningResource","name":"Vector embeddings and search","url":"https://surrealdb.com/learn/ai/vector-embeddings"},{"@type":"LearningResource","name":"Full-text search and BM25","url":"https://surrealdb.com/learn/ai/fulltext-search-bm25"},{"@type":"LearningResource","name":"Building a RAG knowledge base","url":"https://surrealdb.com/learn/ai/rag-knowledge-base"},{"@type":"LearningResource","name":"Hybrid search and reranking","url":"https://surrealdb.com/learn/ai/hybrid-search-reranking"},{"@type":"LearningResource","name":"Building an agent memory store","url":"https://surrealdb.com/learn/ai/agent-memory-store"},{"@type":"LearningResource","name":"Text-to-SurQL: agentic prompt engineering","url":"https://surrealdb.com/learn/ai/text-to-surrealql"},{"@type":"LearningResource","name":"Many sources and agents, one context layer","url":"https://surrealdb.com/learn/ai/multi-source-context-layer"},{"@type":"LearningResource","name":"Chunking strategies","url":"https://surrealdb.com/learn/ai/chunking-strategies"},{"@type":"LearningResource","name":"Evaluating retrieval quality","url":"https://surrealdb.com/learn/ai/evaluating-retrieval"},{"@type":"LearningResource","name":"Graph RAG beyond one hop","url":"https://surrealdb.com/learn/ai/graph-rag-multi-hop"}]}
```

```json
{"@context":"https://schema.org","@type":"LearningResource","name":"AI foundations","description":"How LLMs work - tokens, context windows, why they hallucinate - and the difference between prompt engineering and context engineering.","url":"https://surrealdb.com/learn/ai/ai-foundations","learningResourceType":"lesson","isPartOf":{"@type":"Course","name":"SurrealDB for AI Engineers","url":"https://surrealdb.com/learn/ai"},"position":2}
```

```json
{"@context":"https://schema.org","@type":"Organization","@id":"https://surrealdb.com/#organization","name":"SurrealDB","url":"https://surrealdb.com","logo":"https://surrealdb.com/assets/static/logo.BG7_TG2b.svg","description":"SurrealDB is the context and memory layer for AI agents. A multi-model database for documents, graphs, vectors, and time-series.","foundingDate":"2022","legalName":"SurrealDB Ltd","identifier":{"@type":"PropertyValue","propertyID":"GB-COH","value":"13615201"},"address":{"@type":"PostalAddress","streetAddress":"3rd Floor, 1 Ashley Road","addressLocality":"Altrincham","addressRegion":"Cheshire","postalCode":"WA14 2DT","addressCountry":"GB"},"contactPoint":[{"@type":"ContactPoint","contactType":"customer support","email":"support@surrealdb.com","url":"https://surrealdb.com/contact","availableLanguage":"English"},{"@type":"ContactPoint","contactType":"sales","email":"info@surrealdb.com","url":"https://surrealdb.com/contact","availableLanguage":"English"},{"@type":"ContactPoint","contactType":"security","email":"security@surrealdb.com","url":"https://surrealdb.com/.well-known/security.txt","availableLanguage":"English"},{"@type":"ContactPoint","contactType":"legal","email":"legal@surrealdb.com","url":"https://surrealdb.com/legal","availableLanguage":"English"}],"hasCertification":[{"@type":"Certification","name":"SOC 2 Type 2"},{"@type":"Certification","name":"GDPR"},{"@type":"Certification","name":"Cyber Essentials Plus"},{"@type":"Certification","name":"ISO 27001"}],"owns":[{"@type":"SoftwareApplication","name":"SurrealDB","url":"https://surrealdb.com/surrealdb"},{"@type":"SoftwareApplication","name":"Agent Memory","url":"https://surrealdb.com/agent-memory"}],"knowsAbout":["multi-model databases","document databases","graph databases","vector search","time-series databases","SurrealQL","Agent Memory","real-time databases","embedded databases","context layer","graph ontology","distributed database","knowledge graphs","distributed transaction protocols","highly-scalable databases"],"sameAs":["https://www.wikidata.org/wiki/Q124316308","https://github.com/surrealdb/surrealdb","https://twitter.com/surrealdb","https://www.youtube.com/@surrealdb","https://www.linkedin.com/company/surrealdb","https://discord.gg/surrealdb","https://www.reddit.com/r/surrealdb","https://www.instagram.com/surrealdb","https://medium.com/surrealdb","https://dev.to/surrealdb"]}
```

```json
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://surrealdb.com"},{"@type":"ListItem","position":2,"name":"Learn","item":"https://surrealdb.com/learn"},{"@type":"ListItem","position":3,"name":"Ai foundations","item":"https://surrealdb.com/learn/ai/ai-foundations"}]}
```
