Ultimate Guide to LLM Prompt Testing: A Hands-on Tutorial with Promptfoo

Manish Tyagi
5 min read1 day ago

--

When your prompts work in testing but fail in production

In the rapidly evolving landscape of AI development, ensuring consistent and reliable prompt engineering has become crucial. Have you ever felt like your prompts are playing a game of “Will It Work Today?” If you’re nodding your head (or crying into your keyboard), you’re not alone. Let’s dive into how Promptfoo can save your sanity and make prompt testing actually… fun? Yes, really!

The Prompt Testing Problem (AKA “Why Is This Happening to Me?”)

Before diving into promptfoo, let’s address a fundamental question: Why do we need systematic prompt testing? When working with Large Language Models (LLMs), small changes in prompts can lead to significant variations in outputs. Picture this: You craft the perfect prompt for your AI model. It works beautifully. You celebrate with a victory dance. Then you try it again tomorrow, and…

Expectation vs Reality in Prompt Engineering

Sound familiar? Here’s what typically goes wrong:

  • Your prompt works great… until it doesn’t
  • You’re spending more time testing than building ⏰
  • Different models give wildly different answers
  • Your team members keep changing prompts without telling anyone 🤫

Say hello to Promptfoo 👋

Promptfoo is an open-source tool that brings software engineering best practices to prompt development. Promptfoo addresses these challenges by providing a structured framework for testing prompts across different models and configurations. It’s like having a super-organized friend who helps you test everything systematically. Key features include:

  • Declarative Testing: Define expected outputs and assertions
  • Multi-Provider Support: Test across different LLM providers
  • Automated Evaluation: Run tests in CI/CD pipelines
  • Version Control: Track prompt changes over time
  • Cost Optimization: Estimate and track API costs
  1. Getting Started (Super Easy Mode)

First, let’s get promptfoo on your computer. Open your terminal (don’t worry, it won’t bite):

npm install -g promptfoo

// verify the installation

promptfoo --version

// start using promptfoo by running
promptfoo init

That’s it! You’re already halfway to prompt testing nirvana! 🎉

2. Your First Test (Baby Steps)

Let’s start with something simple. Running `promptfoo init` will create a file called `promptfooconfig.yaml` (fancy name, right?) which you can edit and use for testing purpose:

# The most basic promptfoo config ever!
prompts:
- Tell me a dad joke about {{topic}}

providers:
- openai:gpt-3.5-turbo # Your friendly neighborhood AI

tests:
- vars:
topic: programming
assert:
- type: contains
value: joke
- type: javascript
value: output.length < 200 # Keep it short and sweet!

3. Run your first test:

//This tests every prompt, model, and test case - displaying the eval on your terminal
promptfoo eval

// You can open the web viewer to review the outputs as well, just type:
promptfoo view
How it feels when all tests pass

Let’s Get Serious (But Keep It Fun) 🎮

Now that we’ve got our feet wet, let’s dive into the cool stuff!

Promptfoo can do way more than just basic tests. Here’s a super-powered example:

prompts:
- |
System: You are a helpful coding assistant who loves dad jokes.
User: Write a {{language}} function to {{task}}
Assistant: Here's a solution with a sprinkle of humor!

providers:
- openai:gpt-4
- openai:gpt-3.5-turbo
- anthropic:claude-2

tests:
- vars:
language: Python
task: calculate fibonacci numbers
assert:
- type: contains
value: def fibonacci
- type: regex
value: return.*\d+
- type: javascript
value: |
output.includes('def') &&
output.length < 500 &&
!output.includes('harmful')
  • The Customer Service Bot

Let’s test a customer service bot that needs to maintain a consistent tone:

prompts:
- |
System: You are a friendly customer service agent named Alex.
User: {{customer_message}}
Assistant: Let me help you with that!

tests:
- vars:
customer_message: "I'm really angry about my order!"
assert:
- type: contains
value: understand your frustration
- type: sentiment
value: positive
- type: custom-js
value: |
// Check if response is empathetic
return {
pass: output.toLowerCase().includes('sorry') ||
output.toLowerCase().includes('understand'),
score: 1.0,
reason: 'Response shows empathy'
}

Pro Tips That’ll Make You Look Like a Genius

1. Test Different Personalities

prompts:
- |
System: {{personality}}
User: Explain quantum computing
Assistant: Here's your explanation...

tests:
- vars:
personality: You are a surfer dude who loves physics
assert:
- type: contains
value: dude
- type: contains
value: quantum

2. Cost Saving Mode 💰

# Save those API dollars!
prompts:
- text: "{{query}}"
provider: openai:gpt-3.5-turbo # Use the cheaper model for initial tests

tests:
- vars:
query: "What's the meaning of life?"
assert:
- type: basic-test # Start with simple tests
A placeholder gif showing a meme of donald-duck counting money
Your wallet after implementing cost-saving measures

3. The Magic of Templates

Create reusable templates for common test patterns:

# templates/common-tests.yaml
tests:
safe_output:
- type: no-profanity
- type: max-length
value: 1000
grammar_check:
- type: spelling
- type: grammar

Troubleshooting Like a Boss

When Things Go Wrong (And They Will), remember -

Advanced Features (The Secret Sauce)

  • Custom Evaluation Functions
// evaluators/tone-check.js
module.exports = {
evaluate: async (output, expected) => {
const tone = analyzeTone(output); // Your tone analysis logic
return {
pass: tone === expected,
score: tone === expected ? 1.0 : 0.0,
reason: `Tone is ${tone}, expected ${expected}`
};
}
};
  • CI/CD Integration
# .github/workflows/prompt-tests.yml
name: Prompt Tests
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install -g promptfoo
- run: promptfoo eval

Conclusion

You’re Now a Prompt Testing Wizard! Remember:

  • Start small, test often
  • Use templates for consistency
  • Save money with strategic testing
  • Have fun with it!
A placeholder image showing graduation/success celebration
You after mastering Promptfoo

What’s Next?

Remember: Every great AI application started with someone like you, wondering how to make their prompts more reliable. Now go forth and test those prompts!

— -

P.S. This blog post was tested using promptfoo. Meta, right? 😉

--

--

Manish Tyagi

Helping organizations with GTM strategy for Product & Community Growth