Optimize agents
The Opik Agent Optimizer can optimize both simple prompts and complex agent workflows. For most use cases, you can optimize prompts directly using ChatPrompt. When you need multi-prompt workflows, agent orchestration, or custom execution logic, you’ll use OptimizableAgent to create a custom agent class.
When to use OptimizableAgent vs ChatPrompt
Use ChatPrompt directly (default approach):
- Single-prompt optimization - optimizing one prompt template
- Most common use case
- No custom execution logic needed
Use OptimizableAgent when you need:
- Multi-prompt workflows - orchestrating multiple prompts in sequence
- Agent framework integration - connecting to ADK, LangGraph, CrewAI, etc.
- Custom execution logic - special tool handling, async workflows, etc.
Optimizers work seamlessly with both approaches. The optimizer calls your agent’s invoke_agent() method repeatedly during optimization, passing different prompt candidates to evaluate.
Single-prompt optimization
For most optimization tasks, you can use ChatPrompt directly without creating a custom agent. The optimizer uses a default LiteLLM-based agent under the hood.
Custom agent for framework integration
When integrating with specific agent frameworks (Google ADK, LangGraph, CrewAI, etc.), you’ll create a custom OptimizableAgent subclass. This allows the optimizer to work with your framework’s execution model.
Here’s an example for Google ADK:
The key points:
- Extract the single prompt from
promptsdict:prompt = list(prompts.values())[0] - Get formatted messages:
messages = prompt.get_messages(dataset_item) - Execute using your framework and return the response string
See sdks/opik_optimizer/scripts/llm_frameworks/ for working examples of framework integrations (ADK, LangGraph, CrewAI, etc.). Each script doubles as both documentation and regression tests.
Multi-prompt optimization
For multi-step agent workflows, you must use OptimizableAgent because ChatPrompt only handles a single prompt. Multi-prompt optimization allows you to optimize multiple prompts that work together in a pipeline.
When to use multi-prompt optimization
- Sequential reasoning workflows (analyze → respond)
- Multi-hop retrieval pipelines
- Agent orchestration with multiple steps
- Any workflow where one prompt’s output feeds into another
Implementing a multi-prompt agent
Here’s a simple example of a two-step workflow that analyzes input and then generates a response:
Using the multi-prompt agent
When optimizing, pass a dictionary of prompts instead of a single prompt:
The optimizer will optimize both prompts in the dictionary, trying different combinations to improve performance.
The prompts dict keys (like “analyze” and “respond”) are used to identify which prompt to optimize. The optimizer can optimize all prompts or specific ones based on the optimize_prompt parameter.
Key implementation details
invoke_agent() method signature
All OptimizableAgent subclasses must implement invoke_agent():
Parameters:
prompts: Dictionary mapping prompt names toChatPromptobjectsdataset_item: Dataset row used to format prompt messagesallow_tool_use: Whether tools may be executed (for tool-calling prompts)seed: Optional random seed for reproducibility
Returns: A single string output that will be scored by your metric function
Extracting messages from prompts
Use ChatPrompt.get_messages() to format the prompt with dataset values:
For multi-prompt workflows, pass additional context when calling get_messages():
Best practices
- Error handling: Return meaningful error messages if execution fails
- Model parameters: Respect
prompt.modelandprompt.model_kwargsfor consistency - Reproducibility: Use the
seedparameter when making LLM calls - Opik tracing: The base class handles tracing automatically, but you can add custom metadata via
self.trace_metadata
Complete examples
Single-prompt with ChatPrompt (default)
Multi-prompt workflow
For advanced multi-prompt examples, see sdks/opik_optimizer/benchmarks/agents/hotpot_multihop_agent.py which implements a complex multi-hop retrieval pipeline with Wikipedia search.
Next steps
- Explore optimization algorithms to choose the right optimizer
- Learn about defining datasets and metrics
- Check framework-specific examples in
sdks/opik_optimizer/scripts/llm_frameworks/