Why this lab exists
Every LLM application you’ve built has a prompt. You probably iterated on that prompt by hand — tweaking the instructions, adding examples, adjusting tone. That manual iteration is prompt engineering. DSPy’s core insight is that prompt engineering can be automated: given a metric (a function that says whether the output is correct), you can search over prompt configurations programmatically and pick the best one.
You’ve read about DSPy. You know the terms: Signature, Module, Teleprompter. But until you’ve implemented a Signature that decouples what the task is from how to prompt for it, until you’ve run a bootstrapper that automatically generates few-shot examples by filtering your training data with a metric, until you’ve watched random search improve accuracy by 15 percentage points using 50 LLM calls — you don’t know it.
This lab builds a prompt optimization framework in three stages. Each stage implements a concrete improvement in prompt quality, measured on a toy task. The architecture directly mirrors DSPy’s design: Signature separates the task interface from the prompt format; Module is the executable unit; Bootstrapper generates demonstrations automatically; Teleprompter is the optimizer that compiles modules against a dataset.
What we’re building
A Python prompt optimization framework implemented across three stages. Each stage adds one optimization layer.
We build:
Signature(v0): defines input fields, output fields, and the task instruction — the what, not the howModule(v0): turns a Signature into a runnable prompt-and-parse unit;call_llmis injectable for testabilityChainOfThought(v0): Module subclass that prepends step-by-step reasoning before output fieldsPipeline(v0): chains multiple Modules — output of one feeds into the nextExampleandMetric(v1): labeled training examples and a callable that evaluates predictionsBootstrapper(v1): runs the module on training data, keeps examples where the metric passes, returns demonstrationsFewShotModule(v1): Module subclass that prepends bootstrapped demos to each promptOptimizeInstructions(v2): evaluates candidate instructions on a val subset, selects the bestBootstrapFewShotWithRandomSearch(v2): random search over demo configurations, keeps highest val accuracyPromptHistory(v2): records each trial for inspection- FastAPI server:
POST /compile,POST /forward,GET /history - Spring AI Java integration showing
PromptTemplaterendering vs our optimization
We explicitly skip:
- A real LLM —
call_llmis injected, so all tests run with a mock function; no API key required - Gradient-based optimization (soft prompts) — requires model internals; covered in What the Toy Misses
- Multi-objective optimization (accuracy + cost) — covered in What the Toy Misses
- Persistent prompt registry — our history is in-memory; production uses a model registry
Mental model
DSPy thinks about prompts differently from most frameworks. Instead of writing prompt strings directly, you declare a Signature that says “given a question, produce an answer with these instructions.” The Module then renders that signature into a prompt string. The key insight: the signature is the stable specification; the prompt format is an implementation detail that can be optimized.
v0 — Zero-Shot: Signature describes task; Module renders + calls LLM + parses
Signature(inputs=["question"], outputs=["answer"], instructions="Answer concisely")
|
v
Module.forward(question="What is 2+2?")
|
v
build_prompt():
"Answer concisely.\n\nquestion: What is 2+2?\nanswer:"
|
v
call_llm(prompt) -> "answer: 4"
|
v
parse_output() -> {"answer": "4"}
v1 — Few-Shot: Bootstrapper collects demos; FewShotModule prepends them
Bootstrapper.bootstrap(module, trainset, metric)
- runs module on 20 training examples
- keeps the 14-16 where metric(example, prediction) == True
- returns 5 best as demonstrations
FewShotModule.forward(question="What is 5+5?")
- prompt now includes 5 worked examples before the query
- LLM sees examples of correct behavior -> more likely to follow pattern
v2 — Teleprompter: random search over (instruction, demos) configurations
BootstrapFewShotWithRandomSearch:
for trial in range(10):
demos = random.sample(demo_pool, 5)
candidate.set_demos(demos)
val_accuracy = evaluate(candidate, valset, metric)
if val_accuracy > best: keep this config
-> best_module with highest val accuracy
The three layers address three different sources of prompt quality:
- Instruction quality — what you tell the LLM to do;
OptimizeInstructionssearches over candidates - Example quality — which demonstrations to show;
Bootstrapperfilters by metric, not by hand-picking - Configuration search — which combination of examples works best;
RandomSearchexplores efficiently
Prerequisites
- Python 3.12
- Understanding of Python dataclasses, callables, and type hints
- Basic LLM prompting knowledge — what a prompt is, what few-shot examples do
- No LLM API key required — all modules accept an injectable
llm_fnmock
Stage-by-stage build
Stage 0 Signature + Zero-Shot Module
- Implement Signature(inputs, outputs, instructions) with field validation
- Implement Module.build_prompt(inputs) — format fields as 'FieldName: value'
- Implement Module.call_llm(prompt) — calls injected llm_fn exactly once
- Implement Module.parse_output(response) — extract output fields from response
- Implement ChainOfThought subclass — adds 'reasoning: Let\'s think step by step.' before outputs
- Implement Pipeline — chains modules, carries context through
The Signature is the core abstraction that makes the rest of the framework possible. By separating the task interface (what fields are needed, what the instructions are) from the prompt format (how those fields are formatted into a string), you can optimize the prompt format independently of the task specification.
Why Signature matters:
In a naive implementation, you write prompt strings directly:
prompt = f"Answer concisely.\n\nQuestion: {question}\nAnswer:"The instruction (“Answer concisely”) is embedded in the string. To try a different instruction, you edit the string. To add few-shot examples, you modify the string again. The prompt format and the task specification are tangled.
With a Signature:
sig = Signature(inputs=["question"], outputs=["answer"], instructions="Answer concisely.")
module = Module(sig)
module.forward(question="What is 2+2?")The instruction lives in sig.instructions. To try a different instruction, you create a new Signature. The Module renders whatever Signature it receives — the same Module code works for any task.
The build_prompt format:
Answer concisely.
question: What is 2+2?
answer:Input fields are formatted as FieldName: value. Output fields are formatted as FieldName: with an empty value — the LLM fills them in. This format is readable, parseable, and consistent across all modules.
parse_output in detail:
The LLM response is free text. parse_output looks for lines that start with the output field name followed by a colon:
response = "answer: 4"
# parse_output finds "answer:" prefix -> extracts "4"
result = {"answer": "4"}If the field label is not found (the LLM ignored the format), the full response is used as the value of the first output field. This graceful fallback prevents hard failures on format violations.
ChainOfThought adds one line:
question: What is the capital of France?
reasoning: Let's think step by step.
answer:The reasoning: line is placed between the input fields and the output fields. The LLM reads this as an instruction to reason before answering. In practice, this single line improves accuracy on multi-step questions by 5-15 percentage points — at zero cost beyond the extra tokens.
The test that matters most: test_chain_of_thought_adds_reasoning_prefix verifies that “reasoning: Let’s think step by step.” appears in the prompt before the output field. If the order is wrong, the LLM sees the output label before the reasoning instruction — the CoT effect disappears.
Stage 1 Bootstrapped Few-Shot Compilation
- Implement Example(inputs, outputs, label) dataclass
- Implement exact_match_metric(example, prediction) -> bool
- Implement Bootstrapper.bootstrap(module, trainset, metric, n_demos) -> list[Example]
- Implement FewShotModule.compile(trainset, metric) — calls Bootstrapper, stores demos
- Implement FewShotModule.build_prompt — prepends demos as 'Example N' blocks
- Implement evaluate(module, evalset, metric) -> EvaluationResult
The key question for few-shot prompting is: which examples should you show the LLM? Manual selection is expensive and often intuition-driven. Bootstrapping automates it: run the module on training data, keep examples where the metric passes, use those as demonstrations.
Why bootstrap instead of using gold labels?
When the bootstrapper collects demonstrations, it uses the module’s actual output — not the gold label from the training set. This is a subtle but important distinction.
If a training example has gold = {"answer": "4"} and the module predicts {"answer": "4"} (correct), the demo stores the module’s prediction. This ensures the demonstrations reflect what this particular module can produce. Demos that show gold labels the module never actually generates create a distribution mismatch — the LLM is trained to imitate outputs it couldn’t produce in zero-shot.
The 70-80% success rate:
On a well-scoped classification task, a zero-shot module correctly answers 70-80% of training examples before any optimization. This means: from 20 training examples, expect 14-16 successful bootstrapped demonstrations. The 5 best form the demo pool for FewShotModule.
build_prompt with demos:
Answer concisely.
--- Example 1 ---
question: What is 1+1?
answer: 2
--- Example 2 ---
question: What is 3+3?
answer: 6
--- Your turn ---
question: What is 5+5?
answer:The LLM sees N worked examples before the actual query. The examples demonstrate the expected format (field labels, one answer per line) and the expected quality (correct answers from the metric perspective). The --- Your turn --- separator is optional polish; the examples’ presence is what drives quality improvement.
evaluate() returns structured results:
result = evaluate(module, evalset, metric)
# result.accuracy = 0.75 (fraction correct)
# result.total = 20
# result.correct = 15
# result.examples = [(example, prediction, passed), ...]The per-example breakdown is essential for debugging: when accuracy is low, you need to see which examples failed and what the module predicted. result.examples provides this without requiring a second evaluation pass.
The test that validates everything: test_compiled_module_outperforms_zero_shot. A biased mock LLM returns the correct answer only when it sees at least one example in the prompt (checking for “Example 1”). Zero-shot returns wrong answers; few-shot returns correct ones. The test verifies that few_result.accuracy > zero_result.accuracy — the entire point of compilation.
Stage 2 Teleprompter + Instruction Optimization
- Implement BudgetExceededError — raised when max_llm_calls is exceeded
- Implement PromptHistory(iteration, instruction, demos, val_accuracy, llm_calls)
- Implement OptimizeInstructions.compile(signature, llm_fn, valset) — evaluates each candidate
- Implement BootstrapFewShotWithRandomSearch.compile(module, trainset, valset, metric)
- Wire budget enforcement: _CallCounter wraps llm_fn, raises BudgetExceededError on overflow
The Teleprompter is DSPy’s term for any optimizer that compiles a Module against a dataset. This stage implements two: instruction optimization and random search over demo configurations.
Why random search works:
The demo pool from bootstrapping contains 14-16 successful examples. We want 5 demos. The number of ways to choose 5 from 15 is C(15,5) = 3,003 configurations. Exhaustive search is impractical if each evaluation costs LLM calls. Random search with 10 trials samples 10 configurations — approximately 0.3% of the space. In practice, the improvement from demos is relatively insensitive to which specific 5 you pick (any 5 correct examples help similarly), so 10 trials reliably finds a good configuration.
Budget enforcement:
The _CallCounter wrapper is the key piece:
class _CallCounter:
def __call__(self, prompt: str) -> str:
self.count += 1
if self.count > self._max:
raise BudgetExceededError(self.count, self._max)
return self._fn(prompt)The counter wraps the actual llm_fn. Every call through _CallCounter increments the count. When it exceeds max_llm_calls, it raises before forwarding the call — preventing budget overruns entirely. This is the correct abstraction: the budget is enforced at the infrastructure layer, not sprinkled through the optimization logic.
Instruction optimization cost:
OptimizeInstructions evaluates len(candidate_instructions) different instructions, each on val_subset_size examples. Total cost: num_instructions * val_subset_size LLM calls. With 3 instructions and 5 val examples: 15 LLM calls to pick the best instruction. This is remarkably cheap for the quality gain — the right instruction often improves accuracy by 10-20 percentage points.
Random search cost:
BootstrapFewShotWithRandomSearch runs num_trials trials, each evaluating on the full valset. Total cost: num_trials * len(valset) LLM calls. With 10 trials and 5 val examples: 50 LLM calls. Plus the bootstrapping pass: 20 more calls. Total: 70 LLM calls to find an optimized configuration. Compare to fine-tuning, which requires hundreds of training examples and GPU compute.
PromptHistory for transparency:
history[2] = PromptHistory(
iteration=2,
instruction="Answer correctly",
demos=[...], # the 5 demos used in this trial
val_accuracy=0.80, # accuracy on the val subset
llm_calls=5, # calls consumed by this trial
)The history answers the question “how did we arrive at this prompt?” It shows which instruction and demo combination produced the best accuracy, making the optimization process auditable and reproducible.
Run it yourself
labs/prompt-optimization ↗
Python 3.12+pip or uv - 1 Clone and install dependencies
git clone https://github.com/pushkarkumar-dev/system-design-labs cd system-design-labs/labs/prompt-optimization pip install -e '.[test]' - 2 Run tests (no LLM API key needed — uses mock)
python -m pytest tests/test_prompt_opt.py -v - 3 Try the zero-shot module directly
python -c " from src.v0_signature import Signature, Module sig = Signature(inputs=['question'], outputs=['answer'], instructions='Answer concisely.') mod = Module(sig, llm_fn=lambda p: 'answer: 42') print(mod.forward(question='What is 6 x 7?')) print('Prompt length:', len(mod.build_prompt({'question': 'What is 6 x 7?'}))) " - 4 Try bootstrapped compilation
python -c " from src.v0_signature import Signature, Module from src.v1_bootstrap import Example, FewShotModule, exact_match_metric sig = Signature(inputs=['question'], outputs=['answer'], instructions='Answer concisely.') trainset = [Example({'question': 'Q1'}, {'answer': 'yes'}) for _ in range(5)] module = FewShotModule(sig, llm_fn=lambda p: 'answer: yes') module.compile(trainset, exact_match_metric) print('Demos compiled:', len(module._demos)) print('Prompt includes demos:', 'Example 1' in module.build_prompt({'question': 'test'})) " - 5 Start the FastAPI server
uvicorn src.server:app --port 8000 - 6 Compile a module via the API
curl -s -X POST http://localhost:8000/compile \ -H 'Content-Type: application/json' \ -d '{"signature":{"inputs":["question"],"outputs":["answer"],"instructions":"Answer concisely."},"trainset":[{"inputs":{"question":"Q1"},"outputs":{"answer":"x"}}],"valset":[{"inputs":{"question":"V1"},"outputs":{"answer":"x"}}],"num_trials":3,"max_bootstrapped_demos":2,"max_llm_calls":500}' | python -m json.tool - 7 Run a forward pass on the compiled module
curl -s -X POST http://localhost:8000/forward \ -H 'Content-Type: application/json' \ -d '{"signature":{"inputs":["question"],"outputs":["answer"],"instructions":"Answer concisely."},"inputs":{"question":"What is 2+2?"},"use_compiled":true}' | python -m json.tool - 8 Check optimization history
curl -s http://localhost:8000/history | python -m json.tool
===== Tests (mock LLM — no API key) =====
tests/test_prompt_opt.py::TestV0Signature::test_build_prompt_includes_all_input_fields PASSED
tests/test_prompt_opt.py::TestV0Signature::test_call_llm_called_once_per_forward PASSED
tests/test_prompt_opt.py::TestV0Signature::test_parse_output_extracts_fields PASSED
tests/test_prompt_opt.py::TestV0Signature::test_chain_of_thought_adds_reasoning_prefix PASSED
tests/test_prompt_opt.py::TestV0Signature::test_pipeline_chains_modules PASSED
tests/test_prompt_opt.py::TestV0Signature::test_signature_field_order_preserved PASSED
tests/test_prompt_opt.py::TestV1Bootstrap::test_bootstrapper_selects_correct_examples PASSED
tests/test_prompt_opt.py::TestV1Bootstrap::test_few_shot_prompt_includes_demos PASSED
tests/test_prompt_opt.py::TestV1Bootstrap::test_compiled_module_outperforms_zero_shot PASSED
tests/test_prompt_opt.py::TestV1Bootstrap::test_evaluate_returns_correct_accuracy PASSED
tests/test_prompt_opt.py::TestV2Teleprompter::test_instruction_optimization_selects_best PASSED
tests/test_prompt_opt.py::TestV2Teleprompter::test_random_search_improves_over_baseline PASSED
tests/test_prompt_opt.py::TestV2Teleprompter::test_budget_exceeded_raises_error PASSED
tests/test_prompt_opt.py::TestV2Teleprompter::test_prompt_history_records_all_trials PASSED
14 passed in 0.31s
===== Zero-shot forward =====
{'answer': '42'}
Prompt length: 56 Benchmarks
M2 MacBook Pro, 16GB RAM | Metric | Result | Notes |
|---|---|---|
| Prompt build overhead | 850,000 builds/sec | estimated — string format operations |
| Bootstrap compilation (20 training examples) | 20 LLM calls | one call per training example to filter candidates |
| Random search (10 trials x 5 examples) | 50 LLM calls | estimated — one per validation example per trial |
| Quality improvement (5-shot vs 0-shot) | 15 pp accuracy | estimated — typical DSPy improvement on structured tasks |
Prompt building is CPU-bound string formatting — 850,000 builds/sec is the baseline throughput before any LLM calls. The LLM call count dominates the optimization cost: bootstrapping 20 examples costs 20 calls; random search with 10 trials over a 5-example valset costs 50 more. Total: 70 LLM calls to compile a module. At GPT-4o-mini pricing (~$0.00015 per 1K input tokens), compiling on a 200-token prompt costs roughly $0.002 — two-tenths of a cent per compiled module.
The 15 percentage point accuracy improvement is the headline metric from DSPy’s paper on structured tasks. On simpler tasks (binary classification with clear labels), the improvement is 10-20 pp. On harder tasks (open-ended generation), the improvement is less predictable.
Stress tests
Bootstrapper with a noisy metric: set the metric to return True with 50% probability regardless of correctness (a coin flip). The bootstrapper should still return n_demos examples — it doesn’t evaluate quality, only whether the metric function returns True. This tests that the bootstrapper’s filtering logic is correct under a random metric.
FewShotModule with 0 demos: compile a module on an empty trainset. FewShotModule.build_prompt should produce the same prompt as the base Module — the ”--- Your turn ---” header should not appear when there are no demos, and the output should be identical to zero-shot.
Pipeline with 10 modules: chain 10 modules where each outputs one field consumed by the next. Verify the final context dict contains all 10 output fields and that call_llm was called exactly 10 times (once per module). Memory usage should grow linearly with pipeline depth, not exponentially.
Budget enforcement under concurrent evaluation: run BootstrapFewShotWithRandomSearch with max_llm_calls=25 on a valset of 5 examples and num_trials=10. The budget is exhausted after 5 trials (25 calls). Verify that BudgetExceededError is raised and that the history contains exactly 5 completed trials — no partial trial should be recorded.
Instruction optimization with identical candidates: pass 5 identical candidate instructions. All 5 should score the same accuracy. The returned module should use the first candidate (first one evaluated becomes the initial best). History should contain 5 entries with equal val_accuracy.
What the toy misses
- Gradient-based prompt optimization (soft prompts) Prefix Tuning and P-Tuning learn continuous prompt embeddings via gradient descent — not discrete text. These soft prompts are not human-readable but often outperform hand-crafted prompts. Requires access to model gradients — impossible with black-box APIs like OpenAI or Anthropic.
- Multi-objective optimization Real applications optimize for accuracy AND cost (fewer tokens = cheaper) AND latency. Pareto-optimal prompt finding requires multi-objective search — our greedy accuracy maximization ignores cost. A prompt with 20 demos might score 85% accuracy but cost 10x more than a 3-demo prompt scoring 80%.
- Prompt robustness testing Optimal prompts on a val set often overfit — they trigger biases specific to the validation examples. Robustness requires testing on out-of-distribution inputs and paraphrased questions. Our framework has no holdout test set separate from the val set.
- Cross-model prompt transfer A prompt optimized for GPT-4 may not work on Claude or Gemini. DSPy's compiled programs are partially model-specific because few-shot examples exploit each model's training distribution. Transfer requires re-compilation or model-specific calibration.
- Prompt versioning and A/B testing Production prompt optimization requires rolling out new prompts to a fraction of traffic, measuring real-world performance, and deciding when to fully deploy. Our in-memory PromptHistory doesn't integrate with feature flags or traffic splitting infrastructure.
- Chain optimization across modules Optimizing a Pipeline (chain of Modules) is harder than optimizing one Module. Changing demo selection in Module 1 can affect Module 2's inputs in non-obvious ways. DSPy handles this with joint optimization using traced execution graphs — we optimize each module independently.
Wiring it into a Java/Spring service
Wiring it into a Java/Spring service
org.springframework.ai:spring-ai-openai-spring-boot-starter:1.0.0 org.springframework.boot:spring-boot-starter-web:3.3.0 labs/prompt-optimization/java-integration Clone and run: cd labs/prompt-optimization/java-integration && mvn spring-boot:run Part A — PromptOptClient: the HTTP bridge
PromptOptClient.java wraps our Python FastAPI server with Spring Framework 6.1’s RestClient. Three operations: compile(), forward(), and getHistory().
public class PromptOptClient {
private final RestClient http;
public CompileResponse compile(CompileRequest request) {
return http.post()
.uri("/compile")
.contentType(MediaType.APPLICATION_JSON)
.body(request)
.retrieve()
.body(CompileResponse.class);
}
public ForwardResponse forward(ForwardRequest request) {
return http.post()
.uri("/forward")
.contentType(MediaType.APPLICATION_JSON)
.body(request)
.retrieve()
.body(ForwardResponse.class);
}
}Compile once (offline), then call forward() for every production request. The compiled module’s demos are stored server-side; each /forward call automatically uses them when use_compiled=true.
Part B — Spring AI PromptTemplate: rendering vs optimization
Spring AI’s PromptTemplate renders a fixed template with variable substitution. Our framework optimizes the template itself. These are complementary concerns:
// Spring AI: renders a FIXED template
PromptTemplate template = new PromptTemplate(
"Answer concisely.\n\nquestion: {question}\nanswer:"
);
String rendered = template.render(Map.of("question", "What is 2+2?"));
// -> "Answer concisely.\n\nquestion: What is 2+2?\nanswer:"Spring AI does not choose the instruction (“Answer concisely”) — you do. Our framework searches for the best instruction:
// Our framework: SEARCHES for the best instruction
CompileRequest req = new CompileRequest(
signature, trainset, valset, 10, 5, 500
);
CompileResponse resp = client.compile(req);
// resp.bestValAccuracy() shows how the winning config scored
// GET /history shows which instruction won and whyThe production workflow:
- Run
/compileoffline (70 LLM calls) to find the best instruction and demos - Extract the winning instruction from
/history - Embed it in a
PromptTemplatefor runtime rendering (zero LLM calls per request) - For new tasks or new models: re-run
/compileto re-optimize
Part C — Spring Boot auto-configuration
PromptOptAutoConfiguration registers the client as a Spring bean:
@Bean
public PromptOptClient promptOptClient(PromptOptProperties props) {
return new PromptOptClient(props.baseUrl());
}application.yml controls the server URL:
prompt-opt:
base-url: http://localhost:8000
max-llm-calls: 500
num-trials: 10Run it
# Terminal 1: start the Python optimization server
cd labs/prompt-optimization
uvicorn src.server:app --port 8000
# Terminal 2: run the Spring Boot demo
cd labs/prompt-optimization/java-integration
mvn spring-boot:runExpected output:
=== Prompt Optimization Framework — Spring Integration Demo ===
--- Approach 1: Spring AI PromptTemplate (static rendering) ---
Spring AI's PromptTemplate renders a fixed template with variable substitution.
Template: 'Answer concisely. question: {question}'
Rendered: Answer concisely. question: What is 2+2? answer:
Note: Spring AI renders the template; it does NOT optimize it.
--- Approach 2: Our Prompt Optimization Framework (POST /compile) ---
Status: compiled
Demos selected: 3
Trials run: 5
Best val accuracy: 100%
--- POST /forward (using compiled module) ---
Input: question='What is 6+6?'
Output: {answer=12}
Prompt length: 287 chars (includes demos)
--- GET /history (optimization trial log) ---
Recorded 5 trials:
Trial 0: val_accuracy=100%, llm_calls=2, demos=2
Trial 1: val_accuracy=100%, llm_calls=2, demos=2
--- Key distinction: rendering vs optimization ---
Spring AI PromptTemplate: renders a FIXED template
Our framework: SEARCHES over candidate instructions + demo configurations
Complementary: optimize offline, render at runtime with PromptTemplate
Done. What surprised me
Where to go deeper
- DSPy paper (Khattab et al. 2023): the original framework — arxiv.org/abs/2310.03714
- Automatic Prompt Engineer (APE, 2022): LLM-generated instruction search — arxiv.org/abs/2211.01910
- Prefix Tuning (Li and Liang 2021): gradient-based soft prompt optimization — arxiv.org/abs/2101.00190
- DSPy documentation: full optimizer catalog including
MIPROv2andBootstrapFinetune— dspy.ai - Spring AI PromptTemplate docs: variable substitution and structured output — docs.spring.io/spring-ai
Related labs and questions
- Previous in AI: Agent Framework (lab
agent-framework) — agents that call tools; prompt optimization improves the prompts those agents use to decide which tool to call - Previous in AI: LLM Inference Engine (lab
llm-inference) — the inference engine that serves the LLM calls our optimizer makes - Next in AI: Local GenAI stack — running the full optimization loop locally with Ollama; no API costs
- System design question: You have 1,000 labeled examples and want to optimize a prompt for a classification task. Should you use all 1,000 for bootstrapping, or split into train/val/test? (Hint: use 600/200/200 — bootstrapping consumes training examples; validation guides search; test measures final quality on unseen data)
- System design question: At what scale does prompt optimization pay for itself? If a production prompt handles 10,000 requests/day at $0.001/request, and optimization costs 70 LLM calls at $0.0001/call, a 15pp accuracy improvement on a task where each error costs $0.10 saves $150/day — the $0.007 optimization cost pays back in under 1 second of production traffic.