Tutorial: Code Optimization¶
This tutorial walks a beginner end-to-end through running FermiLink’s benchmark-gated code optimizer on a scientific package (Python, C, C++, or Fortran). It is self-contained: you can follow it from a fresh shell without reading other pages.
fermilink optimize iteratively proposes source-level changes to a
target package, benchmarks each candidate, validates correctness, and
accepts only changes that improve measured performance while
preserving results. You describe your intent in a short structured
markdown file (goal.md); FermiLink will perform the optimization and return a branch with the accepted commits.
For general-purpose HPC simulation workflows (exec, loop,
research, reproduce), see Tutorial: HPC Simulations.
Prerequisites¶
You need the following available locally (all can be user-local, no sudo required):
Python
>= 3.11gitonPATH(optimize campaigns run inside a git worktree)Node.js +
npm(for the Codex agent CLI; other provider CLIs may use their own installers)A C / C++ / Fortran toolchain if the package you want to optimize is compiled (e.g.
gcc,g++,gfortran,cmake,make)
This tutorial assumes you will use the OpenAI Codex CLI as the
agent backend, since it currently gives the strongest optimization
results with gpt-5.4 at xhigh reasoning effort.
Step 1. Install the Codex CLI and authenticate¶
Install the Codex CLI globally (or user-locally via npm --prefix)
and log in once:
npm i -g @openai/codex
codex login
After codex login completes you should be able to run codex
interactively without being prompted for credentials.
Note
FermiLink also supports Claude, Gemini, and OpenCode agents. For optimization we recommend Codex because of its superior code-editing and profiling behavior at high reasoning effort as well as its relative affordability. If you prefer a different provider, see Choosing an AI Agent.
Step 2. Install FermiLink¶
Install FermiLink into the Python environment you want to drive the optimizer from. Note that the Python launcher in Step 4 creates a separate per-campaign venv for the target package, so this environment only needs FermiLink itself.
pip install fermilink
This installs the fermilink CLI and the two opinionated optimize
launchers (fermilink-optimize-python and
fermilink-optimize-cpp) onto your PATH.
Step 3. Set the agent runtime policy¶
Optimization agents need to compile code, run benchmarks, and profile hot paths, so the default restricted sandbox is too tight. Switch the Codex agent to bypass-sandbox mode with the most capable model and highest reasoning effort:
fermilink agent codex --bypass-sandbox \
--model gpt-5.4 \
--reasoning-effort xhigh
# OpenCode is also supported when you prefer OpenCode-managed profiles
fermilink agent opencode --bypass-sandbox \
--model openai/gpt-5.5 \
--reasoning-effort xhigh
# verify what is active
fermilink agent --json
This mirrors the recommended HPC configuration in Tutorial: HPC Simulations.
xhigh reasoning effort is important for optimization: the agent
needs deep deliberation to reason about correctness constraints while
restructuring hot paths.
Warning
Bypassing the sandbox lets the agent execute arbitrary shell commands in your user account. Never run this as root. Use a dedicated non-root account and keep regular backups. Also run campaigns on a clean clone of the target package so the original checkout stays untouched (the launchers in Step 4 enforce this via a sibling git worktree).
Step 4. Write a goal.md file¶
The goal.md file is the only input you author by hand. It is a
short structured markdown file describing what to optimize and
what invariants must hold. FermiLink converts it into a
deterministic benchmark contract automatically.
For the full authoring guide, see Goal files for code optimization.
Also view the example goal files at Project Optimization for inspiration – these are all real goals that have driven successful campaigns on open-source scientific packages.
A minimal goal file looks like this:
# Optimization Goal
## Package
pyscf
## Language
python
## Target
Optimize DIIS behavior for SCF convergence in PySCF, focusing on
reduced overhead and faster convergence paths.
## Editable Scope
- pyscf/lib/diis.py
- pyscf/scf/**
## Performance Metric
Minimize end-to-end SCF kernel time from `mf.kernel()`.
## Correctness Constraints
- Total SCF energy absolute delta <= 5e-8 Hartree vs baseline
- All benchmark cases must converge within baseline cycle limits
## Representative Workloads
- train-rhf-benzene: benzene / RHF / 6-31g** / diis_space=12
- train-uhf-allyl: allyl radical / UHF / spin=1 / def2-TZVP
- test-rhf-c3h7oh: C3H7OH / RHF / 6-31g**
## Build
```bash
pip install -e .
```
## Notes
Keep benchmark behavior deterministic with pinned thread counts.
Save this file anywhere – e.g. ~/goals/pyscf-diis.md.
Tip
If you are new to writing optimization goals, ask FermiLink
itself. From any directory, run fermilink init then codex
(or claude, gemini, or opencode)
and tell the agent: “Use the optimize-goal-authoring skill to
write a well-structured optimization goal for <routine> in
<package>.” See Goal files for code optimization for the full prompt.
Step 5. Launch your first campaign¶
The easiest way to start a campaign is the shipped launcher matching
your language. The launchers handle all the plumbing, including creating a
sibling git worktree and excluding fermilink optimize runtime data from git tracking.
Python packages (PySCF, ASE, PyTorch-based solvers, …):
cd /path/to/pyscf # your clean clone of the target package
fermilink-optimize-python ~/goals/pyscf-diis.md --branch fermilink-optimize/pyscf-diis
C / C++ / Fortran packages (LAMMPS, Quantum ESPRESSO, GROMACS, …):
cd /path/to/lammps
fermilink-optimize-cpp ~/goals/lammps-tip4p.md --branch fermilink-optimize/lammps-tip4p
That is all. The launcher:
Creates a sibling git worktree next to the source repo so your original checkout stays untouched.
Names the worktree branch
fermilink-optimize/<repo>-<task>(derived from the goal file name).(Python launcher only) Creates a per-branch venv under
<repo-parent>/venvs/andpip install fermilinkinto it.Calls
fermilink optimize <goal.md>inside the worktree.
Progress streams live to your terminal. You can check the current optimization status at any time from another shell:
fermilink optimize status
Note
If you are not already inside a git repo, pass
--project-root /path/to/clean/clone instead of cding
first. See the detailed launcher options section below.
Step 6. What happens during a run¶
Understanding the two internal stages helps you read the log stream and intervene if something looks off.
Goal-mode pipeline (one-time setup)
When you submit a goal.md for the first time, FermiLink runs a
two-phase generation pipeline before starting the optimization
loop:
Source analysis: an agent reads your target source code and produces a structured JSON analysis of the package and correctness boundaries.
Benchmark generation: a second agent writes a
benchmark.yamldeterministic contract and abenchmark_runner.pydriver script, which are validated and placed under.fermilink-optimize/autogen/. A copy of your inputgoal.mdis preserved alongside them.
Once generation is done the campaign switches to the standard optimization loop.
Campaign lifecycle (repeated until convergence or iteration cap)
Baseline: the generated benchmark suite runs on the unmodified source to establish incumbent performance.
Worker turn: an AI worker agent proposes a single candidate source-level change inside an isolated git worktree.
Benchmark: the candidate is benchmarked.
Controller turn: a separate controller agent evaluates correctness (against your
## Correctness Constraints) and performance, then accepts or rejects the candidate.Iterate: repeat from step 2 until the iteration cap, the consecutive-rejection limit, or
--forevertermination.
Runtime data for fermilink optimize is persisted under .fermilink-optimize/.
Step 7. Resume from an interrupted run¶
Long campaigns occasionally get interrupted. You can
resume exactly where you left off by passing --resume through to
the launcher:
# Python
fermilink-optimize-python --goal ~/goals/pyscf-diis.md --branch fermilink-optimize/pyscf-diis \
-- --resume
# C/C++/Fortran
fermilink-optimize-cpp --goal ~/goals/lammps-tip4p.md --branch fermilink-optimize/lammps-tip4p \
-- --resume
Anything after -- is forwarded verbatim to the underlying
fermilink optimize call, so -- --resume means “add
--resume to fermilink optimize”.
What --resume does:
Reuses the existing sibling worktree and branch (does not recreate them).
Reuses the already-generated
benchmark.yamland runner under.fermilink-optimize/autogen/– the expensive two-phase goal pipeline is skipped.Reloads controller and worker memories and continues the iteration loop from the last persisted state.
Preserves accepted commits already in the branch history.
You can combine --resume with other flags, e.g. extending the
iteration cap or the per-run benchmark timeout:
fermilink-optimize-cpp --goal ~/goals/lammps-tip4p.md --branch fermilink-optimize/lammps-tip4p \
-- --resume --max-iterations 60 --timeout-seconds 6000
Step 8. Power user: tune the generated benchmark.yaml¶
After the first goal-mode run, FermiLink has written a fully deterministic benchmark contract under:
.fermilink-optimize/autogen/
├── goal.md # copy of your input
├── benchmark.yaml # generated deterministic contract
└── benchmark_runner.py # generated driver script
This benchmark.yaml is what actually drives the optimization loop.
For more deterministic behavior, FermiLink does not re-read ``goal.md`` during iterations.
That means once you are happy with the auto-generated contract (or want to hand-tune it), you can skip goal mode entirely and drive the optimizer directly in expert mode:
fermilink optimize <package_id> <project_path> \
--benchmark .fermilink-optimize/autogen/benchmark.yaml \
--resume
Why you might want to tune the YAML by hand:
Target section: add or remove editable file globs to narrow the agent’s scope.
Correctness tolerances: tighten or loosen numerical tolerances in
field_tolerancesbased on what you observed in the baseline run.Runtime hooks: edit
runtime.pre_commandsto add a custom rebuild recipe of the package.Per-run timeout: change
runtime.timeout_secondsif some cases are outliers.
The rule of thumb: use goal.md to bootstrap the contract, then
treat benchmark.yaml as the source of truth for further
iterations.
Step 9. Check campaign progress¶
At any time, inside the modified repo, inspect the campaign state:
fermilink optimize status
Troubleshooting quick checks¶
Dirty working tree refused: commit or stash your changes, or pass
--allow-dirty-baseto the launcher if you know what you are doing.Every candidate rejected on correctness: your
## Correctness Constraintsare likely too tight. See Writing a goal.md for physically meaningful tolerance guidance.Benchmark always times out: bump
--timeout-seconds, or pick smallertrain-workloads. The optimizer never sees thetest-cases during iteration.Resume does nothing: make sure you are in the same worktree (the sibling one created by the launcher), not the original source checkout.
Detailed reference: launcher and fermilink optimize options¶
The Step 5 and Step 7 commands above cover 95% of day-to-day usage. The remaining sections document every flag for the moments you need fine-grained control.
Common launcher options¶
Shared between fermilink-optimize-python and
fermilink-optimize-cpp.
Flag |
Description |
|---|---|
|
Goal markdown file (required). |
|
Clean git repo to optimize. Defaults to the current working git repo root. |
|
Worktree branch name. Default |
|
Base ref used when creating a new branch. Defaults to
|
|
Parent dir for generated worktrees. Default
|
|
Explicit worktree directory name. Default
|
|
HPC profile JSON forwarded to |
|
Override the worker-agent provider (e.g. |
|
Override the worker-agent model. Inherits sandbox/reasoning from the active agent policy. |
|
Run with a campaign-local |
|
Explicit |
|
Which |
|
Allow uncommitted changes in |
|
Print the resolved |
|
Show the launcher help. |
Python-launcher-only options¶
fermilink-optimize-python additionally provisions a per-branch
virtualenv with pip install fermilink inside it:
Flag |
Description |
|---|---|
|
Python executable used to create the venv. Default
|
|
Parent dir for per-branch venvs. Default
|
|
Override the default venv directory name. |
|
Explicit venv path (overrides |
Extra optimize arguments (after --)¶
Everything after -- on the launcher command line is forwarded
verbatim to fermilink optimize. These are the flags you most
often want to pass through.
Flag |
Description |
|---|---|
|
Run only the baseline benchmark and exit. Useful for sanity checks. |
|
Initialize state and validate inputs without running the loop. |
|
Resume an existing campaign from local state (see Step 7). |
|
Override the campaign iteration cap. |
|
Override the per-turn worker iteration cap. |
|
Override the rejection-based early-stop threshold. |
|
Override the per-run benchmark timeout. |
|
HPC profile for SLURM job submission. |
|
Run indefinitely until interrupted. |
|
Allow startup from a dirty git working tree. |
|
Provider sandbox override for optimize agent turns. |
Expert mode: run directly from benchmark.yaml¶
Once you have a benchmark.yaml (either hand-written or generated
by goal mode as shown in Step 8), you can skip the launchers and the
goal pipeline entirely:
fermilink optimize <package_id> <project_path> \
--benchmark <path/to/benchmark.yaml> \
[--branch NAME] \
[--resume] \
[--max-iterations N] \
[--timeout-seconds N] \
[--hpc-profile PATH]
Further reading¶
Writing a goal.md – full authoring guide for
goal.mdfiles, including every required section, tolerance conventions, and domain-specific examples.Tutorial: HPC Simulations – how to wire
--hpc-profilefor SLURM so optimize benchmarks run on a cluster instead of your laptop.Choosing an AI Agent – picking between Codex, Claude, Gemini, and OpenCode as the worker or controller agent.
Advanced Configuration –
FERMILINK_HOMEand related environment variables.recompile: Updating Package Skills – reusable pipelines and memory-driven skill updates.