How to write effective prompts for LLMs
· Category: AI & Machine Learning
Short answer
Effective prompts provide clear instructions, relevant context, and desired output format to guide large language models toward accurate responses.
Steps
- Write explicit instructions that state the task, audience, and tone.
- Use few-shot prompting by including input-output examples within the context window.
- Apply chain-of-thought prompting for complex reasoning tasks by asking the model to think step by step.
- Structure prompts with delimiters such as triple quotes or XML tags to separate sections.
- Iterate and A/B test prompt variants to identify phrasing that improves performance.
Tips
- Start with zero-shot to establish a baseline before adding examples.
- Place the most important instructions at the beginning or end of the prompt.
- Specify response constraints such as length, format, and forbidden content.
- Use system messages to set persistent behavior across a conversation.
Common issues
- Ambiguous instructions leading to inconsistent or off-topic outputs.
- Exceeding context limits when including too many few-shot examples.
- Sensitivity to small wording changes causing large output variance.
- Prompt injection attacks where user input overrides intended instructions.
Example
import torch
import torch.nn as nn
model = nn.Sequential(
nn.Linear(784, 256),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(256, 10)
)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
This snippet defines a simple neural network with dropout for regularization, a cross-entropy loss, and the Adam optimizer in PyTorch.