Skip to content
derpx06Notes on systems, models & learning
1. LLM Foundations · lesson 6 of 68 · 6 min · January 20, 2024

Prompting Essentials

A deep dive into Prompt Engineering: From tokens and vectors to agents and RAG.

Computing has shifted from deterministic (Input A always equals Output B) to probabilistic. We no longer issue rigid commands; we guide a statistical oracle that predicts the next likely word. This requires a new skill: Prompt Engineering.

Treat the LLM like a hyper-intelligent, well-read, but sleep-deprived intern. It knows everything (Internet training) but has no short-term memory (Context Window) and hallucinates when unsure.

  • Bad Management: Barking vague commands ("Market report!"). Result: Hallucinations.
  • Good Management: Clear context ("You are a senior analyst"), constraints ("Under 200 words"), and format ("Bullet points").

To command the model, you must understand how it "sees".

Models process Tokens (chunks of character), not words. "Apple" is one token; "Lollipop" might be three.

  • Implication: Models struggle with spelling backwards or character-level math because they see the "chunk", not the letters.

Tokens are converted into Vectors (lists of numbers). In this 3D math space:

vector_math.txt
King - Man + Woman ≈ Queen

Prompting is simply giving the model coordinates in this "Meaning Space" to narrow down the probable output.

The model has a First-In, First-Out memory.

Transformers process all tokens simultaneously (in parallel), not sequentially like us. To understand "order," they use Positional Encoding. This technique adds information about the position of each token in the sequence to the input embeddings. Without this, the model wouldn't know if "Man bites Dog" is different from "Dog bites Man". It is critical for capturing the structure of syntax.


Industry experts use the CO-STAR framework to ensure prompts have high vector density.

ComponentDefinitionExample
ContextThe Who/Where.You are a senior Java Engineer.
ObjectiveThe Task.Refactor this legacy code.
StyleThe Tone/Voice.Professional, curt, senior.
ToneEmotional resonance.Helpful but strict.
AudienceWho is reading.Junior developers.
ResponseFormat.Markdown code block only.

The Bad Prompt:

"Write an email declining a wedding."

The CO-STAR Prompt:

co_star_prompt.txt
(Context) You are a polite and socially anxious friend. 
(Objective) Write an email declining a wedding invitation from my college roommate, Sarah. 
(Reason) I will be out of the country for work. 
(Tone) Warm, regretful, but firm. 
(Response) Keep it under 100 words. Do not use emojis.

Most complex tasks can be solved with these foundational patterns.

Models like GPT-4 are trained on massive datasets to be "Instruction Tuned," meaning they can guess what you want without examples.

Example:

Prompt: Classify the text into neutral, negative or positive. Text: I think the vacation is okay. Sentiment: Output: Neutral

Why Use This?

  • Pros: Fast, cheap (few tokens), easy to write.
  • Cons: Unreliable for complex logic or specific formats. Best for "vibe checks" or simple classification.

How to Use Properly:

  • Be Specific: Since you have no examples, your instruction must be flawless. Use the CO-STAR framework.
  • Use Constraints: Explicitly state what NOT to do (e.g., "Do not use slang").

While models are smart, they struggle with specific formats or nuance in Zero-Shot. Few-Shot prompting provides "Exemplars" (Demonstrations) to steer the model.

Pattern:

few_shot_pattern.txt
<Question>?
<Answer>

<Question>?
<Answer>

<Question>?

Example:

few_shot_sentiment.txt
This is awesome! // Negative
This is bad! // Positive
Wow that movie was rad! // Positive
What a horrible show! // 

Output: Negative

Note: In the example above, we "tricked" the model into learning a reversed sentiment pattern by providing examples.

Why Use This?

  • Pros: Drastically improves adherence to specific formats (JSON, CSV) and style (tone/voice).
  • Cons: Consumes more tokens (costlier); requires you to have good examples.

How to Use Properly:

  • Balance Patterns: If you provide 3 "Positive" examples and 0 "Negative" ones, the model will be biased to say "Positive". Always use a diverse set (1 Positive, 1 Negative, 1 Neutral).
  • Randomize Order: To avoid "Recency Bias", shuffle your examples so the model doesn't just copy the last one.

Introduced in Wei et al. (2022), Chain-of-Thought (CoT) compels the model to "show its work." Standard LLMs are bad at math because they try to predict the answer token immediately. By asking it to think step-by-step, we generate intermediate tokens that act as a buffer for reasoning.

The "Odd Numbers" Test:

cot_reasoning.txt
The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1.
A: Adding all the odd numbers (9, 15, 1) gives 25. The answer is False.

The odd numbers in this group add up to an even number: 17,  10, 19, 4, 8, 12, 24.
A: Adding all the odd numbers (17, 19) gives 36. The answer is True.

The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. 
A:

Output: Adding all the odd numbers (15, 5, 13, 7, 1) gives 41. The answer is False.

Why Use This?

  • Pros: Essential for math, logic puzzles, and debugging code. Unlocks "System 2" thinking.
  • Cons: Slower generation; can sometimes hallucinate the reasoning steps themselves.

How to Use Properly:

  • Zero-Shot CoT: Just add "Let's think step by step" to the end of your prompt. It's the lazy way to get 80% of the benefit.
  • Manual CoT: For critical tasks, write the reasoning path yourself in a Few-Shot example. Show the model exactly how to think.

Assigning a role ("You are a Physicist") shifts the model's parameters to a specific cluster of training data, improving accuracy for domain-specific tasks.


Theory is good, but examples are better. Here are three robust patterns you can copy-paste.

Use this to get high-quality, safe code refactors.

pattern_coder.txt
(Role) You are a Principal Software Engineer at Google.
(Task) Refactor the following Python code for readability and performance.
(Constraints)
1. Use type hinting for all functions.
2. Add docstrings in Google style.
3. Do NOT change the external behavior of the function.
4. If you spot a bug, fix it and leave a comment explaining why.

(Input Code)
def calc(x,y):
  return x*y + 10

(Output Format)
Return only the refactored code block.

Use this to turn messy inputs into clean, machine-readable data.

Option 1: JSON (Strict & Standard) Best for APIs and web integrations.

extractor_json.txt
(Context) You are a data parser.
(Task) Extract user details into a strict JSON object.
(Input) "My name is Sid, I live in NYC, and I love coding."
(Output Format)
{
"name": "string",
"city": "string",
"interests": ["string"]
}
(Constraint) Return ONLY the JSON. No markdown.

Option 2: YAML (Token Efficient) YAML is often cheaper (fewer tokens) and less prone to syntax errors (no brackets/commas) for LLMs.

extractor_yaml.txt
(Context) You are a configuration generator.
(Task) Generate a Kubernetes config for a 'web-app' container using image 'nginx:latest'.
(Output Format)
apiVersion: v1
kind: Pod
metadata:
name: <name>
spec:
containers:
- name: <name>
  image: <image>
(Constraint) Return ONLY valid YAML.

Use this to learn a new concept without being given the answer immediately.

pattern_tutor.txt
(Role) You are a Socratic Tutor.
(Objective) Help me understand "Recursion".
(Rules)
1. Do not give me the definition directly.
2. Ask me a simple question to get me started.
3. When I answer, guide me to the next step.
4. Use analogies related to "Stacking Boxes".

For tasks that require Actions or New Knowledge, standard prompting fails.

A Chatbot talks. An Agent acts. It uses a loop of Reason -> Act -> Observe.

  • Thought: I need the CEO's age.
  • Action: Search("Microsoft CEO")
  • Observation: Satya Nadella.

If the model doesn't know a fact, don't force it to guess (Hallucination).

  1. Retrieve relevant documents from a database.
  2. Inject them into the prompt.

For tasks that require strategic lookahead (like Chess, Crosswords, or Creative Writing), simple Chain of Thought fails because it is linear. If the model makes one mistake early on, the entire reasoning chain collapses.

Tree of Thoughts (Yao et al. 2023) generalizes CoT by maintaining a "tree" of possible next steps, allowing the model to look ahead, backtrack, and self-correct. It turns the LLM into a Search Algorithm.

The Mechanism:

  1. Decomposition: Break the problem into steps (e.g., "Write paragraph 1", "Write paragraph 2").
  2. Generation: At each step, generate multiple candidates (Thoughts).
    • Thought A: "Start with a joke."
    • Thought B: "Start with a statistic."
    • Thought C: "Start with a quote."
  3. Evaluation: The model scores each thought.
    • Score A: 0.4 (Too casual).
    • Score B: 0.9 (Strong hook).
    • Score C: 0.6 (Cliché).
  4. Search: Use Breadth-First Search (BFS) or Depth-First Search (DFS) to keep the best thoughts and prune the bad ones. If a path leads to a dead end, backtrack to the previous node.

Why Use This?

  • Pros: Solves problems standard LLMs simply cannot (puzzles, complex planning). It is the state-of-the-art for "Hard Reasoning".
  • Cons: Very expensive and slow (requires 10x-100x more tokens per problem).

How to Use Properly: ToT usually requires an external Python script to control the loop, as the LLM cannot effectively "backtrack" inside a single chat window.

  1. Define a Scorer: Tell the model specifically what makes a "Good" thought (e.g., "Must contain a citation", "Must be under 20 words").
  2. Limit Depth: Set a maximum tree depth (e.g., 3 steps) to prevent infinite loops and wasted money.


TopicKey Concept
Nature of LLMsStochastic Parrot vs World Model
Prompt StructureCO-STAR Framework
TechniquesChain of Thought (CoT), Tree of Thoughts (ToT)
SystemsAgents, RAG, ReAct Framework