Deterministic LLM Pipelines: a framework and experimental toolkit for guaranteeing that the same input produces the same final output artifact, even when intermediate LLM generations differ.
- What conditions are required for end-to-end determinism? — We formalize the pipeline as
prompt → retrieval → generation → transformation → validation → outputand identify where nondeterminism enters. - Where does nondeterminism actually enter the pipeline? — LLM sampling (even at temperature=0), retrieval ordering, timestamp injection, floating-point variance across hardware.
- How do normalization + validation eliminate variance? — We propose a deterministic convergence loop that repeatedly applies
generate → normalize → validateuntil output stabilizes.
Input → [Prompt Template] → [Retrieval] → [LLM Generation] → [Normalization] → [Validation] → Output
↑ |
└──────── convergence loop ────────────┘
| Level | Definition | Example |
|---|---|---|
| Strong | Bitwise identical output (sha256(a) == sha256(b)) |
JSON with sorted keys, normalized whitespace |
| Weak | Semantically identical output (same meaning, different surface form) | "32 years old" vs "age: 32" |
The loop feeds output back through generation → normalization → validation until:
- Two consecutive iterations produce the same artifact hash (converged), or
- Max iterations reached (did not converge)
This is the core contribution: normalization + validation as determinism-forcing functions.
| Model | Experiment | n | Unique Outputs | Strong Determinism | Convergence |
|---|---|---|---|---|---|
| GPT-4o | JSON extraction | 42 | 1 | 100% | 100% |
| GPT-4.1-mini | JSON extraction | 20 | 1 | 100% | 100% |
| GPT-4o | Code generation | 20 | 3 | 80% | 100% |
| GPT-4.1-mini | Code generation | 19 | 2 | 94.7% | 100% |
| GPT-4.1-mini | Summarization | 20 | 19 | 10% | 0% |
Key finding: determinism is a function of output structure. Tasks with canonical forms (JSON) achieve perfect determinism through normalization. Free-form text remains irreducibly nondeterministic.
Three built-in experiments test the framework across task types:
| Experiment | Task |
|---|---|
json_extraction |
Extract structured JSON from text |
code_generation |
Generate a Python function from spec |
summarization |
Summarize text in 2 sentences |
# Install
pip install -e ".[dev,llm]"
# Run with real LLM via GitHub Models (free)
export GITHUB_TOKEN=ghp_...
kai run json_extraction --github -t 0.7 --no-seed -n 20
kai run code_generation --github -t 0.7 --no-seed -n 20
kai run summarization --github -t 0.7 --no-seed -n 20
# Analyze results and generate figures
kai analyze results/json_extraction
# Run simulated experiments (fully reproducible, no API key)
pip install -e ".[dev]"
python -m kai.run_experimentssrc/kai/
├── __init__.py # Package root
├── model.py # Formal model: Artifact, PipelineStage, DeterminismLevel
├── pipeline.py # Pipeline engine with convergence loop
├── stages.py # Concrete stages: LLM generation, normalization, validation
├── simulator.py # Simulated LLM stages for reproducible experiments
├── experiment.py # Experiment runner (N runs, trace collection)
├── experiments.py # Pre-built experiment configs (real API)
├── run_experiments.py # Main script: runs all experiments with simulator
├── analysis.py # Metrics computation and figure generation
└── cli.py # CLI entrypoint
paper/
└── paper.tex # Full research paper (LaTeX)
After running kai analyze, the results directory contains:
fig_hash_frequency_raw.png— Bar chart of raw output hash frequencyfig_hash_frequency_semantic.png— Bar chart of semantic hash frequencyfig_determinism_comparison.png— Strong vs. weak determinism ratesfig_convergence_iterations.png— Distribution of convergence loop iterationsfig_cumulative_variance.png— Cumulative unique outputs over runs
- Coding agents (GitHub Copilot, Cursor, internal tools) need deterministic outputs for reproducible builds and test stability
- Bridges theory and real systems — formalizes what practitioners do informally
- Strong fit for industry conferences (ICSE, ESEC/FSE, ASE) and ML systems venues (MLSys, NeurIPS Systems)
The full research paper is in paper/paper.tex. It includes:
- Formal model with definitions of strong/weak determinism
- Taxonomy of nondeterminism sources (5 categories)
- Deterministic convergence loop algorithm with convergence proof
- Experimental evaluation on real LLM (GPT-4.1-mini, temp=0.7)
- Simulated experiments in appendix for reproducibility
Compile with: cd paper && pdflatex paper.tex && pdflatex paper.tex
MIT