Prompts
The Opik TypeScript SDK provides comprehensive prompt management functionality for versioning, storing, and formatting your LLM prompt templates. Prompts in Opik are versioned automatically, allowing you to track changes over time while seamlessly integrating with your codebase.
Introduction
A prompt in Opik is a versioned template with variables that can be formatted at runtime. Each prompt:
- Has a unique name and auto-generated versions
- Supports Mustache or Jinja2 template syntax
- Tracks metadata, tags, and change descriptions
- Maintains complete version history
- Enables version comparison and rollback
Getting Started
Creating Your First Prompt
Create a prompt with the createPrompt
method:
Retrieving Prompts
Get prompts by name or specific version:
Formatting Prompts
Opik supports two powerful template engines:
- Mustache - Simple, logic-less templates with
{{variable}}
placeholders (default) - Jinja2 - Advanced templating with control flow using
{% %}
blocks and{{ }}
variables
The format()
method works on both Prompt
and PromptVersion
instances:
Template Syntax Examples:
Core Operations
Creating and Updating
Understanding Version Creation
The createPrompt
method intelligently handles versioning based on content changes:
New version is created when:
- Template content (
prompt
) changes - Metadata changes (deep equality check)
- Template type (
type
) changes
No new version (returns existing) when:
- Template, metadata, and type are all identical
- Only
tags
ordescription
differ
Version creation is triggered by changes to template content,
metadata, or type. Changes to tags
, name
or description
alone do
not create new versions - use updateProperties()
for those.
Updating Prompt Properties
Update prompt metadata without creating new versions:
Updating properties (name
, description
, tags
) does not create a new
version. Only changes to the template content, metadata, or type
trigger version creation.
Retrieving and Searching
Searching with Opik Query Language
Search and filter prompts using Opik Query Language (OQL) - a powerful SQL-like syntax for finding exactly the prompts you need.
Supported Fields:
Search Examples:
Deleting Prompts
Version Management
Understanding Versions
Version Metadata and Properties
Access comprehensive version information:
Viewing Version History
Working with Versions
Getting Specific Versions
Comparing Versions
Restoring Previous Versions
Advanced Usage
Integration with Tracing
Prompts work seamlessly with Opik’s tracing functionality:
Best Practices
Store Prompts in Code
Keep your prompts versioned alongside your code:
Use Meaningful Version Descriptions
Tag Your Prompts
API Reference
OpikClient Methods
createPrompt(options)
Creates a new prompt or returns existing version if content unchanged.
Arguments:
options.name: string
- Prompt name (required)options.prompt: string
- Template content (required)options.type?: PromptType
- Template engine (default:MUSTACHE
)options.promptId?: string
- Optional prompt IDoptions.description?: string
- Optional descriptionoptions.metadata?: JsonNode
- Optional metadataoptions.changeDescription?: string
- Version change descriptionoptions.tags?: string[]
- Optional tags
Returns: Promise<Prompt>
- Created or existing prompt
getPrompt(options)
Retrieves a prompt by name and optional version.
Arguments:
options.name: string
- Prompt name (required)options.commit?: string
- Optional commit hash for specific version
Returns: Promise<Prompt | null>
- Prompt instance or null if not found
searchPrompts(filterString?)
Searches prompts with optional OQL filtering.
Arguments:
filterString?: string
- Optional OQL filter expression
Returns: Promise<Prompt[]>
- Array of matching prompts
Supported OQL fields:
id
,name
,created_by
: String fieldstags
: List field (use “contains” operator)
Operators: =
, !=
, contains
, not_contains
, starts_with
, ends_with
, >
, <
deletePrompts(ids)
Deletes multiple prompts and all their versions.
Arguments:
ids: string[]
- Array of prompt IDs to delete
Returns: Promise<void>
Prompt Class
Methods
format(variables)
Formats the prompt template with provided variables.
Arguments:
variables: Record<string, unknown>
- Variables to substitute
Returns: string
- Formatted prompt text
Throws: PromptValidationError
if required variables missing (Mustache only)
getVersions()
Retrieves all version history for this prompt.
Returns: Promise<PromptVersion[]>
- Array of all versions (newest first)
getVersion(commit)
Gets a specific version as a Prompt instance.
Arguments:
commit: string
- Commit hash (8-char or full)
Returns: Promise<Prompt | null>
- Prompt instance or null if not found
useVersion(version)
Restores a specific version by creating a new version with old content.
Arguments:
version: PromptVersion
- Version object to restore
Returns: Promise<Prompt>
- New prompt instance with restored content
updateProperties(updates)
Updates prompt properties without creating new version.
Arguments:
updates.name?: string
- New prompt nameupdates.description?: string
- New descriptionupdates.tags?: string[]
- New tags array
Returns: Promise<this>
- This prompt instance (for chaining)
delete()
Deletes this prompt and all its versions.
Returns: Promise<void>
Properties
id: string
- Unique prompt identifiername: string
- Prompt nameprompt: string
- Current template contentcommit?: string
- Current version commit hashtype: PromptType
- Template engine typedescription?: string
- Prompt descriptiontags?: readonly string[]
- Prompt tagsmetadata?: JsonNode
- Prompt metadatachangeDescription?: string
- Latest version change description
PromptVersion Class
Methods
format(variables)
Formats this version’s template with provided variables.
Arguments:
variables: Record<string, unknown>
- Variables to substitute
Returns: string
- Formatted prompt text
getVersionInfo()
Gets formatted version information string.
Returns: string
- Format: [commit] YYYY-MM-DD by user@email.com - Change description
getVersionAge()
Gets human-readable version age.
Returns: string
- Format: “2 days ago”, “Today”, etc.
compareTo(other)
Compares this version’s template with another version.
Arguments:
other: PromptVersion
- Version to compare against
Returns: string
- Git-style unified diff showing changes
Properties
id: string
- Version unique identifiername: string
- Associated prompt nameprompt: string
- Template content for this versioncommit: string
- Version commit hashtype: PromptType
- Template engine typemetadata?: JsonNode
- Version metadatachangeDescription?: string
- Version change descriptioncreatedAt?: Date
- Creation timestampcreatedBy?: string
- Creator identifier