liveBook · Manning

Token

As explained in Build Python Web Apps with Streamlit

A token is a chunk of text, such as a word or word fragment, processed as an input or generated as an output by a language model.

When an app sends a prompt to a language model, the model reads it in tokens: chunks of text that may be whole words or parts of words. Its reply is made of output tokens. Both count when estimating API costs. section 9.6.2

A model builds a reply one token at a time, weighing possible next tokens. Temperature and top_p change how those possibilities are selected, affecting how predictable or varied the reply may be. section 9.6.1

Keep a trivia app affordable

A prompt that repeats every earlier question grows as the game continues. Keep prompts concise, ask for shorter replies, or generate several questions in one request. OpenAI’s tokenizer can help count tokens before you estimate costs. section 9.6.2

Tokens also measure a model’s context window: how much information can fit in a prompt. Sending an entire knowledge base with every question can be costly and may degrade results. Retrieval-augmented generation instead supplies relevant context. section 10.5.1

Cap a chatbot reply

Setting `max_tokens=2000` on `ChatOpenAI` limits a chatbot response to 2,000 tokens—about 1,500 words in the given example. This helps control response length and cost. section 10.2.3

Figure 9.10 How temperature and top_p affect the creativity and predictability of an LLM’s output.

Compare how temperature and top_p affect the predictability of the next token. section 9.6.1

How Build Python Web Apps with Streamlit covers it

How deep it goes
Defines input and output tokens and explains next-token probabilities, but does not describe how text is split into tokens.
Examples
Uses a multi-question trivia app, a token-counting tool, and a chatbot response cap.
What it assumes
Introduces tokens as words or word parts and explains next-token likelihoods using probabilities from zero to one.
What you can do after
Operate/configure token-related limits and estimate how prompt and reply lengths affect API costs.
How it connects
Connects tokens to temperature, top_p, context windows, and retrieval-augmented generation.

Also explained in

Compare how 31 books explain token →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.