Agentic AI (Part I) -- Prompt Engineering & Chain of Thought


Prompt Engineering

Prompt quality can significantly affect the quality of the LLM responses. This note will cover the techniques and best practices on how to write efficient prompts. We need to define some terminology at the beginning.

Terminology

Term Description
Developer The developer, typically OpenAI or third-party developers using the OpenAI API. Provides high-priority instructions.
User The end-user of the chat interface. Users may not be aware of internal high-priority system instructions. The instruction priority given to the model is lower than developer instructions, referred to as input.
Assistant Typically refers to the model itself, generating responses based on the conversation history.
Conversation A sequence of information fed into the model, consisting of Developer, User, and Assistant messages. Each message must include: Role, Content, and End_turn (Boolean). It can also include: Recipient, and Setting.
Output The model’s output, typically a structured object containing both format and content.

Prompt Engineering definition

Reference: Lilian Weng’s Prompt Engineering Guide

Prompt engineering involves writing effective instructions that enable the model to consistently generate responses that meet requirements. Different prompts should be tailored to different models.

Different model versions may produce different responses to the same prompt. It is strongly recommended to specify the model version when documenting prompts.

Model Types

Model Type Characteristics
Reasoning Models Excel at complex tasks and multi-step planning. Generally slower than GPT models.
GPT Models Excel at generating responses based on clear instructions.
Model Scale Used to balance response speed and answer quality.

Prompt Priority Hierarchy

Prompts have different priority levels:

  • Instructions (Higher Priority): Define the model’s behavior, including tone, objectives, and sample inputs.
  • Input (Lower Priority): The content to which instructions are applied.

The LLM model does not automatically split your prompt into “instructions” vs “inputs” in any formal, structured way.

  • Don’t assume the model “parses” your prompt — it only predicts.
  • Be explicit with structure if you want reliable separation. For instance:
    • Use clear labels: Instruction: ... and Input: ...
    • Use delimiters: ---, ###, or XML-like tags: <instruction> </instruction>
  • Test both orders: If you find the model misinterpreting your prompt, try switching the order of instruction and input, or adding explicit markers.

Example

  • instruction: Translate the following sentence into Chinese
  • input: Describe the city you live in.

When input conflicts with instruction, the model prioritizes the instruction.

Few-shot prompting

Few-shot prompting means presenting a set of high-quality demonstrations, each consisting of both input and desired output, on the target task in the prompt. The model implicitly “picks up” the pattern from those examples and applies it to a prompt. When providing examples, try to show a diverse range of possible inputs with the desired outputs.

It is a counterpart for the terminology zero-shot prompting, which means directly feed the task text to the model and ask for results.

Typically, you will provide examples as part of a developer message in your API request. Here’s an example developer message containing examples that show a model how to classify positive or negative customer service reviews.

# Identity

You are a helpful assistant that labels short product reviews as
Positive, Negative, or Neutral.

# Instructions

* Only output a single word in your response with no additional formatting
  or commentary.
* Your response should only be one of the words "Positive", "Negative", or
  "Neutral" depending on the sentiment of the product review you are given.

# Examples

<product_review id="example-1">
I absolutely love this headphones — sound quality is amazing!
</product_review>

<assistant_response id="example-1">
Positive
</assistant_response>

<product_review id="example-2">
Battery life is okay, but the ear pads feel cheap.
</product_review>

<assistant_response id="example-2">
Neutral
</assistant_response>

<product_review id="example-3">
Terrible customer service, I'll never buy from them again.
</product_review>

<assistant_response id="example-3">
Negative
</assistant_response>

Typical model output

This JSON payload captures an AI assistant’s chat response, complete with identifying metadata and the actual narrative content it generated.

[
  {
    "id": "msg_67b73f697ba4819183a15cc17d011509",
    "type": "message",
    "role": "assistant",
    "content": [
      {
        "type": "output_text",
        "text": "Under the soft glow of the moon, Luna the unicorn danced through fields of twinkling stardust, leaving trails of dreams for every child asleep.",
        "annotations": []
      }
    ]
  }
]

Chain of Thought (CoT)

Chain of Thought (CoT) Prompting was first introduced by Wei et al., 2023. By adding prompts that guide logical reasoning, it improves the output quality of complex reasoning tasks.

Key Points

  • Prerequisite: Model parameter count must exceed 50B.
  • Effectiveness: For simple problems, CoT provides limited quality improvement.

Comparison Example

Message formatting

When crafting developer and user messages, you can combine Markdown formatting and XML tags to help the model understand the logical boundaries of prompts and contextual data.

  • Markdown headers and lists help mark different sections of the prompt and convey hierarchical structure. They also improve readability during development.
  • XML tags help delimit specific content blocks (e.g., support documents for reference) and define metadata via XML attributes that can be referenced in your instructions.

Typically, developer messages should contain the following sections, generally in this order (though optimal content and order may vary by model):

  1. Identity: Describes the assistant’s purpose, communication style, and high-level goals.
  2. Instructions: Provides guidance on how to generate desired responses. What rules should it follow? What should the model do, and what should it absolutely avoid? May include sub-sections like custom function calling guidelines.
  3. Examples: Provides sample inputs and corresponding expected outputs.
  4. Context: Supplies any additional information needed for response generation (e.g., non-public/proprietary data not in training data, or particularly relevant information). Best placed near the end of the prompt as different generation requests may contain different contexts.

Example Prompt Structure

# Identity
You are coding assistant that helps enforce the use of snake case variables in JavaScript code, and writing code that will run in Internet Explorer version 6.
# Instructions
* When defining variables, use snake case names (e.g. my_variable) instead of camel case names (e.g. myVariable).
* To support old browsers, declare variables using the older "var" keyword.
* Do not give responses with Markdown formatting, just return the code as requested.
# Examples
<user_query>
How do I declare a string variable for a first name?
</user_query>
<assistant_response>
var first_name = "Anna";
</assistant_response>

Reference: Open AI prompting guide