It’s easy to organize and version your prompts inside of PromptLayer. This guide explains how to save a new version and retrieve the latest one programmatically.

This module requires an existing prompt in your PromptLayer account. Please follow the Getting Started guide to create one if needed.

Update an Existing Prompt

Every time you update a prompt in PromptLayer, a new version is created. You will always be able to track changes, compare versions, or revert if needed.

Example

In the Getting Started Onboarding, we created a prompt called “ai-poet” to generate a haiku on a given topic. Initially, it uses a single input variable for the topic:

SYSTEM:
You are a skilled poet specializing in haikus.
Your task is to write a haiku based on a topic provided by the user.
The haiku must have 17 syllables, structured in three lines of 5, 7, and 5.

USER:
{topic}

Next, we will update the prompt to allow users to specify a language.

  1. First, add a new input variable {language}
  2. Update the system prompt to instruct the AI to consider both the topic and the language.

Instructions

  1. Navigate to the Prompt Registry.
  2. Select the existing prompt you want to update (e.g., “ai-poet”).
  3. Click the Edit Version #1 button to open the prompt in an editor.
  4. Make some changes
  5. Click Update Template to save these edits as a new version.

Additional Features

  • Label prompt versions for better organization (e.g., “prod” or “dev”). (Learn more about release labels.)
  • Compare previous iterations to track improvements.
  • Use A/B testing to evaluate performance differences. (Learn more)

Retrieve Your Prompts from Code

To keep your PromptLayer prompts synchronized with your codebase, fetch the latest prompt version using the SDK.

  1. Obtain an API Key by going to SettingsPromptLayer API Keys.
  2. Initialize the PromptLayer client in your application.
from promptlayer import PromptLayer

pl_client = PromptLayer(api_key="YOUR_API_KEY")
  1. Retrieve the prompt and run it. This will pull the latest prompt version, run it client side, and log the results.
response = pl_client.run(prompt_name="ai-poet")

print(response)

Once you’ve set up the SDK and your codebase is properly synced with PromptLayer:

  • The latest or production version of your prompts will always synchronized.
  • New prompt versions can be deployed without requiring engineering code deploys.

Additional Resources: