beginner30 minLesson 1 of 5

Fundamentals of Prompt Engineering

Learn core concepts: prompts, tokenization, roles, temperature settings, and basic prompting strategies.

Fundamentals of Prompt Engineering

What is a Prompt?

A prompt is the input you provide to an LLM to elicit a specific response. It's the fundamental unit of interaction with modern AI models like GPT-4, Claude, and Llama. The quality of your prompt directly determines the quality of the model's output — garbage in, garbage out remains as true for AI as for traditional software.

The LLM Input/Output Flow

100%

Full API Call Lifecycle

100%
ℹ️Note

Each API call travels through multiple stages: tokenization converts your text to numbers the model understands, inference generates tokens probabilistically, and detokenization converts results back to text. Understanding this pipeline helps you debug issues like token limits or unexpected output.


Tokenization

Tokenization is the process of breaking text into smaller units (tokens) that the model can understand. Different models use different tokenization algorithms — GPT-4 uses Byte-Pair Encoding (BPE), while other models may use SentencePiece or WordPiece.

Token Facts:

  • 1 token ≈ 4 characters in English
  • 100 tokens ≈ 75 words
  • Different models have different tokenizers
  • Special tokens (like <|endoftext|>) also count toward your limit

Tokenizer Comparison Across Models

ModelTokenizer TypeVocabulary SizeApprox Tokens per Word (English)Special Features
GPT-4 / GPT-3.5BPE (Byte-Pair Encoding)~100K1.3OpenAI tiktoken, handles code well
Claude (Anthropic)BPE (Byte-Pair Encoding)~100K1.3Efficient for multilingual text
Llama 2/3BPE (SentencePiece)~32K1.4Smaller vocab, good for memory-constrained
Gemini (Google)SentencePiece~256K1.2Largest vocabulary, efficient for CJK
python
# Example: Tokenization with OpenAI's tiktoken import tiktoken # Load the tokenizer for GPT-4 encoding = tiktoken.encoding_for_model("gpt-4") text = "Hello, how are you?" tokens = encoding.encode(text) print(f"Original text: {text}") print(f"Tokens: {tokens}") print(f"Token count: {len(tokens)}") # Decode back to text decoded = encoding.decode(tokens) print(f"Decoded: {decoded}")
💡Tip

Always estimate token usage before sending large prompts. A 1000-token prompt vs. response costs roughly $0.01-0.03 with GPT-4, but costs add up quickly in production. Use tiktoken or your model's tokenizer to count tokens client-side before the API call.

Context Window Limits

100%
📌Important

Each model has a context window (GPT-4: 8K-128K, Claude 3: 200K, Gemini: 32K-1M). The sum of your system message + conversation history + user input + generated output must fit within this window. Once exceeded, the model truncates older messages — potentially losing important context.


System vs User vs Assistant Roles

LLMs use a conversation history with three distinct roles:

RolePurposeExample Usage
SystemSets behavior, personality, and context for the entire conversation"You are a helpful Python tutor. Be concise and use code examples."
UserRepresents the human's input or question"How do I sort a list in Python?"
AssistantRepresents the AI's previous responses in the conversation history"You can use the sorted() function or .sort() method..."
ℹ️Note

The system message is particularly powerful—it persists throughout the conversation and guides how the model responds to all subsequent messages.

📌Important

System prompt best practices: Be specific about the model's persona, output format, and constraints. Include guardrails like "If you don't know the answer, say so." Avoid vague instructions like "be helpful" — instead, describe what helpful looks like in your context.

python
from openai import OpenAI client = OpenAI() response = client.chat.completions.create( model="gpt-4", messages=[ # System role: Sets the AI's persona {"role": "system", "content": "You are a concise math tutor. Explain concepts simply."}, # User role: The actual question {"role": "user", "content": "What is the Pythagorean theorem?"} ] ) # Assistant role: The response assistant_reply = response.choices[0].message.content print(assistant_reply)

Cross-Provider Role Examples

python
# Anthropic Claude API import anthropic client = anthropic.Anthropic() response = client.messages.create( model="claude-3-opus-20240229", system="You are a concise math tutor. Explain concepts simply.", # System prompt as separate param messages=[ {"role": "user", "content": "What is the Pythagorean theorem?"} ] ) print(response.content[0].text)
python
# Google Gemini API import google.generativeai as genai genai.configure(api_key="YOUR_API_KEY") model = genai.GenerativeModel( model_name="gemini-1.5-pro", system_instruction="You are a concise math tutor. Explain concepts simply." ) response = model.generate_content("What is the Pythagorean theorem?") print(response.text)

Temperature and Top_p

These parameters control the randomness and creativity of the output:

ParameterRangePurposeTypical Values
Temperature0-2Controls randomness. Lower = more deterministic.0.0 (deterministic), 0.7 (balanced), 1.5 (creative)
Top_p0-1Nucleus sampling. Only consider tokens with cumulative probability mass.0.1 (focused), 0.9 (diverse)

How Temperature Actually Works

At temperature 0, the model always picks the highest-probability token (greedy decoding). As temperature increases, lower-probability tokens become more likely to be chosen, producing more varied and creative outputs.

100%
💡Tip

Choosing temperature values: For factual tasks (classification, extraction, Q&A), use 0.0-0.3. For creative tasks (story writing, brainstorming), use 0.7-1.2. Avoid temperatures above 1.5 unless you want near-random output — the "creative" output quickly becomes incoherent.

⚠️Warning

Setting temperature > 1.0 or top_p > 0.9 can lead to incoherent or hallucinated responses. For factual tasks, use temperature 0.0-0.5.

python
from openai import OpenAI client = OpenAI() # Creative writing - high temperature creative_response = client.chat.completions.create( model="gpt-4", messages=[{"role": "user", "content": "Write a one-sentence story about a robot"}], temperature=1.8, # Very creative top_p=0.95 ) print("Creative:", creative_response.choices[0].message.content) # Factual answer - low temperature factual_response = client.chat.completions.create( model="gpt-4", messages=[{"role": "user", "content": "What is the boiling point of water at sea level?"}], temperature=0.0, # Deterministic top_p=0.1 ) print("Factual:", factual_response.choices[0].message.content)

Temperature and Top_p Interaction

ScenarioTemperatureTop_pEffect
Strict factual0.01.0Model takes no risks, deterministic
Creative writing1.00.95Broad token selection, high creativity
Focused creative0.80.5Creative but stays within likely tokens
Code generation0.20.9Mostly deterministic, slight variation
Brainstorming1.20.95High variety, many alternatives
ℹ️Note

Most APIs recommend adjusting only one parameter. If you set both, temperature first softens the probability distribution, then top_p cuts off the tail. Setting both to extreme values simultaneously can produce very strange results.


Zero-Shot Prompting

Zero-shot prompting is when you ask the model to perform a task without any examples.

Classify this email as "important", "spam", or "neutral": "URGENT: Your bank account has been compromised. Click here to verify."

Basic Prompt Structure Template

100%

When Zero-Shot Works Best

Task TypeZero-Shot PerformanceNotes
Common classificationGoodModels know common categories intrinsically
Simple Q&AExcellentFactual knowledge from training data
TranslationVariableDepends on language pair and model training
Highly specialized tasksPoorNeeds examples or fine-tuning
Novel formatsPoorModels need examples of new output structures
ℹ️Note

Zero-shot works surprisingly well for tasks the model encountered during training. It struggles with edge cases, highly specialized domains, or precise format requirements — that's where few-shot and advanced techniques come in.


Practice Questions

Practice Question

A software engineer wants to use an LLM for a customer support chatbot. Which conversation role should be used to define the chatbot's personality and behavior guidelines?

Practice Question

When tokenizing the sentence "Hello, how are you?" using OpenAI's tiktoken, what happens during the tokenization process?

Practice Question

A developer is building a medical diagnosis assistant and needs the model to give consistent, factual answers. Which temperature setting should they use?

Practice Question

A prompt engineer needs the model to classify emails without providing any examples in the prompt. Which approach is being used?

Practice Question

What does the top_p parameter (nucleus sampling) control when generating LLM responses?

Practice Question

A team building a multilingual chatbot needs a model that handles Japanese, Korean, and Chinese efficiently. Based on the tokenizer comparison, which model's tokenizer is most efficient for CJK languages?

Practice Question

A prompt engineer sends a 150K-token document to a model with a 128K context window and sets max_tokens to 4000. What will happen?

Practice Question

Using temperature=0.8 with top_p=0.5 together produces what kind of behavior?

Practice Question

A developer notices their GPT-4 API call returned 150 tokens in the response but they set max_tokens to 500. The finish_reason was 'stop'. What does this mean?

Practice Question

A prompt engineer is comparing a Llama 2 model (32K vocab) with a GPT-4 model (100K vocab) for a code generation task. What trade-off should they consider?


Success

Key Takeaways:

  • A prompt is the input to an LLM, and tokenization converts text to model-readable tokens
  • The three conversation roles are: system (persona), user (human input), assistant (AI responses)
  • Temperature (0-2) controls randomness; lower values = more deterministic outputs
  • Top_p (0-1) performs nucleus sampling, limiting token selection probability
  • Zero-shot prompting works without examples; good basic structure = Instruction + Context + Input + Output Format
  • Context window limits constrain total tokens; always estimate before sending large inputs
  • Different providers (OpenAI, Anthropic, Google) have similar API patterns but different client libraries
Progress20%