Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

fix: update step with resolved model info even when auto mode resolution fails partway (#10300)

cthomas775b63eca227
diff --git a/letta/agents/letta_agent_v3.py b/letta/agents/letta_agent_v3.py
index 5e81b7bfc..8dabe5784 100644
--- a/letta/agents/letta_agent_v3.py
+++ b/letta/agents/letta_agent_v3.py
@@ -1046,40 +1046,45 @@ class LettaAgentV3(LettaAgentV2):
primary_handle = ""
if is_auto_mode:
- routing_client = await get_llm_routing_client()
- active_llm_config, is_primary, primary_handle = await routing_client.resolve_auto_mode_config(
- stored_llm_config=self.agent_state.llm_config,
- actor=self.actor,
- )
- if not is_primary:
- self.logger.info(f"[LLM ROUTER]: primary {primary_handle} rerouted, falling back to {active_llm_config.handle}")
- # Content-based rerouting (e.g. images → vision-capable model)
- active_llm_config = routing_client.apply_reroute_rules(
- resolved_config=active_llm_config,
- messages=messages,
- stored_llm_config=self.agent_state.llm_config,
- agent_state=self.agent_state,
- )
- active_llm_client = LLMClient.create(
- provider_type=active_llm_config.model_endpoint_type,
- put_inner_thoughts_first=True,
- actor=self.actor,
- )
- # Update the adapter to use the resolved client and config
- llm_adapter.llm_client = active_llm_client
- llm_adapter.llm_config = active_llm_config
- # Update persisted step with resolved model info so billing can
- # identify the actual model and charge at the correct rate.
- # Keep model_handle as the original auto handle so we can still
- # identify that the user was on auto mode.
- await self.step_manager.update_step_resolved_model_async(
- actor=self.actor,
- step_id=step_id,
- provider_name=active_llm_config.model_endpoint_type,
- provider_category=active_llm_config.provider_category or "base",
- model=active_llm_config.model,
- model_endpoint=active_llm_config.model_endpoint,
- )
+ resolved_llm_config = None
+ try:
+ routing_client = await get_llm_routing_client()
+ active_llm_config, is_primary, primary_handle = await routing_client.resolve_auto_mode_config(
+ stored_llm_config=self.agent_state.llm_config,
+ actor=self.actor,
+ )
+ resolved_llm_config = active_llm_config
+ if not is_primary:
+ self.logger.info(f"[LLM ROUTER]: primary {primary_handle} rerouted, falling back to {active_llm_config.handle}")
+ # Content-based rerouting (e.g. images → vision-capable model)
+ active_llm_config = routing_client.apply_reroute_rules(
+ resolved_config=active_llm_config,
+ messages=messages,
+ stored_llm_config=self.agent_state.llm_config,
+ agent_state=self.agent_state,
+ )
+ resolved_llm_config = active_llm_config
+ active_llm_client = LLMClient.create(
+ provider_type=active_llm_config.model_endpoint_type,
+ put_inner_thoughts_first=True,
+ actor=self.actor,
+ )
+ # Update the adapter to use the resolved client and config
+ llm_adapter.llm_client = active_llm_client
+ llm_adapter.llm_config = active_llm_config
+ finally:
+ # Update persisted step with resolved model info so billing can
+ # identify the actual model and charge at the correct rate,
+ # even if resolution fails partway through.
+ if resolved_llm_config is not None:
+ await self.step_manager.update_step_resolved_model_async(
+ actor=self.actor,
+ step_id=step_id,
+ provider_name=resolved_llm_config.model_endpoint_type,
+ provider_category=resolved_llm_config.provider_category or "base",
+ model=resolved_llm_config.model,
+ model_endpoint=resolved_llm_config.model_endpoint,
+ )
else:
active_llm_config = self.agent_state.llm_config
active_llm_client = self.llm_client