Skip to main content
Quick Reference

Gen AI Cheatsheet

Generative models on one page — tokens, prompts, sampling, retrieval, and the traps around context, grounding, and finish reason.

10 sections 10 tables 14 code snippets

Next token

A chat turn

{"role": "system", "content": "Answer from the notes."}
{"role": "user", "content": "When was the lab built?"}

A sharper draw

temperature = 0.2

What a generator does

PieceMeaning
Next tokenThe model scores the vocabulary, then one token is chosen and appended
LogitsRaw scores before softmax. Temperature and penalties edit those scores
WeightsSampling changes this draw. The trained weights stay as they are
ChatA list of messages. The reply is new assistant tokens
WindowThe prompt plus the tokens still to generate must fit that model context
ProviderNames and defaults differ. Some reasoning models reject the sampling knobs

This page is generative models. Training a network from scratch and classical machine learning are left out.

Tokens

Count pieces, not words

# one word can be several tokens

The unit that is billed

UnitWhat to count
TokenThe piece the model reads. A word, a space, or part of a word
WordsA rough guess. The window and the invoice use tokens
TokenizerTwo models can split the same sentence into different counts
ContextA token budget shared by the prompt and the reply
OverflowA prompt past the window is rejected. The API does not trim it for you
Usageprompt_tokens and completion_tokens are the counts on the bill

Messages

Few-shot in the same list

# an assistant example, then the real user turn

Who said it

RoleJob
systemStanding instructions. On o1 and newer, a developer message has this job
userThe question, or the data, for this turn
assistantAn earlier reply, or the answer half of a few-shot example
Few-shotExample user and assistant turns in the same list, before the real question
ConflictThe model sees the whole list. A later message can pull against an earlier one
Data blockRetrieved pages are data. An instruction inside a page is still data

Sampling

Temperature rescales logits

# softmax(logits / temperature)

Nucleus cutoff

top_p = 0.9

How one token is chosen

KnobEffect
temperature 1Softmax of the logits, with the learned shape left as it is
Below 1Sharper. The top token keeps its rank and takes more of the mass
Above 1Flatter. Weaker tokens get a better chance. OpenAI allows up to 2
temperature 0API shorthand for greedy, the argmax. The formula cannot divide by zero
top_pKeep the smallest high-probability set whose mass reaches p, then renormalize. 1 turns the filter off
One knobOpenAI recommends changing temperature or top_p. Some reasoning models reject both

The window

Room left for the answer

# a long prompt leaves fewer tokens to generate

Where the fact sits

LimitMeaning
WindowA model-specific cap on prompt tokens plus generated tokens
Answer roomInstructions and retrieved notes spend the same budget as the reply
max_completion_tokensCaps generated tokens, including reasoning tokens on models that emit them
max_tokensThe older cap. Deprecated, and o-series models reject it
MiddleLiu et al. saw lower accuracy when the needed passage sat in the middle of a long prompt
EndsIn that study, accuracy was higher with the passage at the start or the end

Retrieval

Paste, then generate

# retrieve passages, then put them in the prompt

What retrieval changes

MethodWhat moves
Closed bookThe model answers from its weights only
RAGA retriever picks passages. They go into the prompt. The weights stay put
Missed passageThe generator has no source for a fact that retrieval never returned
IgnoreThe model can skip the passages and answer from memory anyway
EmbeddingsVector search ranks text. A chat model writes the reply
CheckAsk for a span from the notes, then compare that span to the source

What you can change

A prompt is this call only

# prompting leaves the weights unchanged

Prompt, penalty, or weights

ChangeEffect
PromptAffects this request. The trained weights stay as they are
Fine-tuneUpdates weights on your examples. It does not attach a live database
Which oneFacts that change weekly belong in retrieval. A stable style can be a fine-tune
frequency_penaltyPositive values down-weight tokens by how often they already appeared. Range -2 to 2
presence_penaltyPositive values down-weight any token that has appeared at all, favoring new topics
seedSame seed and parameters are a best effort. Determinism is not guaranteed

How it stops

A schema, when the model supports it

{"type": "json_schema"}

Read the stop reason

# length means the cap cut the reply

Finish and format

SignalMeaning
stopA natural end, or a stop sequence. The stop text is left out of the reply
lengthHit max_completion_tokens or the window. The sentence can end mid-way
tool_callsThe model asked for a tool. Your code has not run that tool yet
content_filterThe provider omitted text
json_objectValid JSON only when the prompt also asks for JSON
json_schemaStricter on models that support it. A length cutoff can still leave broken JSON

The bill

Add both sides

# total_tokens = prompt_tokens + completion_tokens

What you pay for

LineWhy
prompt_tokensInstructions, the question, and every retrieved note you sent
completion_tokensTokens the model generated. Reasoning tokens count on models that report them
total_tokensPrompt plus completion
n choicesEach extra reply is more generated tokens. Some newer models reject n
RetryA second call is billed again. A seed does not make the repeat free
CacheSome APIs charge less for a repeated prompt prefix. The reply is still generated

Interview traps

Greedy is still one build

# temperature 0 is argmax, and seed is best effort

Validate before you run it

# tool arguments can be invalid JSON

Traps worth memorising

TrapWhat happensPrefer
temperature 0 means identical textGreedy still depends on the model build. seed does not promise a matchCompare system_fingerprint
Budget in wordsThe window and the bill are tokens, and tokenizers disagreeCount tokens for that model
RAG edits the weightsOnly the prompt changed for this callPut new facts in retrieval
A fluent answer is sourcedFluency is a style. The claim can still be inventedMatch the claim to the notes
Run tool JSON immediatelyArguments can be invalid or inventedValidate, then call the tool
Page text in the instruction slotA retrieved page can contain directions for the modelKeep sources in a data block

How to use this page: sample the next token from logits, keep instructions separate from retrieved text, and read finish_reason before you trust the reply.