Skip to content
groundworkv0.2

How LLMs work, for builders

Tokens, the context window, sampling and hallucination, the handful of facts that explain most surprising agent behavior.

10 min read · Last reviewed November 20, 2026

How LLMs work, for builders

You don't need the math. You need a mental model that predicts when a model will be sharp, when it will drift, and when it will confidently make something up.

You'll be able to#

  • Explain why an forgot an instruction, repeated itself, or invented an API
  • Estimate what fits in a prompt, and why "just paste the whole repo" backfires
  • Set the right expectations about repeatability: same prompt, different answer
  • Write prompts that make hallucinations less likely and easier to catch

The mental model#

Think of a very well-read colleague with a notepad of fixed size. They have read an enormous amount, they answer fluently whether or not they actually know, and they can only work from what is on the notepad right now: your messages, the files and tool results in the conversation, and the answer they are writing.

A useful simplification: the model produces its answer one small chunk (a ) at a time, each chunk chosen from what is most plausible given everything on the notepad so far. Plausible is not the same as true, which is why fluent output can still be wrong.

Key concepts#

  • Token: The unit a model reads and writes. It can be a whole word, part of a word, punctuation or a few characters. Providers measure limits and billing in tokens, and the number of tokens for the same text varies by model and by language. Use the provider's token-counting tool when you need an exact number.
  • : Everything the model can reference when it generates a response, including the response itself. It is working memory, not training data. It holds the system prompt, every message in the conversation, tool definitions and tool results, plus the output being written. On the , an input that is too large for the window fails with an error.
  • : Accuracy and recall can degrade as the amount of text in the window grows. A bigger window is not a license to fill it. What you leave out matters as much as what you put in.
  • Caching changes cost, not capacity: Reusing a cached prompt prefix can make requests cheaper and faster, but those tokens still take up space in the window.
  • (sampling): A setting that controls how much randomness goes into picking each token. Lower values give more conservative output, higher values more varied output. Even at the lowest setting, identical inputs are not guaranteed to produce identical outputs.
  • : Output that is factually wrong or inconsistent with the context you gave. It comes from the same mechanism as correct output, so the model sounds equally sure either way.
  • Grounding: Anchoring the answer in text you supplied (quotes, documents, the actual file) rather than in what the model remembers from training.

Decisions you make (not the agent)#

Your call, not the agent's

DecisionSensible defaultChoose otherwise when…
What goes in the promptThe goal, constraints, and paths to the relevant filesThe task truly spans many files: point to them by path and let the agent read what it needs
When to start a new sessionOne session per task; start fresh when the topic changes or the thread gets long and muddledA follow-up depends on decisions made earlier in the thread
How much to trust a factual claimVerify anything you will ship (run it, read the docs, check the package exists)The output is throwaway or easy to test immediately
Relying on repeatable outputDon't. Make correctness testable instead (tests, types, linters)You need identical output every time: store the result instead of regenerating it
Asking for quotes and sourcesYes, when the answer must come from documents you provideBrainstorming or exploring, where there is no ground truth to quote
Letting the model say "I don't know"Always allow it, explicitlyNever; a forced answer is a guess

What good output looks like#

  • It refers to things that actually exist in your repo and dependencies (real files, real function names, real versions)
  • When the answer depends on your docs, it quotes or points to the relevant passage
  • It states assumptions and says plainly when it lacks information instead of filling the gap
  • It stays consistent with the constraints you set at the start, even late in a long task

Red flags#

  • An import or function that isn't in the codebase → search for it, and run the type checker. Invented names are the classic hallucination.
  • A package you've never heard of, or one that isn't in package.json → check that it exists and is what you expect before installing anything.
  • Early instructions quietly ignored late in a long session → context has grown and muddied; summarize the decisions and start a fresh session.
  • "As we discussed" about something you never said → the model is filling gaps. Restate the facts.
  • Confident explanation with no evidence → ask for the exact lines, file paths or doc passages that support it.
  • Different answers to the same prompt → expected. Treat it as a reason to check with tests, not as a bug in the tool.

Worked example#

Task: Add to a Next.js API route that creates bookings.

You send a short prompt: "Add rate limiting to the bookings endpoint." The agent replies with a tidy diff that imports rateLimit from a package, calls it with an options object, and says the work is done. It reads well and it sounds sure.

What a good result contains: a limiter that uses a dependency already in the project, or one new dependency the agent named and justified; the limit chosen and why; behavior when the limit is hit (an HTTP 429 response); and a test.

The wrong turn: the package named in the import isn't in package.json, and the options object uses fields you can't find in any documentation. The model produced a plausible-looking call from patterns it has seen, not from the library you have.

How review catches it: run the type checker and the tests (the import fails or the types don't match). Then ask the agent to list every external import in the diff and where each one is defined. Open the real docs for the package you decide on, and give the agent the relevant section so the next attempt is grounded in text rather than recall.

Sample prompts#

Weak vs strong#

Strong prompt
Goal: add rate limiting to POST /api/bookings.
Stack: Next.js (App Router), TypeScript, Postgres via Drizzle.
Constraints: max 10 requests per minute per IP; respond with 429 and a Retry-After header when exceeded; keep existing response shapes; use only packages already in package.json. If you think a new package is needed, stop and propose it with the reason before installing.
Non-goals: no changes to the UI, no changes to other routes.
Verify: add a test that sends 11 requests and expects the 11th to return 429; run lint, typecheck and tests, and show me the output.

Why it works: It states the goal, limits, stack, boundaries and the proof. The "only packages already in package.json" rule turns an invented dependency into an explicit decision point.

What changed

  • Goal, stack and limits are stated
  • A dependency rule turns an invented package into an explicit decision
  • Non-goals keep the change inside one route
  • Verification names the test, the commands and the output to show

Ground an answer in your own docs#

Prompt
Using only the document at <path/to/doc>, answer: <question>.
First, extract the exact sentences that are relevant and list them. Then answer using only those sentences. If the document does not cover the question, say "not covered" instead of guessing.

Why it works: It gives the model permission to say it doesn't know, and makes it ground each claim in a quote, two techniques Anthropic's guidance recommends for reducing hallucinations.

Audit for invented names#

Prompt
Review this diff for anything that might not exist. List every import, function call, config key and CLI flag it introduces. For each one, tell me where it is defined (file and line in this repo, or the dependency and version in package.json). Mark anything you cannot point to as "unverified".

Why it works: It turns a vague worry into a checklist the agent can complete and you can spot-check.

Ask it to show its assumptions#

Prompt
Before I accept this, list the assumptions you made that I did not tell you, and the parts of your answer you are least sure about. For each, say what I could run or read to confirm it.

Why it works: It surfaces gaps and gives you a verification step. Treat the answer as a lead to check, not as proof, since the model can be wrong about its own certainty.

Hand off to a fresh session#

Prompt
Summarize this session so I can start a new one: the goal, decisions made and why, files changed, what is done, what is left, and any open questions. Keep it under 15 bullets.

Why it works: A short, curated summary carries forward what matters without dragging along a long, noisy context.

Check your understanding#

Check your understanding

1/3Which of these count toward the context window?(select all that apply)
2/3You set temperature to its lowest setting. What can you rely on?
3/3An answer must come only from your documents. Which prompt change helps most?

Checklist#

  • Every import, function and package in the diff exists (type check and tests pass)
  • Claims that matter are backed by a doc, a quote or a test
  • The prompt allowed the model to say "I don't know"
  • Long session? Summarized the decisions and started fresh if quality dropped
  • Correctness is checked by tests, not by regenerating until it looks right

Go deeper#