Running MIPROv2 on production traces
How route-switch closes the loop: replaying captured traces to score instruction and few-shot candidates with goptuna, then shipping the winner.
route-switch implements MIPROv2 — a joint search over instructions and few-shot demonstrations — and runs it against the traffic the gateway already captured. This is the closed loop that distinguishes route-switch from a plain routing layer.
The loop, concretely
- Bootstrap a calibration sample from the prompt’s SQLite dataset.
- Generate instruction candidates by calling the configured provider with an instruction-proposal meta-prompt.
- Search — drive a Bayesian optimization (goptuna) over (instruction, few-shot demo) combinations.
- Score each candidate by replaying dataset rows under an
evaluation strategy:
Similarity,ExactMatch,KeywordMatch, or a custom Go implementation. - Write the winning prompt back to the registry.
Running it
gateway:
optimization:
enabled: true
interval: 6h # background cadence
Or trigger a one-off run from the CLI with --optimize-prompt. Either way, optimization is
per-prompt and per-(prompt, model) — it does not touch other templates.
Honesty about the loss function
The optimizer scores against your captured traffic with the strategy you configured. If
Similarity (token overlap), ExactMatch (trimmed equality), and KeywordMatch (declared-keyword
presence) don’t capture what “good” means for your task, implement the EvaluationStrategy
interface — otherwise you’ll optimize toward the wrong target.
It reliably moves format adherence, demo selection, and instruction specificity. It will not fix a task the model can’t do, a mismatched scorer, or a non-representative dataset. Set up trace capture first, and see prompt optimization from traces for the end-to-end story.
Frequently asked questions
Is this RLHF or fine-tuning?
No. MIPROv2 rewrites the prompt template — instructions and few-shot demonstrations — scored against your captured data. No model weights are trained.
What does the optimizer reliably improve?
Format adherence, demonstration selection, and instruction specificity. It cannot make a model do a task it fundamentally can't, fix a mismatched score function, or rescue a non-representative dataset.
Related
Choosing a routing strategy
When to use round_robin, weighted, or performance_based routing across registered prompt+model+provider combinations — and how fallback fits in.
GuideCapturing traces to DuckDB
How route-switch logs every call to a per-prompt SQLite dataset and a DuckDB analytics store, and how to read per-prompt cost, latency, and success rate.
GuideDrop-in OpenAI base_url swap
Migrate an existing OpenAI client to route-switch by changing one field — the base URL — without rewriting request code or prompts.