EngineeringIntermediate
Deterministic AI Outputs: Using seed, temperature=0, and Grammar Constraints
Direct Answer & Overview
A technical guide to achieving reproducible, deterministic outputs from LLM APIs using fixed integer seeds, zero-temperature greedy decoding, and formal grammar constraints (CFGs and regex).
1.Why LLMs Are Non-Deterministic by Default
During autoregressive decoding, next-token probabilities are sampled pseudo-randomly based on temperature and top_p. Even with temperature=0 (greedy decoding where the highest-probability token is always selected), parallel GPU floating-point operations across CUDA threads can cause subtle round-off variations, leading to diverging token trajectories on subsequent runs.
2.The `seed` Parameter and System Fingerprints
Setting an explicit integer `seed` (e.g. `seed=42`) instructs the inference server to sample pseudo-random numbers with deterministic initialization. The API returns a `system_fingerprint` in the response, indicating the specific backend GPU configuration used. When both seed and system_fingerprint match, responses are overwhelmingly identical.
3.Grammar-Constrained Decoding (GBNF and Outlines)
To guarantee 100% deterministic schema adherence, grammar-constrained engines (like Outlines and Guidance) mask invalid token logits during inference, mathematically preventing the model from ever generating a token that violates a defined regular expression or Context-Free Grammar (CFG).
Executing Deterministic Seeded Requestspython
from openai import OpenAI
client = OpenAI(base_url="https://api.apihundred.com/v1", api_key="your_key")
def run_deterministic_query(prompt: str):
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}],
temperature=0.0,
seed=1337 # Fixed seed for reproducible generation
)
print("Fingerprint:", response.system_fingerprint)
return response.choices[0].message.content
# Subsequent identical calls produce matching outputs
print(run_deterministic_query("Sort the list [9, 2, 7, 1, 5] and explain step 1."))Frequently Asked Questions
Does OpenAI guarantee 100% determinism with seed?
OpenAI states that seed provides best-effort determinism. Backend model version updates or hardware migrations may alter the system_fingerprint.
Why should unit tests use fixed seeds?
Fixed seeds prevent flaky CI test suites by ensuring consistent output assertions across integration test runs.
How do grammar-constrained decoders work?
They compile a regex or JSON schema into a state machine that sets logits of non-matching tokens to negative infinity at each step.
A100
API100 Engineering Team
Infrastructure & Latency Research

