1- """
2- Routing service implementation.
3-
4- This service manages query routing to appropriate agents using an LLM-based
5- analysis. It defaults to a small, low-cost model for routing to minimize
6- overhead while maintaining quality.
7- """
8-
91import logging
102from typing import Dict , List , Optional , Any
113from solana_agent .interfaces .services .routing import (
@@ -40,7 +32,8 @@ def __init__(
4032 self .agent_service = agent_service
4133 self .api_key = api_key
4234 self .base_url = base_url
43- self .model = model or "gpt-4.1"
35+ # Use a small, cheap model for routing unless explicitly provided
36+ self .model = model or "gpt-4.1-mini"
4437 # Simple sticky session: remember last routed agent in-process
4538 self ._last_agent = None
4639
@@ -80,17 +73,18 @@ async def _analyze_query(self, query: str) -> Dict[str, Any]:
8073
8174 USER QUERY: { query }
8275
76+ ROUTING RULES:
77+ - Match the user query to the agent whose specialization best fits the user's intent
78+ - Return the EXACT agent name that matches best
79+
8380 INSTRUCTIONS:
84- - Look at the user query and match it to the most appropriate agent from the list above
85- - If the user mentions a specific topic or need that matches an agent's specialization, choose that agent
86- - Return the EXACT agent name (not the specialization description)
87-
88- Please determine:
89- 1. primary_agent: The exact name of the best matching agent (e.g., "onboarding", "support")
90- 2. secondary_agents: Names of other agents that might help (empty list if none)
91- 3. complexity_level: 1-5 (5 being most complex)
92- 4. topics: Key topics mentioned
93- 5. confidence: 0.0-1.0 (how confident you are in this routing decision)
81+ - primary_agent: The exact name of the best matching agent (e.g., "onboarding", "event_feedback")
82+ - secondary_agents: Other agents that might help (usually empty)
83+ - complexity_level: 1-5 (5 being most complex)
84+ - topics: Key topics mentioned
85+ - confidence: 0.0-1.0 (how confident you are in this routing decision)
86+
87+ For the query "{ query } ", which agent should handle it?
9488 """
9589
9690 try :
@@ -113,12 +107,14 @@ async def _analyze_query(self, query: str) -> Dict[str, Any]:
113107 "confidence" : analysis .confidence ,
114108 }
115109 except Exception as e :
116- logger .error (f"Error analyzing query: { e } " ) # Use logger.error
110+ logger .error (f"Error analyzing query: { e } " )
111+ logger .debug (f"Query that failed: { query } " )
112+ logger .debug (f"Available agents: { list (agents .keys ())} " )
117113 # Return default analysis on error
114+ first_agent = list (agents .keys ())[0 ] if agents else "general"
115+ logger .debug (f"Defaulting to first agent: { first_agent } " )
118116 return {
119- "primary_specialization" : list (agents .keys ())[0 ]
120- if agents
121- else "general" ,
117+ "primary_specialization" : first_agent ,
122118 "secondary_specializations" : [],
123119 "complexity_level" : 1 ,
124120 "topics" : [],
0 commit comments