Design Optimize Prompts
---
lab:
title: 'Design and optimize prompts'
description: 'Use Git-based experimentation workflow to systematically test and evaluate prompt optimizations'
level: 300
duration: 40 minutes
---
# Design and optimize prompts
This exercise takes approximately **40 minutes**.
> **Note**: This lab assumes a pre-configured lab environment with Visual Studio Code, Azure CLI, and Python already installed.
## Introduction
In this exercise, you'll test prompt optimizations for the Adventure Works Trail Guide Agent using a Git-based experimentation workflow. You'll first establish a quantified baseline using the current production prompt, then create experiment branches to test optimization variants. You'll run automated scripts to capture agent responses, manually score quality, and compare results to make evidence-based decisions about which optimization to deploy to production.
**Scenario**: You're operating the Adventure Works Trail Guide Agent with a v3 production prompt. Before optimizing, you'll measure baseline performance. Then you'll test if a token-optimized prompt (v4) can maintain quality while reducing costs by 40-50%. Finally, you'll test the same optimized prompt with the GPT-4.1-mini model to see if it can further reduce costs while maintaining acceptable quality.
## Set up the environment
To complete the tasks in this exercise, you need:
- Visual Studio Code
- Azure subscription with Microsoft Foundry access
- Git and [GitHub](https://github.com) account
- Python 3.9 or later
- Azure CLI and Azure Developer CLI (azd) installed
> **Tip**: If you haven't installed these prerequisites yet, see [Lab 00: Prerequisites](00-prerequisites.md) for installation instructions and links.
All steps in this lab will be performed using Visual Studio Code and its integrated terminal.
### Create repository from template
You'll start by creating your own repository from the template to practice realistic workflows.
1. In a web browser, navigate to the template repository on [GitHub](https://github.com) at `https://github.com/MicrosoftLearning/mslearn-genaiops`.
1. Select **Use this template** > **Create a new repository**.
1. Enter a name for your repository (e.g., `mslearn-genaiops`).
1. Set the repository to **Public** or **Private** based on your preference.
1. Select **Create repository**.
### Clone the repository in Visual Studio Code
After creating your repository, clone it to your local machine.
1. In Visual Studio Code, open the Command Palette by pressing **Ctrl+Shift+P**.
1. Type **Git: Clone** and select it.
1. Enter your repository URL: `https://github.com/[your-username]/mslearn-genaiops.git`
1. Select a location on your local machine to clone the repository.
1. When prompted, select **Open** to open the cloned repository in VS Code.
### Deploy Microsoft Foundry resources
Now you'll use the Azure Developer CLI to deploy all required Azure resources.
1. In Visual Studio Code, open a terminal by selecting **Terminal** > **New Terminal** from the menu.
1. Authenticate with Azure Developer CLI:
```powershell
azd auth login
```
This opens a browser window for Azure authentication. Sign in with your Azure credentials.
1. Authenticate with Azure CLI:
```powershell
az login
```
Sign in with your Azure credentials when prompted.
> ⚠️ **Important**
> In some environments, the VS Code integrated terminal may crash or close during the interactive login flow.
> If this happens, authenticate using explicit credentials instead:
> ```powershell
> az login --username <your-username> --password <your-password>
> ```
1. Provision resources:
```powershell
azd up
```
When prompted, provide:
- **Environment name** (e.g., `dev`, `test`) - Used to name all resources
- **Azure subscription** - Where resources will be created
- **Location** - Azure region (recommended: Sweden Central)
The command deploys the infrastructure from the `infra\` folder, creating:
- **Resource Group** - Container for all resources
- **Foundry (AI Services)** - The hub with access to models like GPT-4.1
- **Foundry Project** - Your workspace for creating and managing prompts
- **Log Analytics Workspace** - Collects logs and telemetry data
- **Application Insights** - Monitors performance and usage
1. Create a `.env` file with the environment variables:
```powershell
azd env get-values > .env
```
> ⚠️ **Important – File Encoding**
>
> After generating the `.env` file, make sure it is saved using **UTF-8** encoding.
>
> In editors like **VS Code**, check the encoding indicator in the bottom-right corner.
> If it shows **UTF-16 LE** (or any encoding other than UTF-8), click it, choose **Save with Encoding**, and select **UTF-8**.
>
> Using the wrong encoding may cause environment variables to be read incorrectly.
This creates a `.env` file in your project root with all the provisioned resource information.
### Install Python dependencies
With your Azure resources deployed, install the required Python packages.
1. In the VS Code terminal, create and activate a virtual environment:
```powershell
python -m venv .venv
.venv\Scripts\Activate.ps1
```
1. Install the required dependencies:
```powershell
python -m pip install -r requirements.txt
```
This installs necessary dependencies including:
- `azure-ai-projects` - SDK for working with Microsoft Foundry
- `azure-identity` - Azure authentication
- `python-dotenv` - Load environment variables
1. Add the agent configuration to your `.env` file:
Open the `.env` file in your repository root and add:
```
AGENT_NAME="trail-guide"
MODEL_NAME="gpt-4.1"
```
## Understand the experimental workflow
The repository contains the Trail Guide Agent, along with folders for testing and experiments:when to use it
Community prompt sourced from the open-source GitHub repo Hirdeshpal15/Enterprise-Service-Desk-Agent (MIT). A "Design Optimize Prompts" style prompt — adapt the placeholders and specifics to your task. Imported as-is and not independently retested here, so check the output before relying on it.
tags
codingcommunitydeveloper
source
Hirdeshpal15/Enterprise-Service-Desk-Agent · MIT
more in Coding
Coding✓ tested
Senior code review (strict mode)
senior staff engineer running a merciless but fair review
Coding✓ tested
Debug by hypothesis, not by guessing
debugging partner who forms theories before touching code
Coding✓ tested
Generate tests from described behavior
test engineer who writes tests that would actually catch regressions