Prompt Engineering for Local Language Models
Patterns and anti-patterns for gemma3, qwen, llama and similar local LLMs
Article last updated on 25 June 2026
Key insight: Local language models can do pattern matching but only limited inference. Phrase every instruction so that a first-day junior could follow it mechanically without domain expertise.
Cloud LLM vs. Local LLM
| Aspect | Cloud LLM (GPT-4, Claude) | Local LLM (gemma3:27b) |
|---|---|---|
| Implicit inference | Works | Unreliable |
| Vague instructions | Usually interpreted sensibly | Ignored or misinterpreted |
| Zero-shot instructions | Often sufficient | Need few-shot examples |
| Self-verification | Helps | Often degrades the result |
| Conditional behavior | Self-inference sufficient | Paths must be spelled out explicitly as IF-THEN |
| Format adherence | High | Reliable only with explicit templates |
IF-THEN Structuring: The most important lever
When the correct output depends on an input parameter, spell out the path explicitly as an IF-THEN rule. Local models cannot derive this decision on their own.
Before: Universal instruction
Score: 3.62 (gemma3:27b, 5 runs)
After: IF-THEN structuring
Score: 5.0 (gemma3:27b, 5 runs)
Generic template
When IF-THEN is useful
| Input parameter | IF-THEN useful? |
|---|---|
| Number of independent claims | Yes |
| Presence of figures | Yes |
| Document language | Yes |
| Number of prior art references | Yes |
| Stylistic preferences | No, better via few-shot examples |
Conditioning Trade-off
The more strongly a prompt pushes toward a specific source, the more likely the model gives up when the source is missing. The solution: spell out both paths explicitly.
| Phrasing | Source present | Source missing |
|---|---|---|
| Weak: “Use the description” | Partially uses source | Hallucinates freely |
| Strong: “Exclusively from the description” | Reliably uses source | Returns empty or minimal output |
| Conditional: “IF description present: quote from it. ELSE: own formulation” | Reliably uses source | Fallback behavior defined |
Baseline-First Test
Before any optimization, measure 3 baseline runs. If the score is already high and stable, optimization is not worth it; it can even degrade the result.
| Baseline result | Recommendation |
|---|---|
| Score ≥ 4.5 and variance < 0.3 | Hands off, prompt is good enough |
| Score ≥ 4.0 and variance ≥ 0.3 | Stabilization needed (IF-THEN, few-shot) |
| Score < 4.0 | Fundamental revision needed |
| Score < 3.0 | Rebuild prompt structure from scratch |
Score ≠ Quality
Heuristic scoring (e.g., by an evaluator LLM) can be deceptive. Always run out-of-distribution tests, i.e., use inputs that differ from the usual test set.
| What the heuristic measures | What it misses |
|---|---|
| Structural completeness | Factual correctness |
| Template conformity | Patent law plausibility |
| Keyword matching | Argumentation logic |
| Length compliance | Technical depth and nuance |
Golden References
Attorney-drafted example texts beat any heuristic. The model imitates patterns, and good patterns yield good outputs.
| Task | Golden reference |
|---|---|
| Office action response | Real response from the firm (anonymized) |
| Claim drafting | Granted claims from comparable patents |
| Figure description | Description section from published application |
| Abstract | Abstracts from the same technical field |
Golden references are embedded as few-shot examples in the prompt or used as evaluation basis for scoring.
Anti-Repetition for Continuation
During Ctrl+Space completion (continuation), local models frequently repeat the last word or sentence. An explicit instruction prevents this.
Prompt snippet
This snippet belongs in the system prompt of every continuation function. Without it, gemma3 and qwen produce repetitions in ~40% of cases.
Synchronize Stop Tokens
When the prompt uses section headers (e.g., ## Next Section), these must be added to the Ollama stop list. Otherwise the model generates beyond the desired section.
| Symptom | Cause |
|---|---|
| Model generates next section too | Section header missing from stop list |
| Output breaks off mid-sentence | Stop token too short / ambiguous |
| Empty output with new prompt versions | New header accidentally used as stop token at the start |
gemma4 Thinking Trap
gemma4 is a thinking model. For structured prompts, thinking must be disabled, but the mechanism differs from other models.
think: false as an API parameter in the Ollama request. The /no_think prefix from qwen3 does not work with gemma4.
Without think: false, gemma4 frequently produces empty or incomplete outputs with structured prompts because the thinking budget displaces the actual answer.
More on the gemma4 architecture: MoE Architecture →
Explicit knowledge over implicit
Naming a provision is not the same as supplying its content. “Argue under Art. 54, 56, 84, 123(2) EPC” assumes the model knows what each provision requires. Large cloud models usually carry that mapping in their weights; smaller local models swap novelty and inventive step, or invent a plausible-sounding requirement. Name the provision, but spell out the actual requirement next to it.
Before: citation only
After: citation + requirement
Anti-Patterns: What does not work
| Anti-pattern | Why it hurts |
|---|---|
| Self-verify (“Check your output”) | Filters out real findings instead of correcting errors |
| Role boilerplate (“You are an experienced patent attorney”) | No measurable effect with local models |
| Open avoidance rules (“Avoid repetitions”) | Ignored, concrete IF-THEN rules needed |
| Implicit style instructions (“Write professionally”) | Model knows no conventions, use few-shot examples |
| Long justifications before the task | Recency bias, model weighs the end of the prompt more heavily |
Practical Recommendations
- Make every condition explicit. If the result depends on the input, use IF-THEN. Never trust the model to make the right decision on its own.
- Measure baseline before optimizing. 3 runs with the unchanged prompt. If the score is fine, don't touch it.
- Collect golden references. Every well-drafted piece of firm work is a potential few-shot example. Anonymize and add to the prompt library.
- Test out-of-distribution. A prompt that works for mechanical engineering patents can fail for software patents. Always test with divergent inputs.
- Place the task at the end of the prompt. Context and examples first, the actual instruction last. This uses the recency bias to your advantage.