Model Optimizer intelligently routes user queries across different Gemini models (Pro, Flash, etc.) based on query complexity and your cost-quality preferences. This enables significant cost savings while maintaining response quality.
Model Optimizer analyzes each query and automatically selects the most appropriate model from the Gemini family, eliminating the need for manual model selection.
from IPython.display import Markdown, displaytest_queries = [ "What is 2+2?", "Explain the implications of quantum computing on cryptography", "Translate 'hello' to Spanish", "Analyze the economic factors that led to the 2008 financial crisis",]strategies = [ FeatureSelectionPreference.PRIORITIZE_QUALITY, FeatureSelectionPreference.BALANCED, FeatureSelectionPreference.PRIORITIZE_COST,]for query in test_queries: print(f"\n{'='*60}") print(f"Query: {query}") print(f"{'='*60}\n") for strategy in strategies: config = ModelSelectionConfig( feature_selection_preference=strategy ) response = client.models.generate_content( model="model-optimizer-exp-04-09", contents=query, config=GenerateContentConfig( model_selection_config=config, ), ) print(f"Strategy: {strategy.name}") print(f"Response length: {len(response.text)} chars") print(f"---\n")
Combine Model Optimizer with function calling for agentic workflows:
from google.genai.types import FunctionDeclaration, Tool# Define functiondef get_current_weather(location: str, unit: str = "celsius") -> dict: """Gets weather in the specified location. Args: location: The location for which to get the weather. unit: Temperature unit (celsius or fahrenheit). Returns: Weather information as a dictionary. """ return { "location": location, "unit": unit, "weather": "Sunny", "temperature": 22, }# Define function schemaweather_function = FunctionDeclaration( name="get_current_weather", description="Get the current weather in a given location", parameters={ "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA", }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], }, }, "required": ["location"], }, response={ "type": "object", "properties": { "location": {"type": "string"}, "unit": {"type": "string"}, "weather": {"type": "string"}, "temperature": {"type": "number"}, }, },)weather_tool = Tool(function_declarations=[weather_function])# Use with Model Optimizerresponse = client.models.generate_content( model="model-optimizer-exp-04-09", contents="What is the weather like in Boston?", config=GenerateContentConfig( tools=[weather_tool], model_selection_config=ModelSelectionConfig( feature_selection_preference=FeatureSelectionPreference.BALANCED ), ),)print("Function call:", response.function_calls)# Execute functionfunction_map = {"get_current_weather": get_current_weather}for function_call in response.function_calls: func = function_map[function_call.name] result = func(**function_call.args) print(f"Result: {result}")
Combine Model Optimizer with system instructions for consistent behavior:
system_instruction = """You are a helpful financial advisor assistant.Always provide clear, actionable advice.Cite sources when making specific claims."""response = client.models.generate_content( model="model-optimizer-exp-04-09", contents="Should I invest in index funds or individual stocks?", config=GenerateContentConfig( system_instruction=system_instruction, model_selection_config=ModelSelectionConfig( feature_selection_preference=FeatureSelectionPreference.PRIORITIZE_QUALITY ), ),)print(response.text)