Loading...

Go Back

Next page
Go Back Course Outline

Prompt Engineering Full Course


Foundations of Language Models and Prompting

How Language Models Understand Prompts

Large Language Models (LLMs) don't "understand" language in the same way humans do. They process text through a series of mathematical transformations, identifying patterns and relationships within the data they were trained on. Here's a breakdown of how they process prompts:

  • Tokenization and embedding:
    • Tokenization: The input prompt is broken down into smaller units called "tokens." A token can be a word, a part of a word, or even a punctuation mark. For example, the sentence "The quick brown fox" might be tokenized into ["The", "quick", "brown", "fox"].
    • Embedding: Each token is then converted into a numerical representation called an "embedding." Embeddings are vectors (lists of numbers) that capture the semantic meaning of the token. Tokens with similar meanings have similar embeddings.
  • Contextual understanding: LLMs use the embeddings to understand the context of the prompt. They analyze the relationships between the tokens to determine the overall meaning and intent. This involves complex calculations and attention mechanisms, allowing the model to weigh the importance of different words in the prompt.
  • The "black box" and interpretability: While we can describe the general process, the exact workings of LLMs are often considered a "black box." It's difficult to pinpoint precisely why a model generates a specific output for a given prompt. Interpretability is an ongoing area of research, trying to understand how LLMs arrive at their conclusions. However, for prompt engineering, we focus on the controllable input and observed output.

Anatomy of a Prompt

A prompt isn't just a simple question; it's a structured input that provides the LLM with the information it needs to generate a desired response. Here are the key components that make up a well-formed prompt:

  • Instructions, context, examples, and delimiters:
    • Instructions: Clear and direct commands that tell the LLM what to do (e.g., "Summarize this article," "Translate this sentence").
    • Context: Background information that helps the LLM understand the task and the desired output (e.g., "You are a helpful customer service agent").
    • Examples: Demonstrate the desired output format or style (especially important for few-shot prompting).
    • Delimiters: Special characters or formatting that help the LLM distinguish between different parts of the prompt (e.g., using triple quotes """ to separate context from instructions).


  • Persona, role-play, and framing:
    • Persona/Role-play: Instructing the LLM to adopt a specific persona or role (e.g., "Act as a historical expert," "You are a software developer").
    • Framing: Presenting the prompt in a way that influences the LLM's response (e.g., framing a question as a problem to be solved).
  • Constraints, desired output format: Specifying limitations or guidelines for the LLM's output.

Data Pre-Processing for Better Prompts

While the prompt itself is the primary input, sometimes you might need to process external data before incorporating it into the prompt. This ensures the LLM receives the cleanest and most relevant information.

  • Cleaning and formatting input data: Removing irrelevant information, correcting errors, and standardizing the format of external data before including it in the prompt. For example, if you're using data from a spreadsheet, you might need to convert it to a consistent format.
  • Data augmentation for prompts: Creating variations of the input data to make the prompt more robust or to provide the LLM with more examples.
  • Handling noise and inconsistencies: Dealing with errors, inconsistencies, or ambiguities in the input data to prevent the LLM from being misled.


Writing Clear and Effective Prompts

The key to successful prompt engineering is writing prompts that are easy for the LLM to understand and follow. Here are some principles for crafting effective prompts:

  • Clarity, conciseness, and specificity: The prompt should be unambiguous, to the point, and focused on the desired outcome. Avoid vague or open-ended questions.
  • Positive vs. negative framing: Phrasing the prompt in terms of what the LLM *should* do, rather than what it *shouldn't* do, often leads to better results.
  • Iterative refinement: Prompt engineering is an iterative process. It often involves experimenting with different phrasings, formats, and examples to optimize the LLM's output.
Go Back

Next page