induwara.lk
induwara.lkAI · LLM sampling

Presence vs Frequency Penalty Calculator (OpenAI)

Enter a few candidate tokens with their logits and how often each has appeared, then drag the presence and frequency penalty sliders to watch the adjusted logits and softmax probabilities update — and see when the most likely next token flips. Uses OpenAI's exact additive formula, runs fully in your browser, no signup.

By Induwara AshinsanaUpdated Jul 10, 2026
Presence & frequency penaltylogits → softmax
OpenAI additive formula

Candidate tokens

0.00

Subtracted once from any token already used (count > 0).

0.00

Subtracted per prior occurrence — scales with the count.

1.00

Softmax sharpness. Applied to both before and after.

Presets
Most likely — before
the
62.85%
Most likely — after
the
62.85%
Winner
Unchanged
Penalties did not change the top token

Before vs after probability

thetop62.85%62.85%
cat23.12%23.12%
dog14.02%14.02%

Per-token breakdown

TokenLogitCount−Presence−FrequencyAdj logitP beforeP afterΔ
the3400362.85%62.85%0%
cat2000223.12%23.12%0%
dog1.51001.514.02%14.02%0%

Probabilities verified two ways (max softmax difference < 1e-9).

How “the” was adjusted

ℓ' = ℓ − presence·1[count>0] − frequency·count
= 30·10·4 = 3
softmax(ℓ' ÷ T=1) over all 3 tokens
P(“the”): 62.85% 62.85% (0 pts)
Presence is a one-off nudge (any repeat costs the same); frequency compounds with every occurrence. Both are additive on the logit and bounded to [-2, 2] — unlike HuggingFace's multiplicative repetition_penalty.
Additive penalty formula and [-2, 2] range from OpenAI.

How it works

OpenAI's Chat Completions API exposes two sampling controls that look alike but behave differently: presence_penalty and frequency_penalty. Both accept a value from -2.0 to 2.0 and both nudge the model away from (or toward) repeating itself — but they touch the token logits in distinct ways. This tool applies the exact adjustment documented by OpenAI so you can see the effect token by token, without spending API credits to experiment.

For each candidate token j with base logit ℓj and a prior occurrence count cj, the API adjusts the logit before sampling:

ℓ'j = ℓj − frequency_penalty · cj − presence_penalty · 1[cj> 0]
  • The presence term subtracts the full presence_penalty once if the token has appeared at all (cj> 0), regardless of how many times. It is a flat “have we used this already?” switch.
  • The frequency term subtracts frequency_penalty × cj, so it grows linearly with the count. A token used five times is penalised five times as hard as one used once.

After the adjustment the tool converts logits to probabilities the same way the model samples them: divide each adjusted logit by the temperature T, then apply softmax, pj = e^(zj) / Σ e^(zk), computed with max-subtraction for numerical stability. The “before” probabilities use the un-penalised logits ÷ T, so the comparison isolates the penalty effect rather than mixing in a temperature change. As a self-check the tool also recomputes the softmax through the identity pj = 1 / Σ e^(zk − zj) and reports the (near-zero) difference between the two paths.

Because softmax is relative, lowering one token's logit raises every other token's probability. When a previously-favoured but already-repeated token drops far enough, an unused alternative overtakes it — the tool highlights this flip. Note that OpenAI's additive penalties are not the same as HuggingFace's multiplicative repetition_penalty; the two use different math and their values do not transfer.

Worked examples

Presence + frequency flip the winner — presence 0.5, frequency 0.3, T = 1

  1. Token A: logit 2.0, count 3 → adj = 2.0 − 0.5·1 − 0.3·3 = 0.6
  2. Token B: logit 1.5, count 0 → adj = 1.5 − 0 − 0 = 1.5
  3. Before: P(A) = e^2.0 / (e^2.0 + e^1.5) = 62.25%, P(B) = 37.75%
  4. After: P(A) = e^0.6 / (e^0.6 + e^1.5) = 28.91%, P(B) = 71.09%
  5. The already-repeated favourite A loses to the unused token B.

Frequency dominates for heavy repeats — presence 0.2, frequency 0.4

  1. Token "the": logit 3.0, count 5
  2. adj = 3.0 − 0.2·1 − 0.4·5 = 3.0 − 0.2 − 2.0 = 0.8
  3. Total reduction 2.2 = 0.2 presence + 2.0 frequency
  4. For a token seen 5×, the frequency part is 10× the presence part.

The count = 0 vs count = 1 boundary — presence 1.0, frequency 0

  1. Token X: logit 2.0, count 0 → presence part 0 → adj = 2.0
  2. Token Y: logit 2.0, count 1 → presence part 1.0 → adj = 1.0
  3. The presence switch fires only once the count crosses from 0 to 1.
  4. This off-by-one boundary is where presence_penalty first bites.

Frequently asked questions

Sources & references

The additive formula and the [-2, 2] parameter range were last cross-checked against the OpenAI API reference (presence_penalty / frequency_penalty) on 2026-07-10. The two worked examples above (28.91% / 71.09% and adjusted logit 0.8) are reproduced exactly by the calculator.

Related tools

Rate this tool
Be the first to rate

Comments & feedback

Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.

Found a bug, edge case, or want to suggest an improvement?

Email me at [email protected] — most fixes ship within 24 hours.