Choosing Parameters for Your Use Case
Recommended settings for factual Q&A, creative writing, code, and chat.
Choosing Parameters for Your Use Case is a free AI Prompt Engineering lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Prompt Engineering learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Parameter Decision Framework
Choosing sampling parameters is not guesswork — it follows a logic based on your task requirements. Two key dimensions determine the right configuration:
- Output accuracy: how important is it that the output is correct and predictable?
- Output diversity: how important is it that outputs vary and explore?
High accuracy → low temperature. High diversity → high temperature. Most tasks sit somewhere between these extremes.
Factual Q&A: Accuracy is Everything
For factual question answering, there is typically one correct answer. Any randomness increases the chance of a wrong answer. Use:
- temperature = 0: greedy decoding, fully deterministic
- top_p = 1.0: leave unrestricted; at temperature=0, top-p has no effect
This ensures the model always picks its most confident answer, which is the most likely to be correct.
import openai
client = openai.OpenAI(api_key='sk-...')
# Factual Q&A configuration
def factual_qa(question):
resp = client.chat.completions.create(
model='gpt-4o',
messages=[
{'role': 'system', 'content': 'Answer factual questions concisely and accurately.'},
{'role': 'user', 'content': question}
],
temperature=0,
top_p=1.0
)
return resp.choices[0].message.content
answer = factual_qa('What is the boiling point of water at sea level?')
print(answer) # Always: '100 degrees Celsius (212 degrees Fahrenheit).'Code Generation: Near-Greedy
Code must be syntactically correct and semantically precise. Randomness causes syntax errors, wrong variable names, or incorrect logic. Use:
- temperature = 0–0.2: almost greedy — allows minor variation but strongly prefers correct token choices
- top_p = 0.95: eliminates extreme-tail tokens that could cause syntax errors
# Code generation configuration
def generate_code(task):
resp = client.chat.completions.create(
model='gpt-4o',
messages=[
{'role': 'system', 'content': 'You are an expert Python programmer. Write clean, correct code.'},
{'role': 'user', 'content': task}
],
temperature=0.1,
top_p=0.95
)
return resp.choices[0].message.content
code = generate_code('Write a Python function that merges two sorted lists.')
print(code)Summarization: Moderate Settings
Summarization needs accuracy (don't distort facts) but benefits from mild variation (different runs can emphasize different aspects). Use:
- temperature = 0.3–0.5: light randomness preserves natural phrasing
- top_p = 1.0: no nucleus restriction needed at low temperature
# Summarization configuration
def summarize(text):
resp = client.chat.completions.create(
model='gpt-4o',
messages=[
{'role': 'system', 'content': 'Summarize the following text in 3 sentences.'},
{'role': 'user', 'content': text}
],
temperature=0.4,
top_p=1.0
)
return resp.choices[0].message.content
summary = summarize(long_article)
print(summary)Conversational Chat: Natural Variation
Chat responses benefit from natural variation — the same question should not always produce the exact same phrasing. Too much randomness makes responses incoherent. Use:
- temperature = 0.7–0.9: natural, varied conversation
- top_p = 0.9–0.95: eliminates improbable tail tokens that produce unnatural phrasing
# Chat configuration
def chat_response(user_message, history):
messages = history + [{'role': 'user', 'content': user_message}]
resp = client.chat.completions.create(
model='gpt-4o',
messages=[
{'role': 'system', 'content': 'You are a friendly, helpful assistant.'},
*messages
],
temperature=0.8,
top_p=0.9
)
reply = resp.choices[0].message.content
history.append({'role': 'user', 'content': user_message})
history.append({'role': 'assistant', 'content': reply})
return reply, historyCreative Writing: High Diversity
Creative writing values originality and unexpectedness. Low temperatures produce clichéd, predictable output. High temperatures produce vivid, unexpected choices — though too high causes incoherence. Use:
- temperature = 0.9–1.2: diverse vocabulary, non-obvious word choices
- top_p = 0.95: prevent incoherent tail tokens while allowing broad exploration
# Creative writing configuration
def write_story_opening(prompt):
resp = client.chat.completions.create(
model='gpt-4o',
messages=[
{'role': 'system', 'content': 'You are a creative fiction writer. Write vivid, original prose.'},
{'role': 'user', 'content': f'Write a story opening for: {prompt}'}
],
temperature=1.1,
top_p=0.95
)
return resp.choices[0].message.content
opening = write_story_opening('A detective discovers her partner is the killer.')
print(opening)Brainstorming and Ideation
For brainstorming, the goal is maximum diversity of ideas — even unusual or unexpected ones. Use the highest practical temperature:
- temperature = 1.0–1.5: broad exploration of idea space
- top_p = 0.95–1.0: minimal restriction
Run multiple times and collect all outputs — then filter the best ideas manually. Brainstorming prompts benefit from explicit requests for variety: 'Give me 10 different approaches, including unusual ones.'
# Brainstorming: run multiple times for variety
def brainstorm(topic, n_runs=5):
ideas = []
for _ in range(n_runs):
resp = client.chat.completions.create(
model='gpt-4o',
messages=[
{'role': 'system', 'content': 'Generate creative, diverse ideas. Include unusual approaches.'},
{'role': 'user', 'content': f'Give me 3 ideas for: {topic}'}
],
temperature=1.3,
top_p=0.95
)
ideas.append(resp.choices[0].message.content)
return ideas
all_ideas = brainstorm('reducing customer churn')Parameter Sensitivity Testing
Before committing to parameters, run parameter sensitivity tests: hold the prompt constant, vary one parameter at a time, and evaluate output quality at each setting.
def sensitivity_test(prompt, evaluator, temperatures, n_samples=5):
results = {}
for temp in temperatures:
scores = []
for _ in range(n_samples):
resp = client.chat.completions.create(
model='gpt-4o',
messages=[{'role': 'user', 'content': prompt}],
temperature=temp
)
score = evaluator(resp.choices[0].message.content)
scores.append(score)
results[temp] = sum(scores) / len(scores)
for temp, score in sorted(results.items()):
print(f'T={temp}: avg score = {score:.2f}')
best_temp = max(results, key=results.get)
print(f'Best temperature: {best_temp}')
return best_tempWhen to Leave at Defaults
Not every application needs custom parameters. Leave them at defaults when:
- The use case is general chat — default temperature (1.0 for most APIs) is well-tuned
- You are prototyping and do not yet know the right settings
- The task is balanced between accuracy and creativity (e.g., email drafting)
Rule of thumb: customize parameters only when default behavior causes a measurable quality problem in testing. Premature parameter tuning adds complexity without measurable benefit.
# Defaults are often correct
default_resp = client.chat.completions.create(
model='gpt-4o',
messages=[{'role': 'user', 'content': 'Draft a professional email declining a meeting.'}]
# No temperature or top_p specified — API defaults apply
# OpenAI default: temperature=1, top_p=1
)
print(default_resp.choices[0].message.content)Quick Reference Card
Summary table of recommended settings by use case:
PARAMETER_PRESETS = {
# task: (temperature, top_p, notes)
'factual_qa': (0.0, 1.0, 'greedy, max accuracy'),
'classification': (0.0, 1.0, 'deterministic labels'),
'code_generation': (0.1, 0.95, 'near-greedy, avoid tail tokens'),
'data_extraction': (0.2, 1.0, 'slight variation for robustness'),
'summarization': (0.4, 1.0, 'preserve facts, vary phrasing'),
'translation': (0.3, 1.0, 'accurate but natural'),
'chat': (0.8, 0.9, 'natural conversation'),
'email_drafting': (0.7, 0.9, 'professional but varied'),
'creative_writing': (1.1, 0.95, 'vivid, original'),
'brainstorming': (1.3, 0.95, 'maximum diversity'),
}
for task, (temp, top_p, notes) in PARAMETER_PRESETS.items():
print(f'{task:22} temp={temp}, top_p={top_p} | {notes}')Frequency and Presence Penalties
Two additional parameters complement temperature and top-p:
- frequency_penalty (0–2): reduces the probability of tokens proportional to how often they have appeared in the output so far — prevents word repetition
- presence_penalty (0–2): reduces the probability of any token that has appeared at all — encourages topic diversity
For creative writing: add frequency_penalty=0.5 to reduce repeated word choices without changing the temperature.
resp = client.chat.completions.create(
model='gpt-4o',
messages=[{'role': 'user', 'content': 'Write a paragraph about coffee.'}],
temperature=1.0,
top_p=0.95,
frequency_penalty=0.5, # discourage repeating the same words
presence_penalty=0.3 # encourage introducing new topics/aspects
)
print(resp.choices[0].message.content)Knowledge Check
For a task that generates Python code that will be executed in production, which parameter configuration is most appropriate?
Recap: Choosing Sampling Parameters
Match parameters to your accuracy vs diversity needs:
- Factual/code: temp=0–0.2, top_p=0.95 — accuracy first
- Summarization: temp=0.3–0.5 — balance
- Chat: temp=0.7–0.9, top_p=0.9 — natural variation
- Creative: temp=0.9–1.2, top_p=0.95 — diversity
- Brainstorming: temp=1.3, top_p=0.95 — maximum exploration
Run parameter sensitivity tests. Leave at defaults when no quality problem is observed. Add frequency_penalty for repetition problems. This concludes Course 19. Next: Prompt Testing and Regression.
Frequently asked questions
Is the “Choosing Parameters for Your Use Case” lesson free?
Yes — the full text of “Choosing Parameters for Your Use Case” is free to read here on the web, and the AI Prompt Engineering course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Prompt Engineering course, upgrade to CoddyKit PRO.
What will I learn in “Choosing Parameters for Your Use Case”?
Recommended settings for factual Q&A, creative writing, code, and chat. You practise AI Prompt Engineering with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Prompt Engineering?
No prior experience is required. AI Prompt Engineering on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Choosing Parameters for Your Use Case” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Prompt Engineering lesson?
Yes. Every AI Prompt Engineering lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What Is Temperature in LLMs?
- Top-p Nucleus Sampling
- Top-k Sampling
- Choosing Parameters for Your Use Case