# Prompt Basics

Prompting is the methodology through which we communicate instructions, parameters, and expectations to large language models. Consider a prompt as a detailed specification document provided to the model: the more precise and comprehensive the specifications, the higher the quality of the output. This guide establishes the fundamental principles for crafting effective prompts for open-source instruction-tuned models, including Llama, Deepseek, and Gemma.

## Why Prompts Matter

Large language models require clear direction to produce optimal results. Without precise instructions, they may produce inconsistent outputs. Well-structured prompts provide several benefits:

- **Reduce development time** by minimizing iterations needed for acceptable results.
- **Enhance output consistency** to ensure responses meet validation requirements without modification.
- **Optimize resource usage** by maintaining efficient context window utilization.

## Prompt Building Blocks

Most high-quality prompts contain five elements: **role, instructions, context, input, expected output**.

| Element | What it does |
| --- | --- |
| **Role** | Sets persona or expertise ("You are a data analyst…") |
| **Instructions** | Bullet-proof list of required actions |
| **Context** | Background knowledge or reference material |
| **Input** | The data or question to transform |
| **Expected Output** | Schema or miniature example to lock formatting |

### Real-world use case

Here's a real-world example demonstrating how these prompt building blocks work together to extract structured data from an email. Each element plays a crucial role in ensuring accurate, consistent output:

1. **System** - fixes the model's role so it doesn't add greetings or extra formatting.
2. **Instructions** - lists the exact keys; pairing this with [JSON mode](/docs/structured-outputs#json-object-mode) or [tool use](/docs/tool-use) further guarantees parseable output.
3. **Context** - gives domain hints ("Deliver to", postcode format) that raise extraction accuracy without extra examples.
4. **Input** - the raw e-mail; keep original line breaks so the model can latch onto visual cues.
5. **Example Output** - a miniature few-shot sample that locks the reply shape to one JSON object.

## Role Channels

Most chat-style APIs expose **three channels**:

| Channel | Typical Use |
| --- | --- |
| `system` | High-level persona & non-negotiable rules ("You are a helpful financial assistant."). |
| `user` | The actual request or data, such as a user's message in a chat. |
| `assistant` | The model's response. In multi-turn conversations, the assistant role can be used to track the conversation history. |

The following example demonstrates how to implement a customer service chatbot using role channels. Role channels provide a structured way for the model to maintain context and generate contextually appropriate responses throughout the conversation.

## Prompt Priming

Prompt priming is the practice of giving the model an **initial block of instructions or context** that influences every downstream token the model generates.  Think of it as "setting the temperature of the conversation room" before anyone walks in. This usually lives in the **system** message; in single-shot prompts it's the first paragraph you write. Unlike one- or few-shot demos, priming does not need examples; the power comes from describing roles ("You are a medical billing expert"), constraints ("never reveal PII"), or seed knowledge ("assume the user's database is Postgres 16").

### Why it Works

Large language models generate text by conditioning on **all previous tokens**, weighting earlier tokens more heavily than later ones.  By positioning high-leverage tokens (role, style, rules) first, priming biases the probability distribution over next tokens toward answers that respect that frame.

### Example (Primed Chat)

## Core Principles

1. **Lead with the must-do.** Put critical instructions first; the model weighs early tokens more heavily.
2. **Show, don't tell.** A one-line schema or table example beats a paragraph of prose.
3. **State limits explicitly.** Use "Return **only** JSON" or "less than 75 words" to eliminate chatter.
4. **Use plain verbs.** "Summarize in one bullet per metric" is clearer than "analyze."
5. **Chunk long inputs.** Delimit data with ``` or \<\<\< … \>\>\> so the model sees clear boundaries.

## Context Budgeting

While many models can handle up to **128K** tokens (or more), using a longer system prompt still costs latency and money. While you might be able to fit a lot of information in the model's context window, it could increase latency and reduce the model's accuracy. As a best practice, only include what is needed for the model to generate the desired response in the context.

## Quick Prompting Wins

Try these **10-second tweaks** before adding examples or complex logic:

| Quick Fix | Outcome |
| --- | --- |
| Add a one-line persona (*"You are a veteran copy editor."*) | Sharper, domain-aware tone |
| Show a mini output sample (one-row table / tiny JSON) | Increased formatting accuracy |
| Use numbered steps in instructions | Reduces answers with extended rambling |
| Add "no extra prose" at the end | Stops model from adding greetings or apologies |

## Common Mistakes to Avoid
Review these recommended practices and solutions to avoid common prompting issues.

| Common Mistake | Result | Solution |
| --- | --- | --- |
| **Hidden ask** buried mid-paragraph | Model ignores it | Move all instructions to top bullet list |
| **Over-stuffed context** | Truncated or slow responses | Summarize, remove old examples |
| **Ambiguous verbs** (*"analyze"*) | Vague output | Be explicit (*"Summarize in one bullet per metric"*) |
| **Partial JSON keys** in sample | Model Hallucinates extra keys | Show the **full** schema: even if brief |

## Parameter Tuning
Optimize model outputs by configuring key parameters like temperature and top-p. These settings control the balance between deterministic and creative responses, with recommended values based on your specific use case.

| Parameter | What it does | Safe ranges | Typical use |
| --- | --- | --- | --- |
| **Temperature** | Global randomness (higher = more creative) | 0 - 1.0 | 0 - 0.3 facts, 0.7 - 0.9 creative |
| **Top-p** | Keeps only the top p cumulative probability mass - use this or temperature, not both | 0.5 - 1.0 | 0.9 facts, 1.0 creative |
| **Top-k** | Limits to the k highest-probability tokens | 20 - 100 | Rarely needed; try k = 40 for deterministic extraction |

### Quick presets

The following are recommended values to set temperature or top-p to (but not both) for various use cases:

| Scenario | Temp | Top-p | Comments |
| --- | --- | --- | --- |
| Factual Q&A | 0.2 | 0.9 | Keeps dates & numbers stable |
| Data extraction (JSON) | 0.0 | 0.9 | Deterministic keys/values |
| Creative copywriting | 0.8 | 1.0 | Vivid language, fresh ideas |
| Brainstorming list | 0.7 | 0.95 | Variety without nonsense |
| Long-form code | 0.3 | 0.85 | Fewer hallucinated APIs |

## Controlling Length & Cost

The following are recommended settings for controlling token usage and costs with length limits, stop sequences, and deterministic outputs.

| Setting | Purpose | Tip |
| --- | --- | --- |
| `max_completion_tokens` | Hard cap on completion size | Set 10-20 % above ideal answer length |
| Stop sequences | Early stop when model hits token(s) | Use `"###"` or another delimiter |
| System length hints | "less than 75 words" or "return only table rows" | Model respects explicit numbers |
| `seed` | Controls randomness deterministically | Use same seed for consistent outputs across runs |

## Guardrails & Safety

Good prompts set the rules; dedicated guardrail models enforce them. [Meta's **Llama Guard 4**](/docs/content-moderation) is designed to sit in front of: or behind: your main model, classifying prompts or outputs for safety violations (hate, self-harm, private data). Integrating a moderation step can cut violation rates without changing your core prompt structure.

## Next Steps

Ready to level up? Explore dedicated [**prompt patterns**](/docs/prompting/patterns) like zero-shot, one-shot, few-shot, chain-of-thought, and more to match the pattern to your task complexity. From there, iterate and refine to improve your prompts.

---

## Model Migration Guide