15 — LLM Generation Strategies¶
A practical, production-oriented guide to LLM Generation Strategies, covering autoregressive text generation, decoding, greedy search, beam search, sampling, temperature, Top-K, Top-P, Top-A, typical sampling, repetition penalties, frequency and presence penalties, length control, stopping criteria, deterministic vs stochastic generation, constrained generation, structured output, streaming, speculative decoding, KV cache, generation configuration, Hugging Face Transformers, production inference, RAG generation, agentic AI, evaluation, latency, cost optimization, failure modes, and enterprise AI engineering considerations.
1. Overview¶
Large Language Models generate text token by token.
Given an input sequence:
Input Tokens
↓
Transformer
↓
Next-Token Probabilities
↓
Token Selection
↓
Append Token
↓
Transformer
↓
Next Token
↓
Repeat
This process is called autoregressive generation for models such as GPT-style causal language models.
The model does not directly generate an entire paragraph in one operation.
Instead:
Therefore:
LLM generation is fundamentally a probability-based token-selection process.
Generation strategies determine how the next token is selected from the model's probability distribution.
2. Why LLM Generation Strategies Matter¶
The same model can produce very different outputs depending on its generation configuration.
For example:
while:
Similarly:
versus:
Therefore, generation configuration directly affects:
- Accuracy
- Creativity
- Diversity
- Repetition
- Factual consistency
- Latency
- Token consumption
- Structured output reliability
- Tool-calling reliability
- User experience
3. The LLM Generation Pipeline¶
A simplified generation pipeline:
flowchart TD
A["User Prompt"] --> B["Tokenizer"]
B --> C["Input Tokens"]
C --> D["Transformer"]
D --> E["Logits"]
E --> F["Probability Distribution"]
F --> G["Decoding Strategy"]
G --> H["Selected Token"]
H --> I{"Stop?"}
I -->|No| D
I -->|Yes| J["Generated Output"]
The critical component is:
4. Logits¶
The Transformer produces a vector of raw scores called logits.
Conceptually:
Example:
Logits are not probabilities.
They are converted into probabilities using the softmax function.
5. Softmax¶
The softmax function converts logits into a probability distribution.
Conceptually:
The probability for token i can be represented as:
where:
The resulting probabilities satisfy:
and:
6. Next-Token Prediction¶
Suppose the model sees:
It may produce:
The generation strategy determines what happens next.
With greedy decoding:
is selected because it has the highest probability.
With sampling:
is still very likely, but another candidate may occasionally be selected depending on the sampling configuration.
7. Autoregressive Generation¶
The generation loop is:
Example:
The model repeatedly conditions on the tokens generated so far.
8. Autoregressive Generation Formula¶
For a sequence:
the model estimates:
as:
This factorization is fundamental to autoregressive language modeling.
9. Decoding¶
Decoding refers to the strategy used to transform the model's probability distribution into generated tokens.
Major strategies include:
Greedy Decoding
Beam Search
Temperature Sampling
Top-K Sampling
Top-P Sampling
Top-A Sampling
Typical Sampling
Contrastive Search
Constrained Decoding
Speculative Decoding
These methods optimize different objectives.
10. Deterministic vs Stochastic Generation¶
Generation strategies can broadly be divided into:
Deterministic¶
The same input generally produces the same output.
Examples:
- Greedy decoding
- Beam search
- Sampling with effectively disabled randomness
Stochastic¶
Randomness influences token selection.
Examples:
- Temperature sampling
- Top-K sampling
- Top-P sampling
Conceptually:
flowchart LR
A["Generation"] --> B["Deterministic"]
A --> C["Stochastic"]
B --> D["Greedy"]
B --> E["Beam Search"]
C --> F["Temperature"]
C --> G["Top-K"]
C --> H["Top-P"]
11. Greedy Decoding¶
Greedy decoding selects the token with the highest probability at every step.
Example:
Greedy decoding selects:
every time.
12. Greedy Decoding Workflow¶
flowchart TD
A["Prompt"] --> B["Model"]
B --> C["Token Probabilities"]
C --> D["Select Highest Probability"]
D --> E["Append Token"]
E --> F{"Stop?"}
F -->|No| B
F -->|Yes| G["Output"]
13. Advantages of Greedy Decoding¶
Advantages:
- Simple
- Fast
- Deterministic
- Low computational overhead
- Predictable
- Useful for classification-like generation
Good use cases include:
- Structured extraction
- Simple deterministic transformations
- Some enterprise workflows
- Repeatable evaluations
14. Limitations of Greedy Decoding¶
Greedy decoding makes the best decision locally.
The highest-probability token at one step may lead to a poor sequence later.
Example:
But:
The locally best decision may not produce the globally best sequence.
15. Beam Search¶
Beam search maintains multiple candidate sequences instead of selecting only one token at each step.
Example:
The decoder maintains:
and expands them over multiple steps.
16. Beam Search Workflow¶
flowchart TD
A["Prompt"] --> B["Generate Candidates"]
B --> C["Keep Top N Sequences"]
C --> D["Expand Each Sequence"]
D --> E["Score Candidates"]
E --> F["Keep Best N"]
F --> G{"Stop?"}
G -->|No| D
G -->|Yes| H["Best Sequence"]
17. Beam Width¶
Beam search uses:
For example:
is effectively greedy-style search.
maintains four candidate sequences.
Higher beam width generally means:
but does not guarantee better output quality.
18. Beam Search Scoring¶
A simplified sequence score is:
Because multiplying many probabilities can produce extremely small numbers, log probabilities are commonly used.
Long sequences can receive lower scores simply because more probabilities are multiplied.
Therefore, length normalization or related techniques may be used.
19. Beam Search Limitations¶
Beam search can:
- Increase compute
- Increase latency
- Produce repetitive outputs
- Reduce diversity
- Prefer high-probability generic sequences
For open-ended conversational generation, sampling is often more useful than traditional beam search.
20. Greedy vs Beam Search¶
| Greedy | Beam Search |
|---|---|
| One candidate | Multiple candidates |
| Fast | More compute |
| Deterministic | Deterministic |
| Local decisions | Searches multiple paths |
| Simple | More complex |
| Good for simple generation | Useful for sequence optimization |
21. Temperature¶
Temperature controls the sharpness of the probability distribution.
Conceptually:
where:
22. Low Temperature¶
When:
the distribution becomes sharper.
Example:
After lower temperature:
The model becomes more deterministic.
23. High Temperature¶
When:
the distribution becomes flatter.
Example:
After higher temperature:
This increases diversity.
24. Temperature Intuition¶
25. Temperature Graph¶
Probability
^
| Low T
| /\
| / \
| / \
| High T / \
| _____/ \____
+----------------------------> Tokens
The exact distribution depends on the model logits.
26. Temperature Use Cases¶
Low Temperature¶
Useful for:
- Factual responses
- Structured generation
- Extraction
- Classification
- Enterprise workflows
- Tool calling
Higher Temperature¶
Useful for:
- Brainstorming
- Creative writing
- Ideation
- Story generation
- Alternative phrasing
27. Temperature Is Not a Creativity Switch¶
A common misconception is:
More accurately:
Higher temperature increases randomness but does not guarantee:
28. Top-K Sampling¶
Top-K sampling restricts candidate tokens to the K most probable tokens.
Example:
The model considers only:
and samples from them.
29. Top-K Workflow¶
flowchart TD
A["Logits"] --> B["Probability Distribution"]
B --> C["Sort Tokens"]
C --> D["Keep Top K"]
D --> E["Renormalize"]
E --> F["Sample Token"]
30. Example of Top-K¶
Suppose:
Token A = 0.40
Token B = 0.25
Token C = 0.15
Token D = 0.10
Token E = 0.05
Token F = 0.03
Token G = 0.02
With:
only:
remain candidates.
The probabilities are renormalized before sampling.
31. Advantages of Top-K¶
Top-K:
- Prevents extremely unlikely tokens
- Adds controlled randomness
- Is simple to understand
- Can improve generation diversity
32. Limitations of Top-K¶
A fixed K may not fit every probability distribution.
Example:
may need only:
while a broad distribution may reasonably contain:
A fixed K does not adapt to this variation.
33. Top-P Sampling¶
Top-P, also called nucleus sampling, dynamically selects the smallest set of tokens whose cumulative probability exceeds P.
Example:
The decoder keeps tokens until their cumulative probability reaches approximately:
34. Top-P Workflow¶
flowchart TD
A["Probability Distribution"] --> B["Sort Tokens"]
B --> C["Calculate Cumulative Probability"]
C --> D["Keep Smallest Set ≥ P"]
D --> E["Renormalize"]
E --> F["Sample Token"]
35. Example of Top-P¶
Suppose:
With:
cumulative probability:
Therefore the candidate set becomes:
36. Top-K vs Top-P¶
| Top-K | Top-P |
|---|---|
| Fixed number of tokens | Dynamic number of tokens |
| K controls candidate count | P controls cumulative probability |
| Simple | Adaptive |
| Can be rigid | Usually more flexible |
A common production sampling configuration uses:
37. Top-A Sampling¶
Top-A sampling is another adaptive sampling strategy.
It uses the probability of the highest-probability token as a reference and removes candidates below a relative threshold.
Conceptually:
It is less commonly used than Top-P in mainstream production LLM APIs but is useful to understand as part of the broader decoding landscape.
38. Typical Sampling¶
Typical sampling attempts to select tokens that are representative of the distribution's expected information content rather than simply selecting the highest-probability tokens.
The intuition is:
It can provide an alternative to Top-K and Top-P for certain generation workloads.
39. Sampling Strategy Comparison¶
Greedy
→ Highest probability
Temperature
→ Adjust distribution sharpness
Top-K
→ Keep K candidates
Top-P
→ Keep cumulative probability mass
Top-A
→ Relative probability threshold
Typical
→ Information-content-based filtering
These strategies can sometimes be combined.
40. Repetition Problem¶
LLMs can sometimes produce repetitive text.
Example:
Repetition can arise from:
- Decoding configuration
- Model behavior
- Training data
- Long generation
- Prompt structure
Generation penalties can help.
41. Repetition Penalty¶
A repetition penalty modifies the likelihood of tokens that have already appeared.
Conceptually:
A simplified mental model:
The exact implementation depends on the framework.
42. Frequency Penalty¶
A frequency penalty reduces the likelihood of tokens based on how frequently they have already appeared.
Conceptually:
This can encourage lexical diversity.
43. Presence Penalty¶
A presence penalty penalizes tokens based primarily on whether they have already appeared.
Conceptually:
This can encourage the model to introduce new concepts or words.
44. Frequency vs Presence Penalty¶
| Frequency Penalty | Presence Penalty |
|---|---|
| Depends on occurrence frequency | Depends mainly on whether token appeared |
| Repeated tokens get increasingly penalized | Previously used tokens are penalized |
| Encourages lexical diversity | Encourages introducing new tokens/concepts |
Exact behavior depends on the implementation.
45. Generation Length¶
Generation can be controlled using:
This limits the number of newly generated tokens.
Example:
This is generally preferable to assuming a character-based limit.
46. max_length vs max_new_tokens¶
Important distinction:
usually refers to the total sequence length:
while:
controls newly generated tokens:
For application-level generation control:
is often easier to reason about.
47. Minimum Generation Length¶
Some generation configurations support:
This can prevent the model from stopping too early.
However, forcing a minimum length can also result in unnecessary text.
Use it only when the task requires a minimum output length.
48. Stop Sequences¶
A generation system can stop when a specific token sequence appears.
Example:
Workflow:
flowchart LR
A["Generate Token"] --> B["Check Stop Sequence"]
B --> C{"Matched?"}
C -->|No| A
C -->|Yes| D["Stop Generation"]
Stop sequences are particularly useful for:
- Structured generation
- Tool calls
- Multi-part responses
- Prompt templates
- Agent workflows
49. EOS Token¶
The End-of-Sequence (EOS) token tells the model that generation can stop.
Conceptually:
A correctly configured EOS token is important for generation reliability.
50. Early Stopping¶
Generation may terminate when:
or:
is reached.
The system can also stop because:
has been reached.
Therefore:
51. Deterministic Generation Configuration¶
For deterministic-style generation:
The model will generally select tokens deterministically according to the decoding strategy.
52. Sampling Configuration¶
A sampling configuration might look like:
outputs = model.generate(
**inputs,
do_sample=True,
temperature=0.7,
top_p=0.9,
max_new_tokens=256,
)
This enables stochastic sampling.
53. Greedy Generation Example¶
Use this style when:
are important.
54. Top-K Example¶
outputs = model.generate(
**inputs,
do_sample=True,
top_k=50,
temperature=0.7,
max_new_tokens=256,
)
This limits sampling to the top 50 candidate tokens at each generation step.
55. Top-P Example¶
outputs = model.generate(
**inputs,
do_sample=True,
top_p=0.9,
temperature=0.7,
max_new_tokens=256,
)
The candidate set dynamically changes according to the probability mass.
56. Hugging Face GenerationConfig¶
Hugging Face Transformers provides GenerationConfig for storing generation settings.
Example:
from transformers import GenerationConfig
generation_config = GenerationConfig(
max_new_tokens=256,
temperature=0.7,
top_p=0.9,
do_sample=True,
)
This makes generation configuration explicit and reusable.
57. Production Generation Configuration¶
Do not scatter generation parameters throughout application code.
Prefer:
Example:
Version these configurations like other production artifacts.
58. Task-Specific Generation Strategies¶
Different tasks require different decoding strategies.
| Task | Typical Strategy |
|---|---|
| Classification | Low randomness / deterministic |
| Information Extraction | Deterministic or very low temperature |
| JSON Generation | Low randomness + constraints |
| RAG QA | Low-to-moderate randomness |
| Summarization | Low-to-moderate randomness |
| Brainstorming | Moderate/high sampling |
| Creative Writing | Higher sampling |
| Tool Calling | Low randomness |
| Code Generation | Low/moderate randomness |
| Agent Planning | Usually controlled randomness |
These are starting points rather than universal rules.
59. Generation for RAG¶
In a RAG system:
The goal is generally:
Therefore extremely high temperature is usually undesirable for factual enterprise RAG workloads.
60. RAG Generation Pipeline¶
flowchart TD
A["User Query"] --> B["Retriever"]
B --> C["Relevant Documents"]
C --> D["Prompt Construction"]
D --> E["LLM"]
E --> F["Decoding Strategy"]
F --> G["Generated Answer"]
G --> H["Grounding / Validation"]
H --> I["Response"]
61. Generation for Enterprise Search¶
Enterprise search assistants often need:
A common strategy is:
Generation strategy should support the retrieval architecture rather than compensate for poor retrieval.
62. Generation for Summarization¶
Summarization generally benefits from controlled generation.
Possible configuration:
Evaluation should include:
63. Generation for Creative Writing¶
Creative tasks may benefit from:
The objective is:
But increasing randomness indefinitely does not guarantee quality.
64. Generation for Code¶
Code generation requires:
Often:
is preferred.
However, generating multiple candidates with controlled sampling can be useful for:
65. Self-Consistency¶
For some reasoning workloads, multiple sampled outputs can be generated.
Conceptually:
flowchart TD
A["Problem"] --> B["LLM"]
B --> C["Sample 1"]
B --> D["Sample 2"]
B --> E["Sample 3"]
B --> F["Sample N"]
C --> G["Aggregation"]
D --> G
E --> G
F --> G
G --> H["Final Answer"]
This can improve robustness for certain tasks but increases:
66. Best-of-N Generation¶
Another strategy is:
The evaluator can be:
- Rule-based
- Classifier
- Reward model
- LLM judge
- External validator
- Unit tests
Example:
67. Candidate Generation Architecture¶
flowchart LR
A["Prompt"] --> B["Generator"]
B --> C["Candidate 1"]
B --> D["Candidate 2"]
B --> E["Candidate 3"]
B --> F["Candidate N"]
C --> G["Evaluator"]
D --> G
E --> G
F --> G
G --> H["Best Candidate"]
68. Constrained Decoding¶
Some applications require outputs to follow strict constraints.
Examples:
Instead of allowing arbitrary token generation:
constrained decoding restricts:
at each step.
69. Structured Generation¶
Example schema:
The generation system should enforce:
This is much more reliable than relying only on a natural-language instruction.
70. Constrained Generation Architecture¶
flowchart TD
A["Prompt"] --> B["LLM"]
B --> C["Token Probabilities"]
C --> D["Constraint Engine"]
D --> E["Allowed Tokens"]
E --> F["Next Token"]
F --> B
F --> G["Valid Structured Output"]
71. Grammar-Constrained Decoding¶
A grammar can define valid sequences.
Conceptually:
This can significantly improve structured-output reliability.
72. Tool Calling¶
Tool calling is another structured generation scenario.
The model may produce:
The application then:
73. Tool Calling Generation Loop¶
flowchart TD
A["User"] --> B["LLM"]
B --> C{"Tool Call?"}
C -->|Yes| D["Validate Arguments"]
D --> E["Execute Tool"]
E --> F["Tool Result"]
F --> B
C -->|No| G["Final Response"]
74. Generation in Agentic AI¶
Agentic systems repeatedly invoke models.
Generation strategy must therefore control:
A small generation error can propagate through multiple steps.
75. Agent Stop Conditions¶
Agents need explicit stop conditions.
Possible conditions:
Example:
flowchart TD
A["Agent Step"] --> B{"Final Answer?"}
B -->|Yes| C["Stop"]
B -->|No| D{"Max Steps?"}
D -->|Yes| E["Stop / Escalate"]
D -->|No| F["Execute Action"]
F --> A
76. Streaming Generation¶
LLMs can stream generated tokens as they are produced.
Without streaming:
With streaming:
77. Streaming Architecture¶
flowchart LR
A["Client"] --> B["API"]
B --> C["LLM"]
C --> D["Token Stream"]
D --> B
B --> A
Streaming improves perceived responsiveness because users do not need to wait for the entire response.
78. Time to First Token¶
TTFT — Time To First Token measures how long the system takes to produce the first generated token.
Low TTFT is important for interactive applications.
79. Time Per Output Token¶
TPOT — Time Per Output Token measures the time required to generate subsequent tokens.
Conceptually:
Together they strongly influence perceived generation latency.
80. Prefill vs Decode¶
LLM generation can be divided into:
Prefill¶
The model processes the input prompt.
Decode¶
The model generates tokens one at a time.
81. Prefill and Decode Architecture¶
flowchart LR
A["Prompt"] --> B["Prefill"]
B --> C["KV Cache"]
C --> D["Decode"]
D --> E["Token 1"]
E --> D
D --> F["Token 2"]
F --> D
D --> G["Token N"]
This distinction is important for performance engineering.
82. KV Cache¶
The Transformer repeatedly computes attention over prior tokens.
The KV cache stores previously computed:
so the model does not need to recompute them from scratch for every generated token.
Conceptually:
83. Why KV Cache Matters¶
Without KV cache:
With KV cache:
This significantly improves autoregressive decoding efficiency.
84. KV Cache Memory¶
KV cache grows with:
Context Length
+
Number of Layers
+
Number of Attention Heads
+
Head Dimension
+
Batch Size
+
Concurrency
Therefore:
can create substantial memory pressure even when model weights are quantized.
85. Speculative Decoding¶
Speculative decoding uses a smaller draft model to propose tokens that a larger model verifies.
Architecture:
flowchart TD
A["Prompt"] --> B["Draft Model"]
B --> C["Candidate Tokens"]
C --> D["Large Target Model"]
D --> E["Verify Candidates"]
E --> F["Accept / Reject"]
F --> G["Next Generation Step"]
The goal is to improve generation speed without changing the target model's output distribution when implemented correctly.
86. Speculative Decoding Concept¶
Instead of:
use:
If many proposed tokens are accepted:
which can improve throughput or reduce latency.
87. Speculative Decoding Trade-Offs¶
Advantages:
- Potentially lower latency
- Better utilization of target model
- Useful for autoregressive generation
Trade-offs:
- Requires compatible draft model
- Additional infrastructure complexity
- Acceptance rate matters
- Performance depends on hardware and workload
88. Generation and Quantization¶
Generation strategy and model quantization are different concerns.
They can be combined:
or:
89. Generation and LoRA¶
Similarly:
A LoRA-adapted model can use:
depending on the application.
90. Generation and RAG¶
Therefore:
all contribute to final answer quality.
91. Hallucination and Generation¶
Higher randomness can sometimes increase the likelihood of unsupported content.
However:
Hallucinations can result from:
- Missing knowledge
- Poor retrieval
- Ambiguous prompts
- Model limitations
- Training data
- Decoding strategy
Therefore lowering temperature alone is not a complete hallucination strategy.
92. Grounded Generation¶
A production RAG system should use:
Architecture:
flowchart TD
A["User Query"] --> B["Retriever"]
B --> C["Evidence"]
C --> D["Prompt"]
D --> E["LLM"]
E --> F["Controlled Generation"]
F --> G["Grounding Validation"]
G --> H["Response"]
93. Generation Evaluation¶
Generation quality should not be measured only by:
Possible metrics include:
- BLEU
- ROUGE
- METEOR
- BERTScore
- Perplexity
- Task-specific accuracy
- Human evaluation
- LLM-as-a-judge
- Faithfulness
- Groundedness
- JSON validity
- Tool-call accuracy
The correct metric depends on the task.
94. Deterministic Evaluation¶
For reproducible benchmarking:
This makes comparisons between model versions more meaningful.
95. Stochastic Evaluation¶
For creative or sampling-based systems, a single output may not represent the full behavior.
Evaluate:
and measure:
This is especially useful for:
- Creative generation
- Agentic workflows
- Candidate generation
- Self-consistency
96. Generation Quality vs Diversity¶
There is often a trade-off:
while:
The correct point depends on the business task.
97. Generation Strategy Matrix¶
| Strategy | Determinism | Diversity | Compute | Typical Use |
|---|---|---|---|---|
| Greedy | High | Low | Low | Extraction |
| Beam Search | High | Low/Medium | Higher | Sequence tasks |
| Temperature | Variable | Variable | Low | General sampling |
| Top-K | Variable | Medium | Low | Controlled sampling |
| Top-P | Variable | Medium/High | Low | General generation |
| Typical | Variable | Medium | Low/Medium | Alternative sampling |
| Best-of-N | Low | High | High | Candidate selection |
| Speculative | Depends | Depends | More complex | Speed optimization |
98. Task-to-Strategy Mapping¶
flowchart TD
A["Task"] --> B{"Task Type"}
B -->|Extraction| C["Low Randomness"]
B -->|RAG QA| D["Controlled Sampling"]
B -->|Creative| E["Higher Sampling"]
B -->|Code| F["Low / Moderate Sampling"]
B -->|Tool Calling| G["Constrained / Low Randomness"]
B -->|Structured Output| H["Constrained Decoding"]
B -->|Agentic| I["Controlled + Explicit Stop Conditions"]
99. Generation Configuration as a Product Contract¶
Production systems should treat generation configuration as part of the model behavior.
Example:
model:
name: enterprise-llm
version: "3.2"
generation:
strategy: top_p
temperature: 0.2
top_p: 0.9
max_new_tokens: 512
repetition_penalty: 1.05
stopping:
eos_token: true
stop_sequences:
- "<END>"
Changing these values can change user-visible behavior.
Therefore they should be version-controlled.
100. Per-Task Generation Profiles¶
Instead of one global configuration:
use task-specific profiles.
Example:
profiles:
extraction:
temperature: 0.0
max_new_tokens: 256
rag:
temperature: 0.2
top_p: 0.9
max_new_tokens: 512
creative:
temperature: 0.8
top_p: 0.95
max_new_tokens: 1024
tool_calling:
temperature: 0.0
max_new_tokens: 256
This is often more appropriate for enterprise systems.
101. Generation Provider Interface¶
In a cloud-native application, generation configuration can be abstracted behind an interface.
Example request:
This keeps application logic independent from a particular model runtime.
102. Generation Adapter¶
Architecture:
flowchart LR
A["Application"] --> B["LLMProvider"]
B --> C["Generation Adapter"]
C --> D["Model Runtime"]
D --> E["LLM"]
The adapter can translate:
into:
specific parameters.
103. Provider-Agnostic GenerationConfig¶
A useful abstraction might include:
public record GenerationConfig(
Integer maxNewTokens,
Double temperature,
Double topP,
Integer topK,
Double repetitionPenalty,
Boolean doSample,
Integer numBeams
) {}
Provider-specific features should remain optional or capability-driven.
104. Capability-Based Generation¶
Not every provider supports every generation feature.
For example:
Provider A
→ Top-P
→ Temperature
Provider B
→ Top-P
→ Temperature
→ Grammar Constraints
Provider C
→ Tool Calling
→ Structured Output
Therefore:
is often better than assuming universal support.
105. Generation Configuration Validation¶
Before sending a request:
- Temperature range
- Top-P range
- Top-K validity
- Token limits
- Beam count
- Stop sequences
- Model context window
- Provider capabilities
Example:
if (config.temperature() != null
&& config.temperature() < 0) {
throw new IllegalArgumentException(
"Temperature must be non-negative"
);
}
106. Context Window and Generation Length¶
A model has a maximum context window.
Conceptually:
Therefore:
must be considered alongside input length.
Example:
Actual runtime behavior depends on the model and serving system.
107. Context Budget¶
A production application should explicitly manage the token budget.
Context Window
↓
System Prompt
+
User Prompt
+
Retrieved Context
+
Conversation History
+
Output Budget
Architecture:
flowchart LR
A["Context Window"] --> B["System Prompt"]
A --> C["User Input"]
A --> D["RAG Context"]
A --> E["Conversation"]
A --> F["Output Budget"]
This is critical for RAG and agentic systems.
108. Dynamic Max Tokens¶
Instead of always setting:
calculate an appropriate output budget from:
This avoids unnecessary context overflow.
109. Generation and Cost¶
Generation cost is heavily influenced by:
Therefore:
is not only a quality parameter.
It is also:
110. Generation and Latency¶
More generated tokens generally mean:
Therefore:
is one of the simplest ways to control latency.
111. Generation and Concurrency¶
At high concurrency:
Therefore generation configuration must be designed together with:
112. Continuous Batching¶
Modern inference engines can combine requests dynamically.
Conceptually:
flowchart TD
A["Request 1"] --> D["Continuous Batching"]
B["Request 2"] --> D
C["Request 3"] --> D
D --> E["GPU Inference"]
E --> F["Token Streams"]
This improves GPU utilization in high-throughput serving environments.
113. Generation and Batching¶
Batching can improve throughput:
But interactive workloads care about:
while batch workloads care more about:
Therefore optimize according to the workload.
114. Streaming vs Non-Streaming¶
Streaming¶
Advantages:
- Lower perceived latency
- Better interactive UX
- Immediate token delivery
Disadvantages:
- More complex client/server handling
- Partial responses
- More complicated error handling
Non-Streaming¶
Advantages:
- Simple API
- Easier response validation
- Easier batch processing
Disadvantages:
- Higher perceived latency
115. Production Streaming Architecture¶
sequenceDiagram
participant U as User
participant A as API
participant L as LLM
U->>A: Generate Request
A->>L: Generation Request
L-->>A: Token 1
A-->>U: Token 1
L-->>A: Token 2
A-->>U: Token 2
L-->>A: Token N
A-->>U: Token N
L-->>A: EOS
A-->>U: Complete
Streaming protocols may use:
depending on the application architecture.
116. Generation Timeout¶
Production generation should have explicit timeouts.
Possible actions:
Do not allow unlimited generation.
117. Retry Strategy¶
Retries must be designed carefully.
A failed generation request can be retried when:
But blindly retrying:
may not help.
For stochastic generation, retrying may produce a different result, but this should be intentional.
118. Fallback Models¶
Production systems can use fallback models.
flowchart TD
A["Request"] --> B["Primary Model"]
B --> C{"Available?"}
C -->|Yes| D["Response"]
C -->|No| E["Fallback Model"]
E --> D
Fallback decisions may depend on:
- Timeout
- Capacity
- Provider outage
- Cost
- Model health
119. Generation Guardrails¶
Generation should be surrounded by guardrails.
Possible controls:
- PII filtering
- Toxicity detection
- Schema validation
- Grounding validation
- Policy enforcement
- Tool authorization
120. Output Validation¶
For production systems:
Examples:
This is often more reliable than relying only on generation settings.
121. Self-Repair Generation¶
If output fails validation:
Architecture:
flowchart TD
A["LLM Generation"] --> B["Validator"]
B --> C{"Valid?"}
C -->|Yes| D["Return"]
C -->|No| E["Repair / Retry"]
E --> A
A maximum retry count should be enforced.
122. Generation Budgets¶
Production systems should define budgets for:
Example:
This prevents uncontrolled generation.
123. Generation Observability¶
Log or measure:
Model
Model Version
Generation Profile
Temperature
Top-P
Top-K
Max Tokens
Input Tokens
Output Tokens
TTFT
TPOT
Total Latency
Finish Reason
Error
Avoid logging sensitive prompts or outputs unless appropriate data-governance controls are in place.
124. Generation Tracing¶
Distributed tracing can connect:
This makes it possible to identify whether failures come from:
125. Generation Failure Taxonomy¶
Common failures:
Model-Level¶
- Hallucination
- Repetition
- Poor reasoning
- Incorrect tool call
Decoding-Level¶
- Excessive randomness
- Overly deterministic output
- Repetition
- Premature stopping
Context-Level¶
- Context overflow
- Poor retrieved context
- Missing information
Infrastructure-Level¶
- Timeout
- OOM
- GPU saturation
- Runtime failure
126. Debugging Generation Quality¶
Use this sequence:
1. Validate Prompt
↓
2. Validate Input Tokens
↓
3. Validate Model
↓
4. Validate Generation Config
↓
5. Validate Retrieved Context
↓
6. Compare Deterministic Output
↓
7. Compare Sampling Output
↓
8. Evaluate Multiple Runs
This isolates decoding issues from model and data issues.
127. Common Mistake — High Temperature for Factual QA¶
Bad assumption:
For factual enterprise workloads, excessive randomness can reduce consistency.
Prefer:
128. Common Mistake — Temperature 0 Means Zero Hallucination¶
Temperature controls sampling behavior.
It does not guarantee:
A deterministic model can confidently produce incorrect information.
Therefore:
129. Common Mistake — Using Top-K and Top-P Without Understanding Interaction¶
Using multiple sampling filters changes the candidate distribution.
Example:
The resulting behavior depends on implementation order and runtime.
Test configurations empirically rather than assuming their effects are independent.
130. Common Mistake — Excessive max_new_tokens¶
Setting:
does not mean the model should generate 4096 tokens.
It creates a larger maximum budget.
Excessive budgets can increase:
Use task-specific limits.
131. Common Mistake — No Stop Condition¶
Without appropriate stopping:
until:
is reached.
For structured or agentic systems, explicit stop conditions are essential.
132. Common Mistake — One Generation Config for Every Task¶
A single configuration:
for every application is rarely optimal.
Instead:
133. Common Mistake — Ignoring Context Window¶
If:
generation can fail, truncate, or behave unexpectedly depending on the runtime.
Always calculate:
134. Common Mistake — Ignoring Streaming Backpressure¶
Streaming systems must handle:
Production streaming needs:
135. Common Mistake — Logging Sensitive Output¶
LLM outputs may contain:
Therefore observability systems should implement:
136. Production Workflow¶
A production-grade generation workflow:
flowchart TD
A["User Request"] --> B["Input Validation"]
B --> C["Prompt Construction"]
C --> D["Context / RAG"]
D --> E["Generation Profile"]
E --> F["LLM Runtime"]
F --> G["Streaming / Response"]
G --> H["Output Validation"]
H --> I{"Valid?"}
I -->|Yes| J["Return"]
I -->|No| K["Repair / Fallback"]
K --> F
J --> L["Observability"]
The workflow should include:
- Input validation
- Context management
- Generation configuration
- Model invocation
- Output validation
- Retry/fallback
- Observability
- Cost tracking
137. Production Generation Architecture¶
A cloud-native enterprise architecture:
flowchart TD
A["Client"] --> B["API Gateway"]
B --> C["Spring Boot AI Service"]
C --> D["Prompt Service"]
C --> E["RAG Service"]
C --> F["Generation Policy"]
D --> G["LLM Gateway"]
E --> G
F --> G
G --> H["Model Router"]
H --> I["Quantized LLM"]
H --> J["Cloud LLM"]
H --> K["Fallback Model"]
I --> L["Output Guardrails"]
J --> L
K --> L
L --> M["Response"]
C --> N["Observability"]
138. Generation Policy Layer¶
An enterprise system can introduce a policy layer:
Example:
This prevents individual services from arbitrarily changing model behavior.
139. Multi-Model Generation Routing¶
Different tasks can use different models.
flowchart TD
A["Request"] --> B["Model Router"]
B --> C["Small Fast Model"]
B --> D["General Model"]
B --> E["Large Reasoning Model"]
C --> F["Response"]
D --> F
E --> F
Routing can consider:
140. Generation Cost Routing¶
Example policy:
This can reduce cost while maintaining quality.
141. Generation Strategy and SLA¶
Generation configuration should be aligned with:
For example:
Interactive Chat
→ Low TTFT
Batch Summarization
→ High Throughput
Enterprise Extraction
→ High Accuracy
Creative Application
→ High Diversity
One generation strategy cannot optimize all objectives simultaneously.
142. Generation Benchmark¶
A proper benchmark should include:
Example:
| Configuration | Quality | TTFT | TPOT | Memory | Cost |
|---|---|---|---|---|---|
| Greedy | Measure | Measure | Measure | Measure | Measure |
| Temp 0.2 | Measure | Measure | Measure | Measure | Measure |
| Top-P 0.9 | Measure | Measure | Measure | Measure | Measure |
| Best-of-N | Measure | Measure | Measure | Measure | Measure |
143. Experimental Methodology¶
When comparing decoding strategies:
Change only:
Then compare:
This produces a meaningful comparison.
144. Generation Strategy Selection¶
A practical engineering process:
1. Define Task
↓
2. Define Quality Metric
↓
3. Define Latency SLA
↓
4. Define Cost Budget
↓
5. Establish Baseline
↓
6. Test Decoding Strategies
↓
7. Evaluate
↓
8. Select Configuration
↓
9. Deploy
↓
10. Monitor
145. Enterprise Generation Checklist¶
[ ] Task Defined
[ ] Baseline Established
[ ] Generation Strategy Selected
[ ] Temperature Tuned
[ ] Top-P / Top-K Evaluated
[ ] Output Length Defined
[ ] Stop Conditions Defined
[ ] Context Budget Defined
[ ] Structured Output Validated
[ ] Tool Calling Tested
[ ] Streaming Tested
[ ] Timeout Defined
[ ] Retry Policy Defined
[ ] Fallback Defined
[ ] Token Cost Measured
[ ] Latency Measured
[ ] Quality Measured
[ ] Safety Tested
[ ] Observability Enabled
146. Interview Questions¶
Beginner¶
- What is autoregressive generation?
- What is decoding?
- What are logits?
- How are logits converted into probabilities?
- What is greedy decoding?
- What is beam search?
- What is temperature?
- What is Top-K sampling?
- What is Top-P sampling?
- What is an EOS token?
- What is
max_new_tokens?
Intermediate¶
- Greedy vs sampling?
- Greedy vs beam search?
- Temperature vs Top-P?
- Top-K vs Top-P?
- Why can high temperature produce unstable outputs?
- What is repetition penalty?
- Frequency penalty vs presence penalty?
- Why are stop sequences important?
- What is constrained decoding?
- Why is streaming useful?
- What is TTFT?
- What is TPOT?
- What is KV cache?
- Why does KV cache improve autoregressive generation?
- What is speculative decoding?
Advanced¶
- How would you design generation policies for an enterprise AI platform?
- How would you tune generation for RAG?
- How would you tune generation for tool calling?
- How would you optimize generation latency?
- How would you design a multi-model generation router?
- How would you evaluate sampling strategies?
- How would you implement structured-output validation?
- How would you design generation observability?
- How would you control token costs?
- How would you combine quantization with generation optimization?
- How would you design generation fallback strategies?
- How would you handle context-window constraints?
- How would you optimize high-concurrency generation?
- How would you design speculative decoding infrastructure?
- How would you prevent runaway agent generation?
147. Scenario-Based Interview Questions¶
Scenario 1 — RAG Answers Are Too Creative¶
Problem:
Investigate:
Try controlled generation:
Scenario 2 — JSON Output Is Invalid¶
Do not immediately increase or decrease temperature.
Use:
where supported.
Fallback:
Scenario 3 — Chatbot Latency Is Too High¶
Investigate:
Potential optimizations:
Reduce Prompt Size
+
Reduce Output Budget
+
Quantize Model
+
Use Faster Runtime
+
Use Continuous Batching
+
Use Speculative Decoding
Scenario 4 — Model Repeats Itself¶
Investigate:
Do not assume the decoding configuration is the only cause.
Scenario 5 — Agent Runs Forever¶
Implement explicit:
Example:
148. 🚀 Quick Revision Sheet¶
Generation¶
Deterministic¶
- Greedy
- Beam Search
Sampling¶
- Temperature
- Top-K
- Top-P
- Top-A
- Typical Sampling
Control¶
- Repetition Penalty
- Frequency Penalty
- Presence Penalty
max_new_tokens- EOS
- Stop Sequences
Advanced¶
- Constrained Decoding
- Structured Generation
- Best-of-N
- Self-Consistency
- Speculative Decoding
- Streaming
- KV Cache
Production¶
149. Remember¶
LLMs generate text by repeatedly predicting the next token, while decoding strategies determine how those token probabilities are converted into actual output.
Remember the core pipeline:
And:
Greedy
→ Highest Probability Token
Temperature
→ Changes Distribution Sharpness
Top-K
→ Fixed Candidate Count
Top-P
→ Probability-Mass-Based Candidate Set
For production systems:
must be designed together.
150. Key Takeaways¶
- LLMs typically generate text autoregressively, one token at a time.
- Transformer outputs logits that are converted into token probabilities.
- Decoding determines how the next token is selected.
- Greedy decoding selects the highest-probability token.
- Beam search maintains multiple candidate sequences.
- Temperature controls the sharpness of the probability distribution.
- Lower temperature generally produces more deterministic behavior.
- Higher temperature generally increases sampling diversity.
- Top-K sampling restricts generation to a fixed number of high-probability candidates.
- Top-P sampling dynamically selects candidates based on cumulative probability.
- Top-A and typical sampling provide alternative adaptive decoding strategies.
- Repetition penalties can discourage repeated tokens.
- Frequency penalties depend on token occurrence frequency.
- Presence penalties discourage reuse of previously seen tokens.
max_new_tokensprovides an explicit output-token budget.- EOS tokens and stop sequences provide important termination controls.
- Constrained decoding is useful for structured output, JSON, tool calling, and grammar-based generation.
- Streaming reduces perceived latency by returning generated tokens incrementally.
- TTFT measures time to the first generated token.
- TPOT measures the time required for subsequent generated tokens.
- Prefill processes the input context while decode generates tokens autoregressively.
- KV cache improves generation efficiency by reusing attention key/value states.
- KV cache can become a major memory consumer for long-context and high-concurrency workloads.
- Speculative decoding uses a draft model to propose tokens that a target model verifies.
- Best-of-N and self-consistency can improve robustness at the cost of additional compute.
- Generation settings should be task-specific rather than globally fixed.
- RAG applications generally benefit from controlled, grounded generation.
- Creative applications may benefit from higher sampling diversity.
- Tool calling and structured generation generally require controlled decoding and strong validation.
- Temperature does not guarantee truthfulness or eliminate hallucinations.
- Generation quality depends on model quality, context, prompting, retrieval, decoding, and validation.
- Generation configuration should be version-controlled as part of model behavior.
- Production systems should explicitly manage token, latency, retry, agent-step, and cost budgets.
- Generation should be monitored using latency, throughput, token usage, quality, errors, and cost.
- Quantization, LoRA, RAG, and generation strategies solve different problems and can be combined.
- Enterprise AI systems should isolate generation configuration behind provider and inference abstractions.
- Generation policies can route different tasks to different models and decoding profiles.
- The best generation strategy is the one that satisfies the application's quality, latency, reliability, and cost requirements.
151. Chapter Navigation¶
Previous Chapter¶
Current Chapter¶
15. LLM Generation Strategies
Next Chapter¶
Related Chapters¶
- 01. Generative AI Fundamentals
- 02. Language Understanding Fundamentals
- 03. Word Embeddings
- 04. Language Modeling
- 05. Attention and Positional Encoding
- 06. GPT and BERT Architecture
- 07. Hugging Face and Transformers
- 08. LLM Data Preparation
- 09. Hugging Face Training Workflow
- 10. Transformer Fine-Tuning Fundamentals
- 11. Supervised Fine-Tuning (SFT)
- 12. Parameter-Efficient Fine-Tuning (PEFT)
- 13. LoRA and QLoRA
- 14. Model Quantization
- 16. LLM Evaluation
References¶
- Vaswani et al. — Attention Is All You Need
- Hugging Face Transformers Documentation
- Hugging Face Generation Strategies Documentation
- Hugging Face GenerationConfig Documentation
- Hugging Face Text Generation Inference Documentation
- vLLM Documentation
- NVIDIA TensorRT-LLM Documentation
- PyTorch Documentation
- Holtzman et al. — The Curious Case of Neural Text Degeneration
- Meister et al. — Locally Typical Sampling
- Li et al. — Contrastive Search Is What You Need for Neural Text Generation
- Leviathan et al. — Fast Inference from Transformers via Speculative Decoding
- Stern et al. — Blockwise Parallel Decoding for Deep Autoregressive Models
- Wolf et al. — Transformers: State-of-the-Art Natural Language Processing
- Speech and Language Processing — Jurafsky & Martin
Enterprise AI Engineering Handbook
Building Production-Grade Enterprise AI Systems — One Chapter at a Time.