Extending Optimizers
Opik Agent Optimizer is designed to be a flexible framework for prompt and agent optimization. While
it provides a suite of powerful built-in algorithms, you might have unique optimization strategies or
specialized needs. This guide shows how to build your own optimizer by extending the BaseOptimizer
class that all built-in optimizers use.
Architecture Overview
All optimizers in the SDK extend BaseOptimizer, giving you access to the same infrastructure:
Core Concepts for a Custom Optimizer
To design a new optimization algorithm within Opik’s ecosystem, your optimizer needs to interact with several key components:
-
Prompt (
ChatPrompt): Your optimizer takes aChatPromptobject as input. The chat prompt is a list of messages, where each message has a role, content, and optional additional fields. This includes variables that need to be replaced with actual values. -
Evaluation Mechanism (Metric & Dataset): Your optimizer needs a way to score candidate prompts. This is achieved by creating a
metric(function that takesdataset_itemandllm_outputas arguments and returns a float) and an evaluationdataset. -
Optimization Loop: This is the heart of your custom optimizer. It involves:
- Candidate Generation: Logic for creating new prompt variations. This could be rule-based, LLM-driven, or based on any other heuristic.
- Candidate Evaluation: Using the
metricanddatasetto get a score for each candidate. - Selection/Progression: Logic to decide which candidates to keep, refine further, or how to adjust the generation strategy based on scores.
- Termination Condition: Criteria for when to stop the optimization (e.g., number of rounds, score threshold, no improvement).
-
Returning Results (
OptimizationResult): Upon completion, your optimizer returns anOptimizationResultobject that standardizes how results are reported, including the best prompt found, its score, history of the optimization process, and cost/usage metrics.
Creating a Custom Optimizer
Step 1: Define Your Optimizer Class
Extend BaseOptimizer and define your DEFAULT_PROMPTS - the internal prompts your algorithm uses:
Step 2: Implement the optimize_prompt() Method
This is the core method implementing your optimization logic:
Step 3: Implement Candidate Generation
Your custom logic to create new prompt variations. Use get_prompt() to access internal prompts (which respects user’s prompt_overrides):
What BaseOptimizer Provides
The BaseOptimizer class provides robust mechanisms for prompt evaluation that all existing optimizers leverage. Your
custom optimizer reuses these internal evaluation utilities to ensure consistency with the Opik ecosystem.
Using Structured Outputs
For complex generation, use Pydantic models for structured LLM responses:
How to Contribute
Opik is continuously evolving, and community contributions are valuable!
- Feature Requests & Ideas: If you have ideas for new optimization algorithms, features, or improvements to existing ones, please share them through our community channels or by raising an issue on our GitHub repository.
- Bug Reports: If you encounter issues or unexpected behavior, detailed bug reports are greatly appreciated.
- Use Cases & Feedback: Sharing your use cases and how Opik Agent Optimizer is (or isn’t) meeting your needs helps us prioritize development.
- Code Contributions: Pull requests for new optimizers are welcome! See the contribution guide for detailed instructions.
Key Takeaways
- Extend
BaseOptimizerto create custom optimization algorithms with full access to Opik’s infrastructure - Define
DEFAULT_PROMPTSfor your algorithm’s internal prompts - users can customize these viaprompt_overrides - Implement
optimize_prompt()with your optimization logic, using the inheritedevaluate_prompt()to score candidates - Return standardized
OptimizationResultobjects for consistent reporting and dashboard integration - Use
_llm_calls.call_model()for LLM interactions with automatic cost/usage tracking
We encourage you to explore the existing optimizer algorithms to see different approaches to these challenges.
Related
- Custom optimizer prompts - Customize internal prompts
- Custom metrics - Build evaluation metrics
- API Reference - Full parameter documentation