fix: wire enable_tracing UI toggle to backend and optimize agent lookup

- Map enable_tracing form field to require_trace_id_on_calls_by_agent in
  buildAgentDataFromForm and parseAgentForForm so the tracing toggle
  actually takes effect end-to-end
- Replace O(n) linear scan in AgentRegistry.get_agent_by_id with O(1)
  dict lookup via _agent_by_id index, reducing latency in auth hot path
- Update test cleanup to preserve _agent_by_id consistency

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Krrish Dholakia 2026-03-04 19:02:17 -08:00
parent 4b06d3bef6
commit 9aa3afc8d7
3 changed files with 14 additions and 10 deletions

View file

@ -15,14 +15,21 @@ from litellm.types.agents import AgentConfig, AgentResponse, PatchAgentRequest
class AgentRegistry:
def __init__(self):
self.agent_list: List[AgentResponse] = []
self._agent_by_id: Dict[str, AgentResponse] = {}
def reset_agent_list(self):
self.agent_list = []
self._agent_by_id = {}
def register_agent(self, agent_config: AgentResponse):
self.agent_list.append(agent_config)
if agent_config.agent_id:
self._agent_by_id[agent_config.agent_id] = agent_config
def deregister_agent(self, agent_name: str):
for agent in self.agent_list:
if agent.agent_name == agent_name and agent.agent_id:
self._agent_by_id.pop(agent.agent_id, None)
self.agent_list = [
agent for agent in self.agent_list if agent.agent_name != agent_name
]
@ -358,16 +365,9 @@ class AgentRegistry:
agent_id: str,
) -> Optional[AgentResponse]:
"""
Get an agent by its ID from the database
Get an agent by its ID using O(1) dict lookup
"""
try:
for agent in self.agent_list:
if agent.agent_id == agent_id:
return agent
return None
except Exception as e:
raise Exception(f"Error getting agent from DB: {str(e)}")
return self._agent_by_id.get(agent_id)
def get_agent_by_name(self, agent_name: str) -> Optional[AgentResponse]:
"""

View file

@ -56,8 +56,9 @@ def test_a2a_registry_integration():
# Register and test
original_agents = global_agent_registry.agent_list.copy()
original_agent_by_id = global_agent_registry._agent_by_id.copy()
global_agent_registry.register_agent(test_agent)
try:
litellm.completion(
model="a2a/test-agent",
@ -68,6 +69,7 @@ def test_a2a_registry_integration():
assert "registry-url.example.com" in str(e) or "APIConnectionError" in str(type(e).__name__)
finally:
global_agent_registry.agent_list = original_agents
global_agent_registry._agent_by_id = original_agent_by_id
except ImportError:
pytest.skip("Registry not available (not in proxy context)")

View file

@ -278,6 +278,7 @@ export const buildAgentDataFromForm = (values: any, existingAgent?: any) => {
if (values.cost_per_query) params.cost_per_query = parseFloat(values.cost_per_query);
if (values.input_cost_per_token) params.input_cost_per_token = parseFloat(values.input_cost_per_token);
if (values.output_cost_per_token) params.output_cost_per_token = parseFloat(values.output_cost_per_token);
if (values.enable_tracing !== undefined) params.require_trace_id_on_calls_by_agent = values.enable_tracing;
if (Object.keys(params).length > 0) {
agentData.litellm_params = params;
@ -316,5 +317,6 @@ export const parseAgentForForm = (agent: any) => {
cost_per_query: agent.litellm_params?.cost_per_query,
input_cost_per_token: agent.litellm_params?.input_cost_per_token,
output_cost_per_token: agent.litellm_params?.output_cost_per_token,
enable_tracing: agent.litellm_params?.require_trace_id_on_calls_by_agent,
};
};