Large Language Models Explained: Tokens, Context, Prompts, and Tools
This article breaks down the core mechanics of large language models (LLMs) for beginners and developers. It explains why treating an LLM as a "thinking program" leads to misplaced trust, and reframes its behavior as controlled probabilistic text continuation. You will learn what tokens are, how tokenization affects cost, latency, and truncation, and how to design around a token budget. The article then covers context layering, prompt structuring with verifiable constraints, and tool calling as the boundary of model action, closing with a four-stage learning path from concepts to engineering.
1. Fixing Misconceptions Before Learning LLMs
Many beginners treat a large language model as a “program that thinks.” The intuition is understandable, but it creates two problems: over-trusting the output, and being unable to explain why the model makes mistakes. A more rigorous starting point is to acknowledge that a Large Language Model (LLM) is typically a language model trained on architectures such as the Transformer, and that its underlying mechanism is sequence modeling and probabilistic generation.
Its core behavior can be abstracted as “controlled text continuation”: given the existing context, the model produces a probability distribution over the most likely next token, samples or decodes one token, appends it back to the context, and predicts the next one. This loop looks simple, yet it explains most of the capability boundaries and engineering constraints of LLMs.
Four important conclusions follow:
- Output is fundamentally probabilistic prediction, so “looks reasonable” does not equal “factually correct.”
- Generation unfolds token by token, so latency, throughput, and cost are all strongly tied to tokens.
- The model has no reliable long-term memory by default; cross-session information must be supplied by external storage and retrieval.
- The model does not execute external actions on its own; it needs context, prompts, and tools to form a complete application.
2. Tokens: The Basic Unit of Text Processing
A token is the smallest basic unit an LLM uses to process text. It is not equal to “a character” or “a word,” but a fragment produced by a tokenizer according to specific rules — possibly a word, a subword, a punctuation mark, or even part of a common word. Token counts vary widely across languages and domains.
At minimum, three points about tokens matter:
- Measurement and cost: call volume, context occupancy, and output length are usually measured in tokens.
- Truncation and information loss: content beyond the context window is truncated, so the model “cannot see” the earlier text.
- Generation granularity: the model emits one token at a time, so long text requires multiple iterations.
An intuitive example (illustrative only, independent of any specific implementation):
Source: 大模型学习需要掌握知识Possible tokenization: ["大", "模型", "学习", "需要", "掌握", "知识"]
In practice you do not need to write a tokenizer yourself, but you must design with a token mindset: controlling input length, compressing prompts, chunked retrieval, and splitting long documents before recall all revolve around a token budget.
3. Context: The Model’s Working Surface
Since the model has no natural long-term memory, application-level memory must be constructed explicitly. Context typically includes system instructions, the user question, retrieved material, summaries of prior conversation, structured data, and tool results.
A practical approach is to split context into three layers:
- Stable layer: role, rules, output format constraints (keep it as fixed as possible for caching and reuse).
- Dynamic layer: the current user input, retrieved chunks, tool results (changes frequently, length must be controlled).
- Compression layer: long-conversation summaries, key-point lists, entity tables (retain critical information with fewer tokens).
Simple pseudocode:
context = stable_prompt + "\n" + retrieved_chunks + "\n" + summary + "\n" + user_inputanswer = llm_generate(context)
Note that retrieval is not “the more the better.” Irrelevant chunks dilute key information and increase the chance of confusion. A more reliable strategy is to recall first, rerank second, and finally trim to the token budget.
4. Prompts: State the Task Clearly and the Constraints Explicitly
A prompt is the interface through which the model receives a task. A high-quality prompt usually contains four elements: goal, input, constraints, and output format. The more verifiable the constraints, the more stable the results. Requiring “output JSON only,” “answer unknown when uncertain,” or “cite the given material by number” all significantly reduce free-form improvisation.
Example (structured output constraint):
You must answer only based on the given material.Output must be JSON with fields: answer, evidence[], confidence(0-1).If the material is insufficient, set answer to "unknown" and evidence to an empty array.
An advanced technique is a structured alternative to “step-by-step thinking”: rather than requiring the model to expose its reasoning, require it to list evidence first, then make a judgment, then give a conclusion — which makes auditing and evaluation easier.
5. Tools: From “Talking” to “Doing”
The model does not perform external operations directly. To query, compute, write to a database, or send a message, you need a tool-calling mechanism: the model emits a structured call intent, the application executes it and writes the result back into the context, and the model continues generating.
Tool design recommendations:
- Write tool descriptions like API documentation: purpose, input fields, output fields, failure semantics.
- Least privilege: prefer read-only; require confirmation for writes.
- Trimmable results: summarize long tool output before adding it to the context.
- Observability: log the call chain, latency, and error codes for troubleshooting.
Illustrative flow:
User asks -> model decides a tool is needed -> emits tool call (JSON) -> application executes -> result written back to context -> model produces the final answer
6. Learning Path: Four Stages from Concepts to Engineering
- Foundations: Transformer intuition, tokens and tokenizers, probabilistic generation vs. decoding differences.
- Context engineering: context layering, retrieval chunking, summary compression, token budgeting.
- Prompting and evaluation: prompt structuring, output constraints, building a small evaluation set.
- Tools and systems: tool calling, permissions and security, logging, monitoring and alerting, cost control.
In enterprise deployments, common supporting components include object storage for corpora, message queues for asynchronous tasks, container platforms hosting inference services, and logging plus monitoring and alerting for stability. These are general capability categories, which keeps the design portable across platforms.
7. Conclusion
The key to learning LLMs is not memorizing conclusions first, but building an explainable mental model: tokens as the unit of measurement, probabilistic prediction as the behavioral essence, context as the memory surface, prompts as the task interface, and tools as the boundary of action. Once these five things are clear, moving on to advanced topics such as fine-tuning, retrieval-augmented generation, and agents becomes significantly more efficient.
