Hierarchical MINLP-controlled LLM code generation and repair. Six-module pipeline · Budget-constrained selection · MINLP formulation in Julia/JuMP · Streamlit viz dashboard
OptiCode-Agent frames LLM code generation as a Mixed-Integer Nonlinear Program. Given a coding task and hard budget constraints (latency, cost, compute), the agent generates multiple candidate solutions, verifies them, predicts their quality attributes, and uses a MINLP controller to select the optimal candidate — not by heuristic retry, but by solving an optimization problem.
Core idea: treat code generation as a resource allocation problem. The controller maximizes a weighted utility Φ over accuracy, latency, cost, compute, and uncertainty subject to hard resource budgets.
Natural-language task + budgets
│
▼
┌─────────────────────────┐
│ A. Task Parser │ NL + starter code + tests → TaskSpec
└──────────┬──────────────┘
│
▼
┌─────────────────────────┐
│ B. Candidate Generator │ Multi-prompt → N candidates via Grok API
└──────────┬──────────────┘
│
▼
┌─────────────────────────┐
│ C. Verifier Stack │ Syntax · Compile · Lint · Unit tests
│ │ Hidden-test proxy · Profiling · Safety
└──────────┬──────────────┘
│
▼
┌─────────────────────────┐
│ D. Meta-Predictor │ A · L · C · Q · U · H · X · G · V_syn
│ │ (accuracy, latency, cost, compute,
│ │ uncertainty, robustness, complexity,
│ │ repair gain, syntax validity)
└──────────┬──────────────┘
│
▼
┌─────────────────────────┐
│ E. MINLP Controller │ Maximize Φ = α·A + β·H − γ·L − δ·C
│ │ − η·Q − μ·U
│ │ s.t. L ≤ L_max, C ≤ C_max, A ≥ A_min
│ │ Nonlinear state: L ∝ log(1+Σy), A ∝ −λ(Σy)²
└──────────┬──────────────┘
│
▼
┌─────────────────────────┐
│ F. Executor / Sandbox │ Run + repair loop up to R_max rounds
└─────────────────────────┘
The controller objective and state equations are nonlinear. Key terms:
| State variable | Nonlinear equation |
|---|---|
| A (accuracy) | a⁰ + ψ·u + θ·v + ρ·y − λ₁·(Σy)² + λ₂·synergy |
| L (latency) | l_gen + l_ver + l_rep + λ₃·log(1 + Σy) |
| C (cost) | c_gen + c_ver + c_rep + λ₄·e |
| Q (compute) | q_gen + q_ver + q_rep + λ₅·(Σy)² |
| U (uncertainty) | u⁰ − λ₆·v + λ₇·y |
u = model choice, v = verification, y = repair rounds, e = escalation, Σy = total repair across candidates.
The full formulation is implemented in Julia/JuMP (julia/opticode_minlp.jl) using Juniper + Ipopt + HiGHS — solvers that handle mixed-integer nonlinear programs exactly.
| Weight | Default | Controls |
|---|---|---|
| α | 0.45 | Accuracy (highest priority) |
| β | 0.20 | Robustness |
| γ | 0.15 | Latency penalty |
| δ | 0.10 | Cost penalty |
| η | 0.05 | Compute penalty |
| μ | 0.05 | Uncertainty penalty |
opticode-agent/
├── src/opticode_agent/
│ ├── agent.py # Full pipeline orchestration
│ ├── cli.py # CLI entry point
│ ├── parser/task_parser.py # Module A: NL → TaskSpec
│ ├── generator/ # Module B: multi-prompt Grok calls
│ ├── verifier/verifier_stack.py # Module C: syntax + compile + lint + tests
│ ├── predictor/meta_predictor.py # Module D: predict A,L,C,Q,U,H,X,G
│ ├── controller/minlp_controller.py # Module E: MINLP selection
│ └── executor/sandbox.py # Module F: run + repair loop
├── julia/
│ ├── opticode_minlp.jl # Full MINLP formulation (Juniper + Ipopt + HiGHS)
│ └── Project.toml
├── app_viz.py # Streamlit dashboard
├── docs/
│ ├── CONSTRAINTS.md # All 20+ MINLP constraints documented
│ └── FORMULATION_GAP_ANALYSIS.md
└── pyproject.toml
git clone https://github.com/Ajeenckya5/opticode-agentr
cd opticode-agentr
pip install -e .
export XAI_API_KEY="your-key"
# Run on a task
opticode-agent --task "Implement binary search in Python"
# With explicit budgets
opticode-agent --task "Sort a list of integers" \
--latency-max 30 --cost-max 0.05 --accuracy-min 0.7Streamlit dashboard — visualize candidate scores, utility breakdown, and MINLP selection:
pip install -e ".[viz]"
streamlit run app_viz.pyJulia MINLP solver — run the full JuMP formulation directly:
cd julia
julia -e 'using Pkg; Pkg.activate("."); Pkg.instantiate()'
julia opticode_minlp.jlDefault provider: xAI Grok
export XAI_API_KEY="your-key" # xAI (default)
export OPENAI_API_KEY="your-key" # OpenAI-compatible fallbackController weights and MINLP λ parameters are tunable at construction:
from opticode_agent.controller import MINLPController
ctrl = MINLPController(
alpha=0.45, beta=0.20, gamma=0.15, # objective weights
latency_max=30.0, # hard budget (seconds)
cost_max=0.10, # hard budget (USD)
accuracy_min=0.6, # quality floor
)Python 3.10+ · xAI Grok API · Julia 1.10+ / JuMP · Juniper · Ipopt · HiGHS · Streamlit · pyflakes