Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix: llm error interrupting stream for agent loop (#5696)
cthomas06d2cde43d7c
diff --git a/letta/agents/letta_agent_v2.py b/letta/agents/letta_agent_v2.pyindex e42438db7..d36e2fa71 100644--- a/letta/agents/letta_agent_v2.py+++ b/letta/agents/letta_agent_v2.py@@ -1164,25 +1164,29 @@ class LettaAgentV2(BaseAgentV2):# TODO: This can be broken by bad configs, e.g. lower bound too high, initial messages too fat, etc.# TODO: `force` and `clear` seem to no longer be used, we should removeif not skip_summarization:- if force or (total_tokens and total_tokens > self.agent_state.llm_config.context_window):- self.logger.warning(- f"Total tokens {total_tokens} exceeds configured max tokens {self.agent_state.llm_config.context_window}, forcefully clearing message history."- )- new_in_context_messages, updated = await self.summarizer.summarize(- in_context_messages=in_context_messages,- new_letta_messages=new_letta_messages,- force=True,- clear=True,- )- else:- # NOTE (Sarah): Seems like this is doing nothing?- self.logger.info(- f"Total tokens {total_tokens} does not exceed configured max tokens {self.agent_state.llm_config.context_window}, passing summarizing w/o force."- )- new_in_context_messages, updated = await self.summarizer.summarize(- in_context_messages=in_context_messages,- new_letta_messages=new_letta_messages,- )+ try:+ if force or (total_tokens and total_tokens > self.agent_state.llm_config.context_window):+ self.logger.warning(+ f"Total tokens {total_tokens} exceeds configured max tokens {self.agent_state.llm_config.context_window}, forcefully clearing message history."+ )+ new_in_context_messages, updated = await self.summarizer.summarize(+ in_context_messages=in_context_messages,+ new_letta_messages=new_letta_messages,+ force=True,+ clear=True,+ )+ else:+ # NOTE (Sarah): Seems like this is doing nothing?+ self.logger.info(+ f"Total tokens {total_tokens} does not exceed configured max tokens {self.agent_state.llm_config.context_window}, passing summarizing w/o force."+ )+ new_in_context_messages, updated = await self.summarizer.summarize(+ in_context_messages=in_context_messages,+ new_letta_messages=new_letta_messages,+ )+ except Exception as e:+ self.logger.error(f"Failed to summarize conversation history: {e}")+ new_in_context_messages = in_context_messages + new_letta_messageselse:new_in_context_messages = in_context_messages + new_letta_messagesdiff --git a/letta/agents/letta_agent_v3.py b/letta/agents/letta_agent_v3.pyindex 7dc284021..a7aee2130 100644--- a/letta/agents/letta_agent_v3.py+++ b/letta/agents/letta_agent_v3.py@@ -224,10 +224,10 @@ class LettaAgentV3(LettaAgentV2):force=False,)- except:- if self.stop_reason and not first_chunk:- yield f"data: {self.stop_reason.model_dump_json()}\n\n"- raise+ except Exception as e:+ self.logger.error("Error during agent stream", e)+ if first_chunk:+ raise # only raise if first chunk has not been streamed yetif run_id:letta_messages = Message.to_letta_messages_from_list(@@ -424,6 +424,7 @@ class LettaAgentV3(LettaAgentV2):force=True,)else:+ self.stop_reason = LettaStopReason(stop_reason=StopReasonType.llm_api_error.value)raise estep_progression, step_metrics = self._step_checkpoint_llm_request_finish(@@ -505,8 +506,6 @@ class LettaAgentV3(LettaAgentV2):# TODO should we be logging this even if persisted_messages is empty? Technically, there still was an LLM callstep_progression, step_metrics = await self._step_checkpoint_finish(step_metrics, agent_step_span, logged_step)except Exception as e:- import traceback-self.logger.warning(f"Error during step processing: {e}")self.job_update_metadata = {"error": str(e)}