Speeding Up the Agent › Choosing the Right Model for the Task
Michael BrenndoerferAugust 24, 20252 min read
AI Agent HandbookAll chapters Part 15 of 15Performance and Cost Optimization - Doing More with Less
Speeding Up the Agent › Choosing the Right Model for the TaskOne section · Sections in this chapter4 / 15 ↓
Sections in this chapter15
Not every task needs your most powerful model. Claude Sonnet 4.5 is excellent for complex reasoning and tool use, but it's overkill for simple questions. Using a smaller, faster model for straightforward tasks can cut response time in half or more.
Think of it like transportation. You wouldn't take a semi-truck to pick up groceries. A car works fine. Similarly, you don't need your most capable model for every query.
Example: Model Selection Strategy (GPT-5)
Let's build a simple router that chooses the right model based on the query complexity:
In[4]:
Codefrom openai import OpenAI
from anthropic import Anthropic
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
def classify_query_complexity(query):
"""Determine if a query needs a powerful model or a fast one."""
# Simple heuristic: check for complexity indicators
complex_indicators = [
"explain", "analyze", "compare", "why", "how does",
"step by step", "reasoning", "pros and cons"
]
query_lower = query.lower()
is_complex = any(indicator in query_lower for indicator in complex_indicators)
return "complex" if is_complex else "simple"
def route_to_model(query):
"""Route query to appropriate model based on complexity."""
complexity = classify_query_complexity(query)
if complexity == "simple":
# Use GPT-5 for fast, straightforward responses
model = "gpt-5"
max_tokens = 150
else:
# Use Claude Sonnet 4.5 for complex reasoning
anthropic_client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
response = anthropic_client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
messages=[{"role": "user", "content": query}]
)
return response.content[0].text
# Handle simple queries with GPT-5
response = client.chat.completions.create(
model=model,
max_completion_tokens=max_tokens,
messages=[{"role": "user", "content": query}]
)
return response.choices[0].message.content
## Test both paths
print("Simple query:", route_to_model("What's the capital of France?"))
print("Complex query:", route_to_model("Explain why France chose Paris as its capital"))Out[4]:
ConsoleSimple query: Paris.
Complex query: # Why Paris Became France's Capital Paris became France's capital through a combination of **historical, geographical, and political factors** rather than a single deliberate choice: ## Geographic Advantages - **Central location** in the fertile Paris Basin (Île-de-France region) - Positioned on the **Seine River**, enabling trade and transportation - Natural defensive position on islands in the river (Île de la Cité) ## Historical Development - **Ancient roots**: Roman settlement called Lutetia (circa 250 BC) - **Clovis I** (5th century) made it his royal residence when he unified Frankish tribes - **Hugh Capet** (987 AD) established his power base there, making it the de facto capital of the growing French kingdom - Gradually accumulated royal palaces, administrative functions, and political institutions ## Strategic Growth - As French kings expanded their territory from their Île-de-France base, Paris remained the center of royal power - Became the **economic and cultural hub** - universities, churches, markets - By the Middle Ages, it was the largest city in Western Europe ## No Formal Declaration Interestingly, Paris was never officially declared the capital by law until the **French Constitution of 1958**. It simply evolved into that role through centuries of being the seat of power. The choice was essentially organic - Paris grew powerful because French kings ruled from there, and it remained capital because it had become too important politically, economically, and culturally to move.
This approach gives you speed when you need it and power when you need it. The simple query gets a fast response from GPT-5, while the complex one gets the full reasoning capability of Claude Sonnet 4.5.