Prompt Engineering for Mobile SLMs.
Techniques designed for 100-billion-parameter cloud models fail on 1B–8B mobile models. Learn how to calibrate sampling parameters, master native chat templates (ChatML, Llama-3, Gemma), and deploy 8 production-ready offline recipes on Android.
Cloud models like GPT-4 tolerate ambiguous, conversational prompts. Small on-device models (1B to 7B) require explicit role constraints, negative boundaries (e.g. "never output markdown commentary"), and concrete few-shot examples. When you constrain the search space, small models perform astonishingly close to frontier cloud models.
Chat Template Syntax Comparison Table
Every model family expects specific control tokens to delineate roles. Mismatching delimiters causes special token bleed or conversational confusion:
| Format | Supported Models | System Delimiter | User Delimiter | Assistant Delimiter | Stop Tokens | Trailing Gen Prompt |
|---|---|---|---|---|---|---|
| ChatML | Qwen 2.5, SmolLM2, Yi, DeepSeek-R1 (Qwen Distill) | <|im_start|>system\n{system_message}<|im_end|> | <|im_start|>user\n{user_message}<|im_end|> | <|im_start|>assistant\n{assistant_message}<|im_end|> | <|im_end|>, <|endoftext|> | <|im_start|>assistant\n |
| Llama-3 | Llama 3.1, Llama 3.2 (1B/3B), DeepSeek-R1 (Llama Distill) | <|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{system_message}<|eot_id|> | <|start_header_id|>user<|end_header_id|>\n\n{user_message}<|eot_id|> | <|start_header_id|>assistant<|end_header_id|>\n\n{assistant_message}<|eot_id|> | <|eot_id|>, <|end_of_text|> | <|start_header_id|>assistant<|end_header_id|>\n\n |
| Gemma | Gemma 2 (2B, 9B, 27B) | <bos><start_of_turn>user\n{system_message}\n\n{user_message}<end_of_turn> | <start_of_turn>user\n{user_message}<end_of_turn> | <start_of_turn>model\n{assistant_message}<end_of_turn> | <end_of_turn> | <start_of_turn>model\n |
| Mistral / Mixtral | Mistral 7B Instruct v0.2 / v0.3 | <s>[INST] {system_message}\n\n{user_message} [/INST] | [INST] {user_message} [/INST] | {assistant_message}</s> | </s> | (None) |
Sampling Parameter Reference Matrix
Calibrate Local AI inference parameters to the nature of your workload. Small models benefit greatly from dynamic Min-P filtering to cut off low-probability token tails:
| Workload / Task | Temperature | Top-P | Top-K | Min-P | Rep. Penalty | Engineering Rationale |
|---|---|---|---|---|---|---|
| Coding & Syntax Generation | 0.15 – 0.25 | 0.85 | 20 | 0.05 | 1.05 | Deterministic token choices eliminate syntax hallucinations and hallucinated API functions. |
| Math & Formal Logic (e.g. DeepSeek-R1) | 0.60 | 0.95 | 40 | 0.05 | 1.00 | Maintains exploratory chain-of-thought exploration while preventing wandering into infinite reflection loops. |
| Fact Retrieval & Strict Summarization | 0.10 – 0.20 | 0.80 | 30 | 0.08 | 1.15 | Restricts vocabulary to tokens directly anchored in provided context documents. |
| Creative Writing & Dialogue | 0.75 – 0.85 | 0.92 | 60 | 0.03 | 1.18 | Higher entropy provides lexical variety, while repetition penalty prevents looping adjectives. |
Controls logits sharpness. Lower values (0.1–0.2) force highest-probability tokens for deterministic facts.
Discards any token whose probability is below a fraction of the top token. Vastly cleaner than static Top-P on SLMs.
Penalizes logits of recently generated tokens. 1.15–1.20 prevents infinite repetition loops on 1B–3B models.
Limits token pool to the top K highest candidates before applying probability sampling. 20–40 is ideal on mobile.
8 Production-Ready Prompt Recipes
Copy and paste these verified, battle-tested system prompts directly into Local AI:
Recipe 1: Strict JSON Structured Output (Zero Markdown Fluff)
Small models often prepend conversational filler like 'Here is your JSON:'. This prompt enforces pure RFC 8259 JSON output suitable for automated app parsing.
Recipe 2: Air-Gapped NDA & Contract Summarizer (Zero Hallucination)
Extracts strictly verified factual summaries without extrapolating or hallucinating clauses that do not exist in the source document.
Recipe 3: Mobile Code Refactoring & Security Diagnostic
Diagnoses security vulnerabilities, memory leaks, and inefficient algorithms directly on your phone without internet access.
Recipe 4: DeepSeek-R1 Chain-of-Thought Reasoning Controller
Controls the thinking depth of DeepSeek-R1. Enforces concise, verified mathematical and logic steps without rambling into infinite self-reflection loops.
Recipe 5: Document OCR Post-Processing & Table Extraction
Cleans up garbled OCR character artifacts, fixes optical misreads (e.g. '0' vs 'O', '1' vs 'l'), and converts messy scanner outputs into structured Markdown tables.
Recipe 6: Offline Multilingual Translation with Tone & Idiom Preservation
Translates sensitive personal or business communication without sending private text to cloud translation APIs, accurately conveying cultural idioms and register.
Recipe 7: Voice Note & Transcript Action-Item Synthesizer
Ingests rambling, unscripted speech-to-text transcripts and distills them into clear decisions, blockers, and assigned task lists with deadlines.
Recipe 8: On-Device Python & Bash Utility Generator
Produces standalone, single-file scripts with robust error handling, POSIX flag support, and zero external package dependencies.
Frequently Asked Questions
Why do small models (1B–3B) sometimes repeat the exact same sentence in an infinite loop?+
What is Min-P sampling and why is it superior to Top-P on small models?+
How does the ChatML template work and why does it matter?+
How can I prevent small models from rambling or adding unnecessary greetings?+
Does few-shot prompting work well on mobile models?+
How does context window size affect mobile phone RAM and speed?+
Try Local AI on Android
100% private. Run reasoning models with custom system prompts, Min-P sampling, and zero cloud leaks.