Loading

Quipoin Menu

Learn • Practice • Grow

/
interview

Q1. What are frequency penalty and presence penalty in LLM generation?
Frequency penalty and presence penalty are parameters that reduce the likelihood of token repetition. They apply penalties to tokens based on how often they have appeared so far. • Frequency penalty: penalizes tokens proportionally to how many times they have already appeared in the generated text. Higher values (e.g., 0.5 to 1.0) strongly discourage repetition of common words. • Presence penalty: penalizes tokens if they have appeared at least once, regardless of frequency. It treats a token that appears once the same as one that appears ten times. Both parameters typically range from -2.0 to 2.0. Positive values reduce repetition; negative values encourage repetition (rarely used). In OpenAI APIs, default is 0.

Q2. How do frequency penalty and presence penalty differ?
Key differences: • Frequency penalty accumulates: a token that appears 3 times gets 3 times the penalty. • Presence penalty is binary: penalty applies if token appears ≥1 time, regardless of count. • Use frequency penalty to reduce repetitive phrases (e.g., "the the the") or repeated sentence structures. • Use presence penalty to encourage the model to introduce new topics or avoid sticking to a single concept. • Example: In a story, presence penalty (0.5) might make the model talk about new characters, while frequency penalty (0.3) prevents overusing the same adjective. • Both can be used together: typical values frequency_penalty=0.3, presence_penalty=0.3 for creative tasks. For factual tasks, set both to 0.

Q3. Give a practical example of when to increase frequency or presence penalties.
Scenario 1: Generating a list of unique keywords. Use presence_penalty=1.0 to force diverse terms. Scenario 2: Writing a long article where the model repeats the same phrase (e.g., "in addition"). Use frequency_penalty=0.5 to discourage repetition. Scenario 3: Brainstorming product names – set both penalties high (1.0) to avoid identical suggestions. Scenario 4: Summarizing a document – keep penalties low (0.1) to avoid missing important repeated terms. Scenario 5: Chatbot that should not keep saying "I understand" – use presence_penalty=0.2 to gently discourage it. Test with small increments; too high penalties can cause unnatural output or topic jumps.

Q4. How do these penalties affect the token logits mathematically?
Penalties modify the logits (pre-softmax scores) of tokens before sampling. The adjustment is applied per token based on its history. Let c(t) = number of times token t has appeared in the generated text so far. Then the adjusted logit score(t) = original_logit(t) - frequency_penalty * c(t) - presence_penalty * (1 if c(t)>0 else 0). Negative penalties would increase logits (encourage repetition). This formula means that more frequent tokens get higher penalties linearly. The penalties are applied at each generation step. In practice, the effect saturates; setting penalties > 1.0 is rarely needed. Some implementations (like OpenAI) apply penalties only to tokens that have already appeared at least once.

Q5. When should you avoid using frequency and presence penalties?
Avoid penalties when: • The task requires exact repetition (e.g., copying a list, generating code with repeated keywords). • The expected output is naturally repetitive (e.g., generating a poem with chorus). • The prompt already includes repetition that the model should continue. • Using with temperature=0 (greedy) – penalties have no effect because only the top token is selected. • Very short responses where penalties may artificially suppress legitimate repeats (e.g., "yes yes"). • The model's default behavior already produces sufficiently diverse outputs. • In production systems where consistency and determinism are more important than diversity. Best practice: start with 0, then increase gradually only if repetition is a problem.